Skip to content

Instantly share code, notes, and snippets.

@imjakechapman
Created June 21, 2016 00:45
Show Gist options
  • Select an option

  • Save imjakechapman/8c433de722e764e702b57f98fadf6312 to your computer and use it in GitHub Desktop.

Select an option

Save imjakechapman/8c433de722e764e702b57f98fadf6312 to your computer and use it in GitHub Desktop.
RedbullGRC Event Page Parser
// Usage
//
// 1. Copy this script and paste in to the dev tool console in chrome
// 2. type createRedbullGRCEvents().then(function() { console.log('PARSING EVENTS FINISHED'); });
// 3. After the console log appears type copy(window.RedbullGRCEVENTS)
// 4. JSON Object with event listings from that page are now copied to your clipboard, enjoy.
// Load jQuery onto the page, just in case it hasn't already been
function loadjQuery() {
// Load jQuery onto the page
var js = document.createElement("script");
js.type = "text/javascript";
js.src = "http://code.jquery.com/jquery.js";
document.body.appendChild(js);
}
// Loop through events page and find all event listings
// Get the link to that events details page
// Fetch event details page and parse through the return html
// Create individual events and push them on the window.RedbullGRCEVENTS object.
// Return promise to allow callback function in dev tools
function createRedbullGRCEvents() {
window.RedbullGRCEVENTS = {};
var deferred = $.Deferred();
var eventListings = $('.events_list').find('li.clearfix');
$.each(eventListings, function(i, listing) {
var listingLink = $(listing).find('.misc').find('a').first().attr('href');
$.get(listingLink, function(resp) {
var eventData = new EventDataGenerator(resp).createEventData();
RedbullGRCEVENTS[eventData.date] = eventData;
if(i === eventListings.length - 1) {
deferred.resolve();
}
});
});
return deferred;
}
function EventDataGenerator(parentSelector) {
var PARENT_ELEMENT = $(parentSelector);
var EVENTDATA = {
"isLive": false,
"title": null,
"date": null,
"previewImage": null,
"info": {
"intro": null,
"agenda": {
"1": {
"day": null,
"times": null,
},
"2": {
"day": null,
"times": null,
}
}
},
"results": {
"supercars": {
"qualifying1": null,
"qualifying2": null,
"qualifying3": null,
"qualifying4": null,
"semi-finalA": null,
"semi-finalB": null,
"lcq": null,
"final": null
},
"lites": {
"qualifying1": null,
"qualifying2": null,
"qualifying3": null,
"qualifying4": null,
"semi-finalA": null,
"semi-finalB": null,
"lcq": null,
"final": null
}
}
};
function createEventData() {
createEventTitle();
createEventIntro();
createEventTimestamp();
createEventAgenda();
return EVENTDATA;
}
function createEventTitle() {
EVENTDATA['title'] = $.trim(PARENT_ELEMENT.find('#event_detail').find('#hdr').find('#name').find('h2').text());
}
function createEventIntro() {
EVENTDATA['info']['intro'] = $.trim(PARENT_ELEMENT.find('#event_detail').find('#txt_r').find('p').first().text());
}
function createEventTimestamp() {
EVENTDATA['date'] = new Date(PARENT_ELEMENT.find('#name').find('h3').text().split('.').join('/')).getTime() / 1000;
}
function createEventAgenda() {
var eventScheduleTitle = PARENT_ELEMENT.find('#txt_r h4').filter(function() { return $(this).text() === 'Event Schedule' }).first();
// First Day
var dayOnetitleElement = eventScheduleTitle.next('h3');
var dayOneTitleText = dayOnetitleElement.find('em').find('b').text().split(/\s{4}/).join(' ');
EVENTDATA['info']['agenda']['1']['day'] = dayOneTitleText;
var dayOneTimes = dayOnetitleElement.next('ul');
var dayOneTimesArr = [];
$.each(dayOneTimes.find('li'), function(i, li) {
var text = $(li).text().split(/(AM|PM)/);
dayOneTimesArr.push({ "time": text[0] + text[1], "desc": $.trim(text[2]) });
EVENTDATA['info']['agenda']['1']['times'] = dayOneTimesArr;
});
// Second Day
var dayTwoTitleElement = eventScheduleTitle.next('h3').next('ul').next('h3');
var dayTwoTitleText = dayTwoTitleElement.find('em').find('b').text().split(/\s{4}/).join(' ');
EVENTDATA['info']['agenda']['2']['day'] = dayTwoTitleText;
var dayTwoTimes = dayTwoTitleElement.next('ul');
var dayTwoTimesArr = [];
$.each(dayTwoTimes.find('li'), function(i, li) {
var text = $(li).text().split(/(AM|PM)/);
dayTwoTimesArr.push({ "time": text[0] + text[1], "desc": $.trim(text[2]) });
EVENTDATA['info']['agenda']['2']['times'] = dayTwoTimesArr;
});
}
return {
createEventData: createEventData
}
}
loadjQuery();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment