]> git.example.dev Git - binbsis50.git/commitdiff
added a utility to load sample tracks
authorLuigi Pinca <luigipinca@gmail.com>
Sun, 24 Feb 2013 10:01:28 +0000 (11:01 +0100)
committerLuigi Pinca <luigipinca@gmail.com>
Sun, 24 Feb 2013 10:01:28 +0000 (11:01 +0100)
.gitignore
package.json
util/artist-ids.js [new file with mode: 0644]
util/load_sample_tracks.js [new file with mode: 0644]

index bf79e26babe0276f5afb33188411461b6ee4038c..8be1936f0110c92d7fd99be8396c1205e15b7641 100644 (file)
@@ -1,3 +1,3 @@
-.npmignore
-node_modules/
 db/
+node_modules/
+.npmignore
index 3aa5d9ee405990bf1ceb20d41ee8b061ffe86360..429ef80596a5ef415ea47fb409ad434e1e160c13 100644 (file)
@@ -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 (file)
index 0000000..c15c275
--- /dev/null
@@ -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 (file)
index 0000000..54f3ada
--- /dev/null
@@ -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);
+  });
+});