-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·216 lines (194 loc) · 6.12 KB
/
index.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
211
212
213
214
215
216
'use strict'
const jsonParser = require('xml2json')
const request = require('request')
let keyUrl = 'https://www.shoutcast.com/Developer'
let apiUrl = 'http://api.shoutcast.com'
let tuneUrl = 'http://yp.shoutcast.com'
let self, getUrl, getResponse
class SHOUTcastAPI {
constructor(apiKey, format='json') {
if (typeof apiKey == 'undefined') throw new Error('An API Key is mandatory, please check ' + keyUrl + ' in order to obtain a valid key.')
self = this
this.BITRATE_8 = 8
this.BITRATE_16 = 16
this.BITRATE_32 = 32
this.BITRATE_64 = 64
this.BITRATE_96 = 96
this.BITRATE_128 = 128
this.MEDIATYPE_MP3 = 'audio/mpeg'
this.MEDIATYPE_AAC = 'audio/aacp'
this.apiKey = apiKey
this.format = format
getUrl = (entryPoint) => {
return apiUrl + '/' + entryPoint
}
getResponse = (entryPoint, data, onSuccess=console.log, onError=console.log) => {
data.k = this.apiKey
data.f = this.format
if (data.limit && typeof data.limit.length != 'undefined') data.limit = data.limit.join(',')
// Some entrypoints accepts X,Y where X is the offset and Y is the number of results to be returned
request.get(
{
url: getUrl(entryPoint),
gzip: true,
qs: data
},
function (err, res, body) {
if (!err && res.statusCode == 200) {
if (self.format == 'json' && (body[0] + body[1]) == '<?') {
// some entrypoints returns only XML response so we need to convert them
body = jsonParser.toJson(body)
}
let json = JSON.parse(body)
json = typeof json.response != 'undefined' ? json.response.data : json
onSuccess(json)
} else {
onError(err)
}
}
)
}
}
getTop500Stations(opts) {
getResponse(
'legacy/Top500',
{
limit: opts.limit || 10,
br: opts.bitRate || this.BITRATE_64,
mt: opts.mediaType || this.MEDIATYPE_MP3
},
opts.success,
opts.error
)
}
getStationsByKeyword(opts) {
if (typeof opts.search == 'undefined') throw new Error('Search parameter is mandatory.')
getResponse(
'legacy/stationsearch',
{
search: opts.search,
limit: opts.limit || [0,10],
br: opts.bitRate || this.BITRATE_64,
mt: opts.mediaType || this.MEDIATYPE_MP3
},
opts.success,
opts.error
)
}
getStationsByGenre(opts) {
if (typeof opts.genre == 'undefined') throw new Error('Genre parameter is mandatory.')
getResponse(
'legacy/genresearch',
{
genre: opts.genre,
limit: opts.limit || [0,10],
br: opts.bitRate || this.BITRATE_64,
mt: opts.mediaType || this.MEDIATYPE_MP3
},
opts.success,
opts.error
)
}
getStationsByNowPlaying(opts) {
if (typeof opts.query == 'undefined') throw new Error('Query parameter is mandatory.')
else if (typeof opts.query.length != 'undefined') opts.query = opts.query.join('||')
getResponse(
'station/nowplaying',
{
ct: opts.query,
genre: opts.genre || undefined,
limit: opts.limit || 10,
br: opts.bitRate || this.BITRATE_64,
mt: opts.mediaType || this.MEDIATYPE_MP3
},
opts.success,
opts.error
)
}
getStationsByAdvancedSearch(opts) {
if (typeof opts.bitRate == 'undefined') throw new Error('BitRate parameter is mandatory.')
if (typeof opts.mediaType == 'undefined') throw new Error('MediaType parameter is mandatory.')
if (typeof opts.genreId == 'undefined') throw new Error('Genre ID parameter is mandatory.')
getResponse(
'station/advancedsearch',
{
genreId: opts.genreId,
genre: opts.genre || undefined,
limit: opts.limit || 10,
br: opts.bitRate,
mt: opts.mediaType
},
opts.success,
opts.error
)
}
getRandomStations(opts) {
getResponse(
'station/randomstations',
{
genre: opts.genre,
limit: opts.limit || 10,
br: opts.bitRate || this.BITRATE_64,
mt: opts.mediaType || this.MEDIATYPE_MP3
},
opts.success,
opts.error
)
}
getGenres(opts) {
getResponse(
'legacy/genrelist',
{},
opts.success,
opts.error
)
}
getPrimaryGenres(opts) {
getResponse(
'genre/primary',
{},
opts.success,
opts.error
)
}
_getSecondaryGenres(opts) {
let data = {}
if (typeof opts.parentId != 'undefined') {
data = {
parentid: opts.parentId
}
} else if (typeof opts.genreId != 'undefined') {
data = {
id: opts.genreId
}
} else if (typeof opts.hasChildren != 'undefined') {
data = {
haschildren: String(opts.hasChildren)
}
} else {
throw new Error('Some mandatory parameter is missing.')
}
getResponse(
'genre/secondary',
data,
opts.success,
opts.error
)
}
getGenreByParentId(opts) {
if (typeof opts.parentId == 'undefined') throw new Error('Parent ID parameter is mandatory.')
this._getSecondaryGenres(opts)
}
getGenreById(opts) {
if (typeof opts.genreId == 'undefined') throw new Error('Genre ID parameter is mandatory.')
this._getSecondaryGenres(opts)
}
getGenresByChildrenFlag(opts) {
if(typeof opts.hasChildren == 'undefined') opts.hasChildren = true
this._getSecondaryGenres(opts)
}
getPlaylistUrlByStationId(base, stationId) {
return tuneUrl + base + '?id=' + stationId
}
}
module.exports = SHOUTcastAPI