Skip to content
Browse files

add server.js back

  • Loading branch information...
1 parent 45be0d5 commit 4425061196d9ff2538fda8c0f78ff18825cfcf5b @stoive committed May 3, 2012
Showing with 210 additions and 0 deletions.
  1. +210 −0 server.js
View
210 server.js
@@ -0,0 +1,210 @@
+var express = require('express');
+var request = require('request');
+var util = require('util');
+var url = require('url');
+var querystring = require('querystring');
+var _ = require('underscore');
+var nano = require('nano')("http://localhost:5984/");
+var db = nano.use('mhd');
+var libxmljs = require('libxmljs');
+
+var sevenDigital = {
+ auth: {
+ consumer_key: '',
+ consumer_secret: ''
+ }
+};
+
+var echonest = {
+ api_key: '',
+ path: 'http://developer.echonest.com/api/v4/',
+ auth: {
+ consumer_key: '',
+ consumer_secret: ''
+ }
+}
+
+var lastfm = {
+ "apiKey": "",
+ "secret": ""
+};
+
+var soundcloud = {
+ auth: {
+ consumer_key: '',
+ consumer_secret: ''
+ }
+};
+
+var app = express.createServer(
+ express.bodyParser()
+);
+
+app.listen(8000);
+
+app.get('/in', function(req, res, next) {
+ res.sendfile(__dirname + '/web/inside.html');
+});
+
+app.get('/out', function(req, res, next) {
+ res.sendfile(__dirname + '/web/outside.html');
+});
+
+process.on('uncaughtException', function (err) {
+ process.stderr.write(err + '\n');
+});
+
+app.get('/inside/search/artist/:artistid/track/:tracktitle', function(req, res, next) { /**/
+ request({
+ url: echonest.path + 'song/search?' + querystring.stringify({
+ api_key: echonest.api_key,
+ format: 'json',
+ results: 5,
+ artist_id: req.params.artistid,
+ title: req.params.tracktitle
+ }) + "&bucket=id:7digital-AU&bucket=audio_summary&bucket=tracks",
+ oauth: echonest.auth,
+ json: true
+ }, function(err, echoRes, user) {
+ //console.log(util.inspect(arguments, false, null));
+ res.json(echoRes.body.response.songs);
+ });
+});
+
+app.get('/inside/search/artist/:q', function(req, res, next) {
+ request({
+ url: echonest.path + 'artist/search?' + querystring.stringify({
+ api_key: echonest.api_key,
+ format: 'json',
+ results: 5,
+ name: req.params.q
+ }),
+ oauth: echonest.auth,
+ json: true
+ }, function(err, echoRes, user) {
+ res.json(echoRes.body.response.artists);
+ });
+});
+
+app.get('/events', function(req, res, next) {
+ request('http://ws.audioscrobbler.com/2.0/?' + querystring.stringify({
+ method: "geo.getevents",
+ location: 'sydney',
+ api_key: lastfm.apiKey
+ }), function(err, lastfmRes) {
+ res.send(lastfmRes.body, {'Content-Type': 'application/xml'});
+ });
+});
+
+
+app.post('/playlist/new/:venue', function(req, res, next) { /* venue=some%20venue */
+ // create a 7digital basket
+ // extract GUID for basket, save to...
+ request({
+ url: 'http://api.7digital.com/1.2/basket/create?' + querystring.stringify({
+ country: 'AU'
+ }),
+ oauth: sevenDigital.auth
+ }, function(err, sdRes) {
+ var basketId = libxmljs.parseXmlString(sdRes.body).get('//basket/@id').text();
+ db.insert({ event: { venue: req.params.venue }, basketId: basketId, tracks: [] }, function(err, dbRes) {
+ res.json(dbRes);
+ });
+ });
+});
+
+app.get('/playlist/get/:id', function(req, res, next) {
+ db.get(req.params.id, function(err, body) {
+ res.json(body);
+ });
+});
+
+app.get('/playlist/basket/:id', function(req, res, next) {
+ db.get(req.params.id, function(err, body) {
+ request({
+ url: 'http://api.7digital.com/1.2/basket?' + querystring.stringify({
+ country: 'AU',
+ basketId: body.basketId
+ }),
+ oauth: sevenDigital.auth
+ }, function(err, sdRes) {
+ res.send(sdRes.body, {'Content-Type': 'application/xml'});
+ });
+ });
+});
+
+app.get('/playlist/search/:q', function(req, res, next) { /* q=some%20location */
+ db.view('search', 'query', {
+ startkey: req.params.q,
+ endkey: req.params.q + String.fromCharCode(0xffff)
+ }, function(err, dbRes) {
+ res.json(dbRes.rows);
+ });
+});
+
+app.get('/sc/:artist/:track', function(req, res, next) {
+ request({
+ url: 'http://api.soundcloud.com/tracks.json?' + querystring.stringify({
+ q: req.params.artist + ' ' + req.params.track
+ }),
+ oauth: soundcloud.auth
+ }, function(err, scRes) {
+ res.json(JSON.parse(scRes.body));
+ });
+});
+
+app.put('/playlist/update/:id', function(req, res, next) {
+ // check if tracks.pop() has 7digital ID, add to basket if so
+ if (req.body.basketId && req.body.tracks[req.body.tracks.length-1].foreign_id) {
+ var trackId = req.body.tracks[req.body.tracks.length-1].foreign_id.split(':').pop();
+ request({
+ url: 'http://api.7digital.com/1.2/track/details?' + querystring.stringify({
+ trackId: trackId,
+ country: 'AU'
+ }),
+ oauth: sevenDigital.auth
+ }, function(err, sdRes) {
+ var releaseId = libxmljs.parseXmlString(sdRes.body).get('//release/@id').text();
+ request({
+ url: 'http://api.7digital.com/1.2/basket/addItem?' + querystring.stringify({
+ basketId: req.body.basketId,
+ releaseId: releaseId,
+ country: 'AU'
+ }),
+ oauth: sevenDigital.auth
+ }, function(err, sdRes) {
+ console.log(util.inspect(arguments, false, null));
+ });
+
+ });
+ }
+ // normalise tracks
+ if (req.body.tracks) {
+ req.body.tracks = req.body.tracks.filter(function(curr, i) {
+ return !req.body.tracks.slice(0, i).some(function(prev) { return prev.trackid == curr.trackid });
+ });
+ }
+ db.insert(req.body, function(err, dbRes) {
+ res.json(dbRes);
+ });
+
+});
+
+app.use(express.static(__dirname + '/web/', { /*maxAge: 1000 * 60 * 5*/ }));
+
+/*
+ * Echonest happy path: partial search of Miles Da[vis]
+ */
+/*
+request({
+ url: echonest.path + 'artist/search?api_key=' + echonest.api_key + "&format=json&results=5&name=miles%20da",
+ oauth: echonest.auth,
+ json: true
+}, function(err, res, user) {
+ console.log(util.inspect(_.pluck(res.body.response.artists, 'name'), false, null));
+});
+*/
+
+/*
+ * 7digital happy path: 3 track samples
+ */

0 comments on commit 4425061

Please sign in to comment.
Something went wrong with that request. Please try again.