Skip to content

Instantly share code, notes, and snippets.

@maranomynet
Last active July 7, 2021 17:36
Show Gist options
  • Save maranomynet/d13e984fb4a22b19a2df to your computer and use it in GitHub Desktop.
Save maranomynet/d13e984fb4a22b19a2df to your computer and use it in GitHub Desktop.
This American Life - RSS Archive Builder
// Usage:
// 1. Visit http://www.thisamericanlife.org/radio-archives
// 2. Paste this script into the developer console of your browser.
// 3. Wait...
// 4. Copy-paste the resulting XML into a file.
//
(function(){
var jQuery = window.jQuery;
var doc = document;
Element.prototype.$ = document.$ = function (selector) {
return [].slice.call(this.querySelectorAll(selector));
};
var xml = doc.implementation.createDocument(null, null);
var appendChildren = function (elm, children) {
for (var i=0,l=children.length; i<l; i++) {
var child = children[i];
if ( child instanceof Array ) {
appendChildren(elm, child);
break;
}
if ( typeof child === 'string' ) {
child = xml.createTextNode( child );
}
if ( child ) {
elm.appendChild( child );
}
}
};
var x = function (tagname) {
var children = [].slice.call(arguments, 1);
var attrs = {};
if ( children && typeof children[0] !== 'string' && !(children[0] instanceof Node) ) {
attrs = children.shift();
}
var elm = xml.createElement(tagname);
for (var attrName in attrs) {
elm.setAttribute(attrName, attrs[attrName]);
}
children && appendChildren(elm, children);
return elm;
};
var episodes = [];
var buildXml = function () {
var rss = x('rss', { version:'2.0' },
'\n',
x('channel',
'\n',
x('title', 'This American Life Archive (Unofficial)'),
'\n',
x('link', 'http://www.thisamericanlife.org'),
'\n',
x('copyright', 'Copyright 1995-2015 Ira Glass'),
'\n',
x('description', 'All the episodes'),
'\n',
x('language', 'en'),
'\n',
episodes
),
'\n'
);
doc.head.parentNode.removeChild( doc.head );
doc.body.innerHTML = '<pre/>';
doc.body.firstChild.textContent =
'<?xml version="1.0" encoding="UTF-8"?>\n' +
rss.outerHTML;
var selection = window.getSelection();
var range = doc.createRange();
range.selectNodeContents( doc.body );
selection.removeAllRanges();
selection.addRange(range);
};
var years = doc.$('#archive-date-nav a');
var yearIdx = 0;
var loadNextYear = function () {
if ( yearIdx < years.length ) {
var url = years[yearIdx].href;
jQuery.get( url, function (result) {
window.console.log('Got ', url);
var page = doc.createElement('result');
page.innerHTML = result.replace(/<img/gi, '<_img');
page.$('#archive-episodes >.item-list > ul > li').forEach(function (episode) {
var dateElm = episode.$('.date')[0];
dateElm.parentNode.removeChild( dateElm );
var d = dateElm.textContent.trim().split('.');
var date = new Date( d[2], d[0] - 1, d[1] ).toString().replace(/[\+\-]\d{4} \(GMT\)/g,'').replace(/ /,', ');
var title = episode.$('h3 > a')[0].textContent.trim();
var number = title.split(':')[0];
var summary = episode.$('.content')[0].textContent.trim();
episodes[episodes.length] =
x('item', '\n',
x('title', '#'+title), '\n',
x('guid', { isPermaLink:false }, 'Episode '+ number +' of This American Life'), '\n',
x('description', summary), '\n',
x('pubDate', date), '\n',
x('enclosure', {
url: 'http://podcast.thisamericanlife.org/podcast/'+ number +'.mp3',
type: 'audio/mpeg',
}), '\n'
);
episodes[episodes.length] = '\n';
});
setTimeout(loadNextYear, 500+Math.random()*1000);
});
yearIdx++;
}
else {
buildXml();
}
};
loadNextYear();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment