Skip to content

Instantly share code, notes, and snippets.

@snorpey
Last active September 27, 2015 10:35
Show Gist options
  • Save snorpey/4d72a8c64291464099bb to your computer and use it in GitHub Desktop.
Save snorpey/4d72a8c64291464099bb to your computer and use it in GitHub Desktop.
B5 am Feiertag Downloader

B5 am Feiertag Downloader

English readme see below

Deutsch

Dieses Skript erlaubt es, alle Episoden eines Jahres von "Notizen aus ..." des Bayerischen Rundfunks auf die Festplate herunterzuladen.

Installation

  • node.js installieren
  • dieses Skript herunterladen und entpacken
  • Konsole öffnen
  • Den Ordner, in dem das Skript liegt in der Konsole öffnen
  • npm install in der Konsole ausführen
  • node index.js in der Konsole ausführen

Benutzung

node index.js in der Konsole ausführen

Die Jahreszahl kann relativ einfach im Quelltext der Datei index.js angepasst werden. Voreingestellt ist das aktuelle Jahr.

English

This script downloads all "Notizen aus ..." Episodes of a given year.

Installation

  • Install node.js
  • Download and extract this script on your hard drive
  • Open terminal
  • change directory to the folder you extracted the script to
  • run npm install

Use

run node index.js in the terminal

var fs = require( 'fs' );
var cheerio = require( 'cheerio' );
var request = require( 'request' );
var httpreq = require( 'httpreq' );
var mkpath = require( 'mkpath' );
var list_url = 'http://www.br.de/service/suche/suche104.html?documentTypes=broadcast&page=__PAGE__&query=%22Notizen+aus+aller+Welt%22&sort=date&resultsPerPage=40';
var episodes = [ ];
var current_episode_index = -1;
var current_page_index = 1;
var last_page_index;
// you can replace this with a number, e.g:
// var YEAR = 2012;
var YEAR = new Date().getFullYear();
updateNextPage();
function updateNextPage () {
request(
list_url.replace( '__PAGE__', current_page_index ),
function ( error, response, content ) {
if ( ! error && response.statusCode === 200 ) {
var $list = cheerio.load( content );
var search_results = $list( '.detail_inlay .search_result' );
var episode, title, page_url, year;
search_results.each(
function ( index, item ) {
title = $list( item ).find( '.teaser_title' ).text();
year = parseInt( $list( item ).find( '.search_date' ).text().split('|')[0].split('.')[2], 10 );
page_url = 'http://www.br.de' + $list( item ).find( '.link_broadcast' ).attr( 'href' );
episode = { title: title, url: page_url, year: year };
if ( year === YEAR ) {
console.log( 'found episode: ' + year + ' - ' + title );
episodes.push( episode );
}
}
);
if ( ! last_page_index ) {
last_page_index = parseInt( $list('.search_navi_list .last_numb').text(), 10 );
}
if ( current_episode_index < last_page_index ) {
current_episode_index++;
updateNextPage();
} else {
downloadNextEpisode();
}
}
}
);
}
function downloadNextEpisode () {
if ( current_episode_index < episodes.length - 1 ) {
current_episode_index++;
var episode = episodes[current_episode_index];
request(
episode.url,
function ( error, response, content ) {
if ( ! error && response.statusCode === 200 ) {
var $page = cheerio.load( content );
var detail_url = 'http://www.br.de' + $page( 'a.contenttype_podcast.link_arrow' ).attr( 'href' );
request(
detail_url,
function ( error, response, content ) {
if ( ! error && response.statusCode === 200 ) {
var $detail = cheerio.load( content );
var src = $detail( 'a.dl_button' ).attr( 'href' );
episode.src = src;
console.log( 'downloading ' + episode.title + '...' );
// updateNextEpisodeSrc();
httpreq.get(
episode.src,
{ binary: true },
function ( err, res ) {
if ( err ) {
console.log( 'ERROR REQUESTING FILE', err );
return;
} else {
var path = __dirname + '/episodes/' + episode.year;
mkpath( path, function ( err ) {
if ( err ) {
console.log( 'unable to create directory', path, err );
return;
}
fs.writeFile(
path + '/' + episode.title + '.mp3',
res.body,
function ( err ) {
if ( err ) {
console.log( 'ERROR WRITING FILE', episode.title );
}
console.log( 'finished downloading ' + episode.title + '.' );
downloadNextEpisode();
}
);
} );
}
}
);
}
}
);
}
}
);
} else {
return;
}
}
{
"name": "b5-am-feiertag",
"version": "0.0.1",
"devDependencies": {},
"dependencies": {
"cheerio": "^0.18.0",
"httpreq": "^0.4.3",
"mkpath": "^0.1.0",
"request": "^2.51.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment