This repository was archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
243 lines (219 loc) · 7.24 KB
/
index.js
File metadata and controls
243 lines (219 loc) · 7.24 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const querystring = require('querystring')
const request = require('request')
const hostname = 'http://api.dirble.com/v2'
module.exports = function (apiKey) {
if (!isValidApiKey(apiKey)) {
return
}
/*
Attempts to validate the provided api key and throws an exception if it is invalid.
@param {string} the api key to validate
*/
function isValidApiKey (apiKey) {
if (!(typeof apiKey === 'string')) {
throw new Error(`You must supply a valid API key as a string. Provided type: ${typeof apiKey}`)
}
if (!apiKey) {
throw new Error('You must supply a valid API key!')
}
return true
}
/*
Makes a request to the dirble api at the given path using the given parameters
@param {string} path The path the request will be sent to
@param {object} params Any query parameters that should be sent along with the request
*/
function makeRequest (path, params) {
// The api key will always be sent as a query parameter
params = params || {}
params.token = apiKey
return new Promise(function (resolve, reject) {
request(`${hostname}${path}?${querystring.stringify(params)}`,
function (error, response, body) {
if (error) {
reject(error)
}
try {
var result = JSON.parse(body)
if (response.statusCode === 200) {
resolve(result)
}
reject(result.error)
} catch (error) {
reject(response.statusMessage)
}
})
})
}
var dirble = {}
/*
Returns a list of radio stations
@param {number} [page = 0] show which per_page stations to show
@param {number} [perPage = 20] How many stations per page to show
@param {number} [offset = 0]
*/
dirble.getStations = function (page, perPage, offset) {
return makeRequest('/stations', { page: page, per_page: perPage, offset: offset })
}
/*
Returns a list of recently added radio stations
@param {number} [page = 0] show which per_page stations to show
@param {number} [perPage = 20] How many stations per page to show
@param {number} [offset = 0]
*/
dirble.getRecentlyAddedStations = function (page, perPage, offset) {
return makeRequest('/stations/recent', { page: page, per_page: perPage, offset: offset })
}
/*
Returns a list of popular radio stations
@param {number} [page = 0] show which per_page stations to show
@param {number} [perPage = 20] How many stations per page to show
@param {number} [offset = 0]
*/
dirble.getPopularStations = function (page, perPage, offset) {
return makeRequest('/stations/popular', { page: page, per_page: perPage, offset: offset })
}
/*
Returns a radio station with the given id
@param {number} [id] Id of a radio station
*/
dirble.getStation = function (id) {
if (!id || id < 0) {
return new Promise(function (resolve, reject) {
reject('You must supply a valid id')
})
}
return makeRequest(`/station/${id}`)
}
/*
Returns a list of radio stations that are similar to the radio station with the given id
@param {number} [id] Id of a radio station
*/
dirble.getSimilarStations = function (id) {
if (!id || id < 0) {
return new Promise(function (resolve, reject) {
reject('You must supply a valid id')
})
}
return makeRequest(`/station/${id}/similar`)
}
/*
Returns a list of recently played songs from a radio station with the given id
@param {number} [id] Id of a radio station
*/
dirble.getSongHistory = function (id) {
if (!id || id < 0) {
return new Promise(function (resolve, reject) {
reject('You must supply a valid id')
})
}
return makeRequest(`/station/${id}/song_history`)
}
/*
Returns a list of recently played songs from all stations
*/
dirble.getSongHistory = function () {
return makeRequest('/songs')
}
/*
Returns a list of all categories
*/
dirble.getCategories = function () {
return makeRequest('/categories')
}
/*
Returns a list of all primary categories
*/
dirble.getPrimaryCategories = function () {
return makeRequest('/categories/primary')
}
/*
Returns a list of all child categories for a given category
NOTE: Dirble does not seem to respect page, per_page or offset query parameters
@param {number} id The Id of a category
@param {number} [page = 0] show which per_page stations to show
@param {number} [perPage = 20] How many stations per page to show
@param {number} [offset = 0]
*/
dirble.getChildCategories = function (id, page, perPage, offset) {
if (!id || id < 0) {
return new Promise(function (resolve, reject) {
reject('You must supply a valid id')
})
}
return makeRequest(`/category/${id}/childs`, { page: page, per_page: perPage, offset: offset })
}
/*
Returns all the categories as a tree structure
*/
dirble.getCategoryTree = function () {
return makeRequest('/categories/tree')
}
/*
Returns a list of radio stations for a given category
@param {number} id The id of a category
@param {number} [page = 0] show which per_page stations to show
@param {number} [perPage = 20] How many stations per page to show
@param {number} [offset = 0]
*/
dirble.getStationsWithCategory = function (id, page, perPage, offset) {
if (!id || id < 0) {
return new Promise(function (resolve, reject) {
reject('You must supply a valid id')
})
}
return makeRequest(`/category/${id}/stations`, { page: page, per_page: perPage, offset: offset })
}
/*
Returns a list of radio stations based on a search query
@param {string} query A string to search for
*/
dirble.search = function (query) {
if (!query) {
return new Promise(function (resolve, reject) {
reject('You must supply a valid query')
})
}
return makeRequest(`/search/${query}`)
}
/*
Returns a list of all countries
*/
dirble.getCountries = function () {
return makeRequest('/countries')
}
/*
Returns a list of all continents
*/
dirble.getContinents = function () {
return makeRequest('/continents')
}
/*
Returns a list of countries for a given continent
@param {number} id The id of a continent
*/
dirble.getCountriesInContinent = function (id) {
if (!id || id < 0) {
return new Promise(function (resolve, reject) {
reject('You must supply a valid id')
})
}
return makeRequest(`/continents/${id}/countries`)
}
/*
Returns a list of radio stations for a given country
@param {string} countryCode The code for a country, eg. 'US'
@param {number} [page = 0] show which per_page stations to show
@param {number} [perPage = 20] How many stations per page to show
@param {number} [offset = 0]
*/
dirble.getStationsInCountry = function (countryCode, page, perPage, offset) {
if (!countryCode) {
return new Promise(function (resolve, reject) {
reject('You must supply a valid country code')
})
}
return makeRequest(`/countries/${countryCode}/stations`, { page: page, per_page: perPage, offset: offset })
}
return dirble
}