Skip to content

Instantly share code, notes, and snippets.

@nikcorg
Last active January 3, 2016 03:29
Show Gist options
  • Select an option

  • Save nikcorg/8402679 to your computer and use it in GitHub Desktop.

Select an option

Save nikcorg/8402679 to your computer and use it in GitHub Desktop.
Radio Helsingin soittolistan sisällön kysely Spotifysta
var u = require("underscore");
var q = require("q");
var cheerio = require("cheerio");
var hyperquest = require("hyperquest");
var concat = require("concat-stream");
function trim(str) {
return str.trim();
}
function getUrl(url) {
var deferred = q.defer();
try {
var req = hyperquest.get(url);
req.on("response", function (r) {
if (r.statusCode !== 200) {
deferred.reject(new Error(r.statusCode));
}
});
req.on("error", deferred.reject);
req.pipe(concat(u.compose(deferred.resolve, String)));
} catch (e) {
console.log(e);
}
return deferred.promise;
}
function getTrackData(query) {
var deferred = q.defer();
var url = "http://ws.spotify.com/search/1/track.json?q=" + encodeURIComponent(query);
getUrl(url).then(u.compose(deferred.resolve, JSON.parse), deferred.reject);
return deferred.promise;
}
var sleepBetweenRequestsMs = 150;
var program = 1606;
var year = new Date().getYear() + 1900;
var month = new Date().getMonth() + 1;
if (process.argv.length > 2) {
program = process.argv[2];
}
if (process.argv.length > 3) {
year = process.argv[3];
}
if (process.argv.length > 4) {
month = process.argv[4];
}
getUrl(u.template("http://www.radiohelsinki.fi/soittolistat/?action=search&mo=<%= month %>&ye=<%= year %>&program=<%= program %>")({
program: program,
month: month,
year: year
})).
then(cheerio.load.bind(cheerio)).
then(function (dom) {
return dom(".playlist a").
map(function (idx, el) {
return cheerio(el).text();
}).
map(function (name) {
return name.split(/:+/, 2).map(trim);
}).
map(function (details) {
return details[1] + " artist:" + details[0];
}).
sort();
}).
then(u.uniq).
then(function (tracks) {
var start = Date.now();
return tracks.reduce(function (prev, query, idx) {
return prev.then(function () {
process.stderr.write((idx + 1) + " (" + ((Date.now() - start) / 1000) + "s) " + query);
return getTrackData(query).
then(function (trackdata) {
if (trackdata.info.num_results > 0) {
process.stderr.write("...found it\n");
return process.stdout.write(trackdata.tracks[0].href + "\n");
}
return process.stderr.write("...no results\n");
}).
delay(sleepBetweenRequestsMs);
}).
fail(function (err) {
process.stderr.write("...failed: " + err.message + "\n");
});
}, q.resolve());
}).
done();
@nikcorg

nikcorg commented Jan 13, 2014

Copy link
Copy Markdown
Author

Find program ID:s on http://www.radiohelsinki.fi/soittolistat/

Invoke on cmd line with program id and optionally month and year as params (use 0 and 0 for all time). Redirect output to file (or pbcopy on a mac) and paste results onto new Spotify playlist.

Not very user friendly, but it's (so far still) just a one time hack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment