-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
110 lines (94 loc) · 3.11 KB
/
server.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
var restify = require('restify')
, server = restify.createServer()
, path = require('path')
, filed = require('filed')
, async = require('async')
, PORT = process.env.PORT || 8081
, bing = require('./bing.js')
, flickr = require('./flickr.js')
, lyrics = require('./lyrics.js')
, STATIC_PATH = path.join(process.cwd(), 'static')
//console.log('cwd: %s', process.cwd())
//console.log('__dirname: %s', __dirname)
console.log('static path: %s', STATIC_PATH)
server.get('static/:filename', function (req, res) {
var filename = req.params.filename
, filePath = path.join(STATIC_PATH, filename)
//if (filename.slice(0, STATIC_PATH.length) !== filename) { //basically, filepath contained ../
// res.statusCode = 403
// res.end('hmmmmmmmmmmmmmmmmmmm')
// return
//}
console.log('serving file: ' + filePath)
filed(filePath).pipe(res)
})
server.get('/', function(req, res){
filed(path.join(process.cwd(), '/static/index.html')).pipe(res)
})
server.get('/flickr/:query', function(req, res) {
flickr(req.params.query, function(e, pics) {
if (e) return res.end(e)
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({images : pics}))
})
})
server.get('/images/:artist/:mood/:track', function (req, res) {
var artist = req.params.artist
, mood = req.params.mood
, track = req.params.track
console.log('getting images for artist: %s \tmood: %s \ttrack: %s', artist, mood, track)
function tagCallbackWithType (type, cb) {
return function(err, results) {
if (err) return cb(err)
results.forEach(function(itm) {
itm.__webType = type
})
cb(null, results)
}
}
async.parallel({
bingArtist: function(cb) {
bing(artist, tagCallbackWithType('image-artist-'+artist, cb))
},
bingTrack: function(cb) {
bing(track, tagCallbackWithType('image-track-'+track, cb))
},
bingMood: function(cb) {
bing(mood, tagCallbackWithType('image-mood-' + mood, cb))
},
flickrArtist : function (cb) {
flickr(artist, tagCallbackWithType('image-artist-'+artist, cb))
}
}, function(err, allResults) {
if (err) return res.end(err)
var artistArray = allResults.bingArtist
, moodArray = allResults.bingMood
, trackArray = allResults.bingTrack
, flAristArray = allResults.flickrArtist
, randomImages = []
, i = 0
while (artistArray.length > 0 || moodArray.length > 0 || trackArray.length > 0 || flAristArray.length > 0) {
if (artistArray.length > 0) {
randomImages.push(artistArray.shift())
}
if (moodArray.length > 0) {
var url = moodArray.shift()
if(i < 1 || (i % 5) === 0) {
randomImages.push(url)
}
}
if (trackArray.length > 0) {
randomImages.push(trackArray.shift())
}
if (flAristArray.length > 0) {
randomImages.push(flAristArray.shift())
}
i++;
}
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({images : randomImages}))
})
})
server.listen(PORT, function() {
console.log('%s listening at %s', server.name, server.url);
})