From c2a99845596e55583d52402c1fa200ee35979c3c Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sun, 24 Feb 2013 11:01:28 +0100 Subject: [PATCH] added a utility to load sample tracks --- .gitignore | 4 +- package.json | 23 +++++----- util/artist-ids.js | 33 +++++++++++++++ util/load_sample_tracks.js | 86 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+), 12 deletions(-) create mode 100644 util/artist-ids.js create mode 100644 util/load_sample_tracks.js diff --git a/.gitignore b/.gitignore index bf79e26..8be1936 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -.npmignore -node_modules/ db/ +node_modules/ +.npmignore diff --git a/package.json b/package.json index 3aa5d9e..429ef80 100644 --- a/package.json +++ b/package.json @@ -1,25 +1,28 @@ { "analyze": false, "bundleDependencies": [ - "async", - "connect-redis", - "express", - "jade", - "nodemailer", - "redis", - "socket.io" + "express" ], "dependencies": { + "async": "0.2.x", "canvas": "0.13.x", - "ws": "0.4.x" + "connect-redis": "1.4.x", + "express": "3.x", + "jade": "0.28.x", + "nodemailer": "0.3.x", + "redis": "0.7.x", + "socket.io": "0.9.x" + }, + "devDependencies": { + "JSONStream": "0.4.x" }, "engines": { "node": "0.8.x" }, "name": "binb", "scripts": { - "start": "app.js" + "start": "node app.js" }, "subdomain": "binb", - "version": "0.3.5-7" + "version": "0.3.5-8" } diff --git a/util/artist-ids.js b/util/artist-ids.js new file mode 100644 index 0000000..c15c275 --- /dev/null +++ b/util/artist-ids.js @@ -0,0 +1,33 @@ +/** + * Expose an object with some iTunes artist IDs. + */ + +module.exports = { + pop: [ + 262836961 // ADELE + , 459885 // Avril Lavigne + , 1419227 // Beyoncé + , 217005 // Britney Spears + , 64387566 // Katy Perry + , 277293880 // Lady GaGa + , 184932871 // MIKA + ], + rap: [ + 1587965 // A Tribe Called Quest + , 1971863 // Beastie Boys + , 465802 // Cypress Hill + , 384304 // EPMD + , 289550 // OutKast + , 13503763 // Swollen Members + , 43680 // The Roots + ], + rock: [ + 5040714 // AC/DC + , 462006 // Bob Dylan + , 994656 // Led Zeppelin + , 3296287 // Queen + , 562555 // The Beach Boys + , 136975 // The Beatles + , 62819 // The Jimi Hendrix Experience + ] +}; diff --git a/util/load_sample_tracks.js b/util/load_sample_tracks.js new file mode 100644 index 0000000..54f3ada --- /dev/null +++ b/util/load_sample_tracks.js @@ -0,0 +1,86 @@ +/** + * Module dependencies. + */ + +var artistIds = require('./artist-ids') + , http = require('http') + , JSONStream = require('JSONStream') + , limit = 7 // The number of songs to retrieve for each artist + , parser = JSONStream.parse(['results', true]) + , popIds = artistIds.pop + , rapIds = artistIds.rap + , rc = require('redis').createClient() + , rockIds = artistIds.rock + , rooms = require('../config').rooms + , skip = 0 // Skip counter + , songId = 0; + +var options = { + headers: {'content-type': 'application/json'}, + host: 'itunes.apple.com', + // Look up multiple artists by their IDs and get `limit` songs for each one + path: '/lookup?id='+popIds.concat(rapIds, rockIds).join()+'&entity=song&limit='+limit, + port: 80 +}; + +/** + * Set the rooms in which the songs of a given artist will be loaded. + */ + +var updateRooms = function(artistId) { + rooms = ['mixed']; + if (artistId === popIds[0]) { + rooms.push('hits', 'pop'); + // Set the skip counter (there is no need to update the rooms for the next pop artists) + skip = popIds.length - 1; + } + else if (artistId === rapIds[0]) { + rooms.push('rap'); + skip = rapIds.length - 1; + } + else { + rooms.push('oldies', 'rock'); + skip = rockIds.length - 1; + } +}; + +parser.on('data', function(track) { + if (track.wrapperType === 'artist') { + if (skip) { + skip--; + return; + } + updateRooms(track.artistId); + return; + } + + rc.hmset('song:'+songId, + 'artistName', track.artistName, + 'trackName', track.trackName, + 'trackViewUrl', track.trackViewUrl, + 'previewUrl', track.previewUrl, + 'artworkUrl60', track.artworkUrl60, + 'artworkUrl100', track.artworkUrl100 + ); + + rooms.forEach(function(room) { + rc.sadd(room, songId); + }); + + songId++; +}); + +parser.on('root', function() { + rc.quit(); + process.stdout.write('OK\n'); +}); + +rc.del(rooms, function(err) { + if (err) { + throw err; + } + process.stdout.write('Loading sample tracks... '); + http.get(options, function(res) { + res.pipe(parser); + }); +}); -- 2.54.0