Skip to content

Instantly share code, notes, and snippets.

@jehoshua02
Last active August 29, 2015 14:07
Show Gist options
  • Save jehoshua02/16fb42505fc0f6f4ce84 to your computer and use it in GitHub Desktop.
Save jehoshua02/16fb42505fc0f6f4ce84 to your computer and use it in GitHub Desktop.
Keeping track of General Conference Talks and Music we've revisited.
var getHtml = function () {
var html;
$.ajax({ url: 'gc.html', async: false, success: function (data) { html = data; } });
var $html = $('<div></div>').html(html).find('> *').eq(0);
return $html;
};
var extractMusicItem = function ($item) {
var item = {};
item.type = 'music';
item.name = $item.find('td').eq(0).find('a').text();
item.url = $item.find('td').eq(0).find('a').attr('href');
item.artist = $item.find('.choir').text();
item.watch = $item.find('.watch a').attr('href');
item.listen = $item.find('.listen a').attr('href');
return item;
};
var extractTalkItem = function ($item) {
var item = {};
item.type = 'talk';
item.name = $item.find('td').eq(0).find('.talk a').text();
item.url = $item.find('td').eq(0).find('.talk a').attr('href');
item.speaker = $item.find('.speaker').text().replace(/^(?:By |Presented by )/, '');
item.watch = $item.find('.watch a').attr('href');
item.listen = $item.find('.listen a').attr('href');
return item;
};
var extractVideoItem = function ($item) {
var item = {};
item.type = 'video';
item.name = $item.find('td').eq(0).find('.talk').text();
item.author = $item.find('td').eq(0).find('.speaker').text();
item.url = $item.find('.watch a').attr('href');
return item;
};
var extractSessionItem = function ($item) {
if ($item.is('.music')) {
return extractMusicItem($item);
} else if ($item.find('td').eq(0).find('.talk').text().indexOf('Video Presentation') !== -1) {
return extractVideoItem($item);
} else {
return extractTalkItem($item);
}
};
var extractSessionData = function ($session) {
var session = {};
// extract session data
session.name = $session.find('.head-row').find('td').eq(0).find('h2').text();
session.id = $session.attr('id');
session.url = 'https://www.lds.org/general-conference/sessions/2014/10#' + session.id;
// extract session items
session.items = [];
var $items = $session.find('.head-row').nextAll('tr');
$items.each(function (index, item) {
session.items.push(extractSessionItem($(item)));
});
return session;
};
var extractSessions = function ($html) {
var $primary = $('<div></div>').html($html).find('#primary');
var $sessions = $primary.find('.sessions[id]');
var sessions = [];
$sessions.each(function (index, session) {
sessions.push(extractSessionData($(session)));
});
return sessions;
};
var md = {};
String.prototype.repeat = function (num) {
return new Array( num + 1 ).join( this );
};
md.nl = "\n";
md.a = function (text, url) {
return '[' + text + '](' + url + ')';
};
md.h = function (level, text) {
return '#'.repeat(level) + ' ' + text;
};
md.li = function (text) {
return '+ ' + text;
};
md.check = function (text) {
return md.li('[ ] ' + text);
};
var musicItemToMarkdown = function (item) {
console.log(item);
var a = md.a(item.name + ', ' + item.artist, item.url);
var check = md.check(a);
return check;
};
var talkItemToMarkdown = function (item) {
var a = md.a(item.name + ', ' + item.speaker, item.url);
var check = md.check(a);
return check;
};
var videoItemToMarkdown = function (item) {
var a = md.a(item.name + ', ' + item.author, item.url);
var check = md.check(a);
return check;
};
var sessionToMarkdown = function (data) {
var h2 = md.h(2, md.a(data.name, data.url));
var list = [];
$.each(data.items, function (index, item) {
if (item.type === 'music') {
list.push(musicItemToMarkdown(item));
} else if (item.type === 'video') {
list.push(videoItemToMarkdown(item));
} else {
list.push(talkItemToMarkdown(item));
}
});
var markdown = h2 + md.nl.repeat(2) + list.join(md.nl);
return markdown;
};
var sessionsToMarkdown = function (data, date) {
var sessions = [];
$.each(data, function (index, session) {
sessions.push(sessionToMarkdown(session));
});
return sessions.join(md.nl.repeat(3));
};
$(function () {
var html = getHtml();
var sessions = extractSessions(html);
var markdown = sessionsToMarkdown(sessions);
$('.output__pre').text(markdown);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment