-
Notifications
You must be signed in to change notification settings - Fork 2
/
app (Harald Kirschner's conflicted copy 2012-05-05).js
executable file
·212 lines (144 loc) · 3.91 KB
/
app (Harald Kirschner's conflicted copy 2012-05-05).js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var async = require('async');
var express = require('express');
var util = require('util');
var jade = require('jade');
var stylus = require('stylus');
var nib = require('nib');
var sdigital = require('7digital-api');
// Redis
// if (process.env.REDISTOGO_URL) {
// rtg = require('url').parse(process.env.REDISTOGO_URL);
// redis = require('redis').createClient(rtg.port, rtg.hostname);
// redis.auth(rtg.auth.split(':')[1]);
// } else {
// redis = require('redis').createClient();
// }
// MongoDB
// mongoose = require('mongoose');
// mongoose.connect(process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://localhost/app');
// 7digital
sdigital.configure({
oauthkey: process.env.SDIGITAL_KEY,
oauthsecret: process.env.SDIGITAL_SECRET
});
var tracks = new sdigital.Tracks();
// Configure express
app = module.exports = express.createServer();
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
// app.use(express.session({ secret: process.env.SESSION_SECRET || 'secret123' }));
// Stylus
app.use(stylus.middleware({
src: __dirname + '/views',
dest: __dirname + '/public',
compile: function(str, path) {
var compiler;
compiler = stylus(str);
compiler.set('filename', path);
compiler.set('compress', false);
compiler.use(nib());
return compiler;
}
}, app.helpers({
_: require('underscore')
})));
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function() {
app.set('view options', {
pretty: true
});
app.use(express.static(__dirname + '/public', {
maxAge: 31557600000
}));
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
app.configure('production', function() {
return app.use(express.errorHandler());
});
// In memory cache for fast search
var cache = {
search: {}
};
// listen to the PORT given to us in the environment
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log('Listening on %d', port);
});
// Components
app.get('/', function index(req, res) {
res.render('index.jade', {
title: 'Mixtape',
env: {
FB_ID: process.env.FB_ID
}
});
});
app.get('/play/:id', function play(req, res) {
tracks.getPreview({trackId: req.params.id}, function(err, data) {
var url = data.url || null;
res.json({url: url});
});
});
app.get('/search/:q', function search(req, res) {
query = (req.params.q || '').trim();
if (!query.length) {
res.json([]);
return;
}
var cachehit = cache.search[query] || null;
if (cachehit) {
res.json(cachehit);
return
}
// 7digital track search API
tracks.search({q: query}, function(err, data) {
data = data.searchResults.searchResult || []; // JSON, XML style
data = data.map(function(track) {
track = track.track;
// Nice duration
var duration = track.duration;
var seconds = String(duration % 60);
while (seconds.length < 2) {
seconds += '0'
}
duration = Math.round(duration / 60) + ':' + seconds
return {
id: track.id,
artist: track.artist.name,
title: track.title,
duration: duration,
url: track.url,
image: track.release.image || null
};
});
cache.search[query] = data;
res.json(data);
});
// nest.song.search({
// combined: query,
// sort: 'song_hotttnesss-desc' // for better results, hits first
// }, function(err, data) {
// data = data.songs || [];
// data = data.map(function(song) {
// return {
// id: song.id,
// artist: song.artist_name,
// title: song.title
// };
// });
// cache.search[query] = data
// res.json(data);
// });
});
app.get('/channel', function(req, res) {
res.setHeader('Cache-Control', 'public, max-age=' + (31557600000 / 1000)); // 1y
res.send('<script src="//connect.facebook.net/en_US/all.js"></script>');
});