Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add server.js back

  • Loading branch information...
commit 4425061196d9ff2538fda8c0f78ff18825cfcf5b 1 parent 45be0d5
Steven Thurlow authored

Showing 1 changed file with 210 additions and 0 deletions. Show diff stats Hide diff stats

  1. +210 0 server.js
210 server.js
... ... @@ -0,0 +1,210 @@
  1 +var express = require('express');
  2 +var request = require('request');
  3 +var util = require('util');
  4 +var url = require('url');
  5 +var querystring = require('querystring');
  6 +var _ = require('underscore');
  7 +var nano = require('nano')("http://localhost:5984/");
  8 +var db = nano.use('mhd');
  9 +var libxmljs = require('libxmljs');
  10 +
  11 +var sevenDigital = {
  12 + auth: {
  13 + consumer_key: '',
  14 + consumer_secret: ''
  15 + }
  16 +};
  17 +
  18 +var echonest = {
  19 + api_key: '',
  20 + path: 'http://developer.echonest.com/api/v4/',
  21 + auth: {
  22 + consumer_key: '',
  23 + consumer_secret: ''
  24 + }
  25 +}
  26 +
  27 +var lastfm = {
  28 + "apiKey": "",
  29 + "secret": ""
  30 +};
  31 +
  32 +var soundcloud = {
  33 + auth: {
  34 + consumer_key: '',
  35 + consumer_secret: ''
  36 + }
  37 +};
  38 +
  39 +var app = express.createServer(
  40 + express.bodyParser()
  41 +);
  42 +
  43 +app.listen(8000);
  44 +
  45 +app.get('/in', function(req, res, next) {
  46 + res.sendfile(__dirname + '/web/inside.html');
  47 +});
  48 +
  49 +app.get('/out', function(req, res, next) {
  50 + res.sendfile(__dirname + '/web/outside.html');
  51 +});
  52 +
  53 +process.on('uncaughtException', function (err) {
  54 + process.stderr.write(err + '\n');
  55 +});
  56 +
  57 +app.get('/inside/search/artist/:artistid/track/:tracktitle', function(req, res, next) { /**/
  58 + request({
  59 + url: echonest.path + 'song/search?' + querystring.stringify({
  60 + api_key: echonest.api_key,
  61 + format: 'json',
  62 + results: 5,
  63 + artist_id: req.params.artistid,
  64 + title: req.params.tracktitle
  65 + }) + "&bucket=id:7digital-AU&bucket=audio_summary&bucket=tracks",
  66 + oauth: echonest.auth,
  67 + json: true
  68 + }, function(err, echoRes, user) {
  69 + //console.log(util.inspect(arguments, false, null));
  70 + res.json(echoRes.body.response.songs);
  71 + });
  72 +});
  73 +
  74 +app.get('/inside/search/artist/:q', function(req, res, next) {
  75 + request({
  76 + url: echonest.path + 'artist/search?' + querystring.stringify({
  77 + api_key: echonest.api_key,
  78 + format: 'json',
  79 + results: 5,
  80 + name: req.params.q
  81 + }),
  82 + oauth: echonest.auth,
  83 + json: true
  84 + }, function(err, echoRes, user) {
  85 + res.json(echoRes.body.response.artists);
  86 + });
  87 +});
  88 +
  89 +app.get('/events', function(req, res, next) {
  90 + request('http://ws.audioscrobbler.com/2.0/?' + querystring.stringify({
  91 + method: "geo.getevents",
  92 + location: 'sydney',
  93 + api_key: lastfm.apiKey
  94 + }), function(err, lastfmRes) {
  95 + res.send(lastfmRes.body, {'Content-Type': 'application/xml'});
  96 + });
  97 +});
  98 +
  99 +
  100 +app.post('/playlist/new/:venue', function(req, res, next) { /* venue=some%20venue */
  101 + // create a 7digital basket
  102 + // extract GUID for basket, save to...
  103 + request({
  104 + url: 'http://api.7digital.com/1.2/basket/create?' + querystring.stringify({
  105 + country: 'AU'
  106 + }),
  107 + oauth: sevenDigital.auth
  108 + }, function(err, sdRes) {
  109 + var basketId = libxmljs.parseXmlString(sdRes.body).get('//basket/@id').text();
  110 + db.insert({ event: { venue: req.params.venue }, basketId: basketId, tracks: [] }, function(err, dbRes) {
  111 + res.json(dbRes);
  112 + });
  113 + });
  114 +});
  115 +
  116 +app.get('/playlist/get/:id', function(req, res, next) {
  117 + db.get(req.params.id, function(err, body) {
  118 + res.json(body);
  119 + });
  120 +});
  121 +
  122 +app.get('/playlist/basket/:id', function(req, res, next) {
  123 + db.get(req.params.id, function(err, body) {
  124 + request({
  125 + url: 'http://api.7digital.com/1.2/basket?' + querystring.stringify({
  126 + country: 'AU',
  127 + basketId: body.basketId
  128 + }),
  129 + oauth: sevenDigital.auth
  130 + }, function(err, sdRes) {
  131 + res.send(sdRes.body, {'Content-Type': 'application/xml'});
  132 + });
  133 + });
  134 +});
  135 +
  136 +app.get('/playlist/search/:q', function(req, res, next) { /* q=some%20location */
  137 + db.view('search', 'query', {
  138 + startkey: req.params.q,
  139 + endkey: req.params.q + String.fromCharCode(0xffff)
  140 + }, function(err, dbRes) {
  141 + res.json(dbRes.rows);
  142 + });
  143 +});
  144 +
  145 +app.get('/sc/:artist/:track', function(req, res, next) {
  146 + request({
  147 + url: 'http://api.soundcloud.com/tracks.json?' + querystring.stringify({
  148 + q: req.params.artist + ' ' + req.params.track
  149 + }),
  150 + oauth: soundcloud.auth
  151 + }, function(err, scRes) {
  152 + res.json(JSON.parse(scRes.body));
  153 + });
  154 +});
  155 +
  156 +app.put('/playlist/update/:id', function(req, res, next) {
  157 + // check if tracks.pop() has 7digital ID, add to basket if so
  158 + if (req.body.basketId && req.body.tracks[req.body.tracks.length-1].foreign_id) {
  159 + var trackId = req.body.tracks[req.body.tracks.length-1].foreign_id.split(':').pop();
  160 + request({
  161 + url: 'http://api.7digital.com/1.2/track/details?' + querystring.stringify({
  162 + trackId: trackId,
  163 + country: 'AU'
  164 + }),
  165 + oauth: sevenDigital.auth
  166 + }, function(err, sdRes) {
  167 + var releaseId = libxmljs.parseXmlString(sdRes.body).get('//release/@id').text();
  168 + request({
  169 + url: 'http://api.7digital.com/1.2/basket/addItem?' + querystring.stringify({
  170 + basketId: req.body.basketId,
  171 + releaseId: releaseId,
  172 + country: 'AU'
  173 + }),
  174 + oauth: sevenDigital.auth
  175 + }, function(err, sdRes) {
  176 + console.log(util.inspect(arguments, false, null));
  177 + });
  178 +
  179 + });
  180 + }
  181 + // normalise tracks
  182 + if (req.body.tracks) {
  183 + req.body.tracks = req.body.tracks.filter(function(curr, i) {
  184 + return !req.body.tracks.slice(0, i).some(function(prev) { return prev.trackid == curr.trackid });
  185 + });
  186 + }
  187 + db.insert(req.body, function(err, dbRes) {
  188 + res.json(dbRes);
  189 + });
  190 +
  191 +});
  192 +
  193 +app.use(express.static(__dirname + '/web/', { /*maxAge: 1000 * 60 * 5*/ }));
  194 +
  195 +/*
  196 + * Echonest happy path: partial search of Miles Da[vis]
  197 + */
  198 +/*
  199 +request({
  200 + url: echonest.path + 'artist/search?api_key=' + echonest.api_key + "&format=json&results=5&name=miles%20da",
  201 + oauth: echonest.auth,
  202 + json: true
  203 +}, function(err, res, user) {
  204 + console.log(util.inspect(_.pluck(res.body.response.artists, 'name'), false, null));
  205 +});
  206 +*/
  207 +
  208 +/*
  209 + * 7digital happy path: 3 track samples
  210 + */

0 comments on commit 4425061

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