Skip to content

Commit 2823fc7

Browse files
authored
Merge pull request #257 from fraz3alpha/make-technical-info-parse-more-robust
Make technical info parse more robust
2 parents 2900746 + bcb42f1 commit 2823fc7

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

browser-extensions/common/js/background.js

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -253,38 +253,49 @@ function addEventToCountryData(data, country_name, event_id, event_name) {
253253

254254
function parse_tee_data_event_status(data, result) {
255255

256-
if (result === undefined) {
257-
return
258-
}
256+
var parseSuccess = false
257+
258+
if (result !== undefined) {
259+
260+
// Reset the event status data to a blank map
261+
data.event_status = {}
262+
263+
var ownerDocument = document.implementation.createHTMLDocument('virtual');
264+
// Load the results into a virtual document, so that it doesn't attempt to load
265+
// inline scripts etc...
266+
// Solution taken from https://stackoverflow.com/questions/15113910/jquery-parse-html-without-loading-images
267+
// referencing https://api.jquery.com/jQuery/ & https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createHTMLDocument
268+
$(result, ownerDocument).find('div[id=mw-content-text]>table:first').each(function(table_index) {
269+
var content_table = $(this)
270+
271+
content_table.find('tbody>tr').each(function(row_index) {
272+
var content_table_row_cell = $('td', this)
273+
// Only attempt to parse it if there are enough cells
274+
if (content_table_row_cell[0] !== undefined) {
275+
if (content_table_row_cell.length >= 5) {
276+
var parkrun_info = {
277+
parkrun_name: content_table_row_cell[0].innerText.trim(),
278+
parkrun_event_director: content_table_row_cell[1].innerText.trim(),
279+
parkrun_event_number: content_table_row_cell[2].innerText.trim(),
280+
parkrun_status: content_table_row_cell[3].innerText.trim(),
281+
parkrun_country: content_table_row_cell[4].innerText.trim()
282+
}
283+
data.event_status[parkrun_info.parkrun_event_number] = parkrun_info
284+
// Note that we've parsed at least something successfully.
285+
parseSuccess = true
286+
// console.log(parkrun_info)
287+
}
288+
} else {
289+
console.log("Techincal Event Information table row is malformed: "+JSON.stringify(content_table_row_cell))
290+
}
291+
})
292+
})
259293

260-
// Reset the event status data to a blank map
261-
data.event_status = {}
262-
263-
var ownerDocument = document.implementation.createHTMLDocument('virtual');
264-
// Load the results into a virtual document, so that it doesn't attempt to load
265-
// inline scripts etc...
266-
// Solution taken from https://stackoverflow.com/questions/15113910/jquery-parse-html-without-loading-images
267-
// referencing https://api.jquery.com/jQuery/ & https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createHTMLDocument
268-
$(result, ownerDocument).find('div[id=mw-content-text]>table:first').each(function(table_index) {
269-
var content_table = $(this)
270-
271-
content_table.find('tbody>tr').each(function(row_index) {
272-
var content_table_row_cell = $('td', this)
273-
if (content_table_row_cell[0] !== undefined) {
274-
var parkrun_info = {
275-
parkrun_name: content_table_row_cell[0].innerText.trim(),
276-
parkrun_event_director: content_table_row_cell[1].innerText.trim(),
277-
parkrun_event_number: content_table_row_cell[2].innerText.trim(),
278-
parkrun_status: content_table_row_cell[3].innerText.trim(),
279-
parkrun_country: content_table_row_cell[4].innerText.trim()
280-
}
281-
data.event_status[parkrun_info.parkrun_event_number] = parkrun_info
282-
// console.log(parkrun_info)
283-
}
284-
})
285-
})
294+
console.log(Object.keys(data.event_status).length + " event statuses available")
295+
296+
}
286297

287-
return
298+
return parseSuccess
288299

289300
}
290301

@@ -433,9 +444,10 @@ function update_cache_data(data_events, data_tee) {
433444

434445
// If the technical event information has been obtained, then
435446
// lets parse that.
436-
// if (data_tee !== null) {
437-
parse_tee_data_event_status(data, data_tee)
438-
// }
447+
var parseResult = parse_tee_data_event_status(data, data_tee)
448+
console.log("Techincal Event Information parse result: "+parseResult)
449+
// If the page hasn't been fetched, or the file can't be parsed, parseResult will be false.
450+
// We should do something with that value, like mark that the page should be fetched again.
439451

440452
// This could potentially do nothing if no event info is available
441453
compute_event_status(data)

browser-extensions/common/js/lib/challenges_ui.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ function drawRegionnaireMap(divId, data) {
530530
$.each(data.geo_data.data.countries, function (countryName, countryInfo) {
531531

532532
// We have the total number of events and complete events in the following
533-
var countryChildEventsCount = countryCompletionInfo[countryName].childEventsCount
533+
var countryChildEventsCount = countryCompletionInfo[countryName].childActiveEventsCount
534534
var countryChildEventsCompletedCount = countryCompletionInfo[countryName].childEventsCompletedCount
535535

536536
// Only bother displaying this country if it has any events
@@ -1038,7 +1038,7 @@ function drawRegionnaireDataTable(table, data) {
10381038
var worldEventsCount = 0
10391039
var worldEventsCompletedCount = 0
10401040
$.each(countryCompletionInfo, function(countryName, countryInfo) {
1041-
worldEventsCount += countryInfo.childEventsCount
1041+
worldEventsCount += countryInfo.childActiveEventsCount
10421042
worldEventsCompletedCount += countryInfo.childEventsCompletedCount
10431043
})
10441044

0 commit comments

Comments
 (0)