-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
215 lines (141 loc) · 3.74 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
'use strict';
( ( root, cx ) => {
if ( typeof define === 'function' && define.amd ) {
// AMD
define( ['cross-fetch'], cx )
} else if ( typeof exports === 'object' ) {
// Node, CommonJS-like
module.exports = cx( require( 'cross-fetch' ) )
} else {
// Browser globals (root is window)
root.albumArt = cx( root.fetch )
}
} )( this, fetch => {
const albumArt = async ( artist, options, cb ) => {
// Massage inputs
if ( typeof artist !== 'string' ) {
throw new TypeError( 'Expected search query to be a string' )
}
if ( typeof options === 'function' ) {
cb = options
options = null
}
if ( typeof cb !== 'function' ) {
cb = null
}
// Default options
let query = artist.replace( '&', 'and' )
const opts = Object.assign( {
album: null,
size: null
}, options )
// Image size options
const SIZES = {
SMALL: 'small',
MEDIUM: 'medium',
LARGE: 'large'
}
// Public Key on purpose - don't make me regret this
const apiEndpoint = 'https://api.spotify.com/v1'
const authEndpoint = 'https://accounts.spotify.com/api/token'
const clientId = '3f974573800a4ff5b325de9795b8e603'
const clientSecret = 'ff188d2860ff44baa57acc79c121a3b9'
let method = 'artist'
if ( opts.album !== null ) {
method = 'album'
query += ` ${opts.album}` // add space + album name
}
// Create a query like "<artist> <album>" and escape it
const queryParams = `?q=${encodeURIComponent( query )}&type=${method}&limit=1`
// Create request URL
const searchUrl = `${apiEndpoint}/search${queryParams}`
const authString = `${clientId}:${clientSecret}`
let authorization
if ( typeof btoa !== 'undefined' ) {
authorization = btoa( authString )
} else if ( Buffer ) {
authorization = Buffer.from( authString ).toString( 'base64' )
} else {
throw new Error( 'No suitable environment found' )
}
// Start by authorizing a session
let error = null
const authToken = await fetch( authEndpoint, {
method: 'post',
body: 'grant_type=client_credentials',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Basic ${authorization}`
}
} )
.then(
res => res.json()
)
.then(
json => json.access_token
)
.catch(
err => {
error = err
}
)
// Perform image search
const response = !error && await fetch( searchUrl, {
method: 'get',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Bearer ${authToken}`
}
} )
.then(
res => res.json()
)
.then(
json => {
if ( typeof ( json.error ) !== 'undefined' ) {
// Error
return Promise.reject( new Error( `JSON - ${json.error} ${json.message}` ) )
}
if ( !json[method + 's'] || json[method + 's'].items.length === 0 ) {
// Error
return Promise.reject( new Error( 'No results found' ) )
}
// Select image size
const images = json[method + 's'].items[0].images
let smallest = images[0]
let largest = images[0]
for ( const element of images ) {
if ( parseInt( element.width ) < parseInt( smallest.width ) ) {
smallest = element
}
if ( parseInt( element.width ) > parseInt( largest.width ) ) {
largest = element
}
}
if ( opts.size === SIZES.SMALL ) {
return smallest.url
}
if ( opts.size === SIZES.MEDIUM && images.length > 1 ) {
return images[1].url
}
// Large by default
return largest.url
}
)
.catch( err => {
error = err
} )
// Callback
if ( cb ) {
return cb( error, response )
}
// Non-callback, throw errors
if ( error ) {
throw error
}
// Promise
return response
}
// Exposed public method
return albumArt
} )