-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
45 lines (36 loc) · 2.5 KB
/
api.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
/* eslint-disable no-param-reassign */
// When starting a front end, it is usual to first separate out the functions which will be implemented by
// a web service. This module demonstrates how such a “mock” service can look. Note how the external interface
// of the final ApiService object matches the one in api.js. That’s because, in reality, this file is actually
// how api.js _starts_, and it morphs into the final api.js when you connect to the web service for real.
// Actual response of APISeeds Lyrics API searching for sample song
// Must be regenerated every hour by running node app.js and pasting the key here
// Not sure if there is a better way to do this without excessive effort/libraries
const spotifyToken = 'BQDTJkbhY7kbF0e1KpRvf9UI9F7xVL3928MOB8lM1xDRr3tYX48nXE6Skg9nRlgg9clKvNLtWHNrRTGmgn0';
const youtubeKey = 'AIzaSyAfMRljYNN6yDhCS7fmhbC46mfw9rQFE7g';
const lyricsKey = 'jZTFcQIDm9bSctMHnFHDW3r0VMWGq55RucTyD4iWiYVip6thmqI1hfiQ1Z7xLxFU';
const searchLyrics = (artist, track) => {
artist = artist.trim().replace(' ', '+');
track = track.trim().replace(' ', '+');
const fetchString = `https://orion.apiseeds.com/api/music/lyric/${artist}/${track}?apikey=${lyricsKey}`;
return fetch(fetchString).then(response => response.json());
};
// Actual response of Youtube API searching for sample song
const youtubeSearch = (artist, track) => {
artist = artist.trim().replace(' ', '+');
track = track.trim().replace(' ', '+');
const fetchString = `https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&order=relevance&q=${artist}${track}&key=${youtubeKey}`;
return fetch(fetchString).then(response => response.json());
};
// Actual response of Spotify API searching for sample song
const spotifySearch = (artist, track) => {
artist = artist.trim().replace(' ', '+');
track = track.trim().replace(' ', '+');
const fetchString = `https://api.spotify.com/v1/search?query=track:${track}+artist:${artist}&type=track&market=US&offset=0&limit=1`;
return fetch(fetchString, { headers: { Authorization: `Bearer ${spotifyToken}` } }).then(response => response.json());
};
const spotifyRecommendations = (artistSeed, trackSeed) => {
const fetchString = `https://api.spotify.com/v1/recommendations?limit=3&market=US&seed_artists=${artistSeed}&seed_tracks=${trackSeed}`;
return fetch(fetchString, { headers: { Authorization: `Bearer ${spotifyToken}` } }).then(response => response.json());
};
export { searchLyrics, youtubeSearch, spotifySearch, spotifyRecommendations };