|
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; |
|
} |
|
} |