-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.coffee
55 lines (40 loc) · 1.59 KB
/
app.coffee
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
fs = require 'fs'
http = require 'http'
cheerio = require 'cheerio'
express = require 'express'
request = require 'request'
bodyParser = require 'body-parser'
app = express()
app.set 'port', process.env.PORT or 8080
app.use bodyParser.json()
app.use bodyParser.urlencoded({extended: true})
app.use '/public', express.static(__dirname + '/public')
$ = cheerio.load fs.readFileSync(__dirname + '/index.html')
app.get '/', (req, res) ->
res.send $.html()
app.post '/', (req, res) ->
query = req.body.query
url = 'https://api.spotify.com/v1/search?type=track&q=' + query.split(' ').join('+')
request.get url, (error, response, body) ->
if error
console.log 'Error: ', error
return res.send $.html()
if response.statusCode isnt 200
console.log 'Invalid status code: ', response.statusCode
return res.send $.html()
data = JSON.parse body
if data.tracks.total is 0
console.log 'Track not found!'
return res.send $.html()
artist = data.tracks.items[0].artists[0].name
track = data.tracks.items[0].name
previewUrl = data.tracks.items[0].preview_url
albumCover = data.tracks.items[0].album.images[1].url
$('#artist').text artist
$('#track').text track
$('#preview').text previewUrl
$('#album-cover').attr 'src', albumCover
$('#result').removeClass('hidden')
res.send $.html()
server = app.listen app.get('port'), ->
console.log "Started server, listening on port #{app.get('port')}"