-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
165 lines (124 loc) · 3.92 KB
/
index.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
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
express = require 'express'
fibrous = require 'use-global-fibrous'
request = require 'request'
{
pluck
sortBy
} = require 'lodash-node'
market = 'US'
_apiUrl = (method, params) ->
"""The URL for an API call"""
host = "api.spotify.com"
version = "v1"
paramList = []
for key, val of params
paramList.push "#{ encodeURIComponent key }=#{ encodeURIComponent val }"
"""https://#{ host }/#{ version }/#{ method }?#{ paramList.join '&' }"""
apiCall = fibrous (method, params) ->
"""Calls Spotify's API"""
url = _apiUrl method, params
console.log "Spotify API call: ", url
response = request.sync url
try
JSON.parse response.body
catch
null
artistId = fibrous (artistName) ->
"""Gets a Spotify artistId from a search query for artist name"""
# https://api.spotify.com/v1/search?q=Ryan+Adams&type=artist&market=US&limit=1&offset=0
result = apiCall.sync 'search',
q: artistName
type: 'artist'
market: market
limit: 1
offset: 0
result?.artists?.items?[0]?.id
allAlbumIds = fibrous (artistId) ->
"""Gets all the albums on Spotify of a given artist"""
# https://api.spotify.com/v1/artists/2qc41rNTtdLK0tV3mJn2Pm/albums?market=US&limit=50&offset=0
result = apiCall.sync "artists/#{ artistId }/albums",
market: market
limit: 50
offset: 0
albums = result?.items
if albums?
(a.id for a in albums)
else
[]
segment = (list, step) ->
"""Segments a list of things into a series of lists of length at most `step`"""
(list[offset...offset + step] for offset in (i for i in [0...list.length] by step))
allTracksFromAlbumIds = fibrous (albumIds) ->
"""Gets all the track ids from a bunch of albumIds"""
segments = segment albumIds, 20
futures = []
for s in segments
# https://api.spotify.com/v1/albums?ids=6JNlf8swWaLPW6PpQA9ghW,5FV8d3DhSoArvwr0Qqgzq3,6R2ec7b25hSZrF19oj3yRG
futures.push apiCall.future 'albums',
ids: s.join ','
trackIds = (x.id for x in Array::concat (t for t in Array::concat(((a?.tracks?.items for a in b?.albums) for b in fibrous.wait futures)...))...)
segments = segment trackIds, 50
futures = []
for s in segments
futures.push apiCall.future 'tracks',
ids: s.join ','
tracks = ({
id: t.id
name: t.name
href: t.href
uri: t.uri
popularity: t.popularity
} for t in Array::concat (x?.tracks for x in fibrous.wait futures)...)
popularTracks = sortBy tracks, 'popularity'
popularTracks.reverse()
popularTracksByArtist = fibrous (artistName) ->
"""Given an artist name, list all their tracks"""
_artistId = artistId.sync artistName
albumIds = allAlbumIds.sync _artistId
allTracksFromAlbumIds.sync albumIds
_escape = (text) ->
"""HTML escape"""
text.toString().replace(/&/g,'&' ).replace(/</g,'<').
replace(/"/g,'"').replace(/'/g,''')
htmlForTracks = (tracks) ->
"""Returns HTML for a bunch of tracks"""
fields = ['id', 'popularity', 'name', 'uri']
html = "<style>BODY { font-family: Helvetica; font-size: 10pt; }</style><table><tr><th>#</th>"
for f in fields
html += "<th>#{ f }</th>"
html += "</tr>"
n = 0
for t in tracks
html += "<tr>"
n++
html += "<td>#{ n }</td>"
for f in fields
x = t[f]
if ':' in x
val = """<a href="#{ x }">#{ x }</a>"""
else
val = _escape x
html += "<td>#{ val }</td>"
html += "</tr>"
html += "</table>"
if require.main is module
app = express()
app.get '/', (req, res) ->
res.send "Hello world!"
app.get "/tracks/:artistName", (req, res) ->
fibrous.run ->
tracks = popularTracksByArtist.sync req.params.artistName
res.send htmlForTracks tracks
server = app.listen 3000, ->
host = server.address().address
port = server.address().port
console.log "Top tracks app listening at http://%s:%s", host, port
module.exports = {
htmlForTracks
popularTracksByArtist
allTracksFromAlbumIds
allAlbumIds
artistId
apiCall
_apiUrl
}