Skip to content

Commit

Permalink
added support for configurable radio stations
Browse files Browse the repository at this point in the history
  • Loading branch information
crohrer committed Jan 3, 2016
1 parent 6a7df6c commit fb6519c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
4 changes: 4 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
"clientSecret": "",
"userId": "",
"playlistId": "",
"radioTrackserviceUrl": "http://fm4.orf.at/trackservicepopup/main",
"radioTitleSelector": "b",
"radioArtistSelector": "i",
"fm4": true,
"localEnvironment": true
}
26 changes: 18 additions & 8 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ var playlistTracks = [];

getAllPlaylistTracks();

function getRadioTracks(){
function getRadioTracks(trackserviceUrl){
console.log('getting tracks from radio trackservice');
var trackserviceReq = http.request({
hostname: 'fm4.orf.at',
path: '/trackservicepopup/main'
}, function(res) {
var trackserviceReq = http.request(trackserviceUrl, function(res) {
var html = '';
if(res.statusCode === 302){
console.log('following redirect to ' + res.headers.location);
getRadioTracks(res.headers.location);
return;
}
if(res.statusCode !== 200){
logger.log('Trackservice Error: Status '+res.statusCode);
process.exit(1);
Expand All @@ -32,14 +34,22 @@ function getRadioTracks(){
res.on('end', function() {
var $ = cheerio.load(html);
var tracks = [];
$('b').each(function(i, elem){
$(config.radioTitleSelector).each(function(i, elem){
var $title = $(this);
var $artist = $title.next('i');
var $artist;
if(config.fm4){ // special handling for weird html structure on fm4 trackservice
$artist = $title.next(config.radioArtistSelector);
} else {
$artist = $title.siblings(config.radioArtistSelector);
}
tracks.push({
title: $title.text(),
artist: $artist.text()
});
});
if(tracks.length === 0){
logger.log('no tracks found on radio trackservice.');
}
searchSpotify(tracks);
});
});
Expand Down Expand Up @@ -95,7 +105,7 @@ function getPlaylistTracks(offset){
playlistTracks.push(item.track.uri);
});
if(data.next === null){
getRadioTracks();
getRadioTracks(config.radioTrackserviceUrl);
} else {
getPlaylistTracks(offset + LIMIT);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spotifyRadioPlaylist",
"version": "1.0.0",
"version": "2.0.0",
"dependencies": {
"cheerio": "^0.19.0"
}
Expand Down

0 comments on commit fb6519c

Please sign in to comment.