Last active
September 17, 2018 01:57
-
-
Save gate5th/fbdea426d3ef1e8981840fbc1cc4b9fa to your computer and use it in GitHub Desktop.
oldschoolshuffle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//spotifyFunctions.js | |
//waiting for the API to be fixed so can't use spotify-web-api-js library | |
//for playlist stuff. Creating this global variable to hold the accessToken | |
//and use it manually for our temporary playlist function. Once the JMPerez library | |
//is fixed then can go back to just using it. | |
//Playlist API issues: https://developer.spotify.com/community/news/2018/06/12/changes-to-playlist-uris/ | |
let globalAccessToken = ""; | |
//have to update out setAccessToken function to also update the globalAccessToken so we can use | |
//it in tempCreatePlaylist | |
export function setAccessToken(accessToken) { | |
//since using spotifyApi as helper library you can set the access code once | |
//you get it and then not have to include it in every request | |
spotifyApi.setAccessToken(accessToken); | |
globalAccessToken = accessToken; | |
} | |
//[Most of the rest of this file goes here, but omiting from the gist to save space | |
//In our createPlaylist function that we build earlier | |
async function createPlaylist(simplifiedTrackArray, playlistName, addRelatedDiscography) { | |
//have to get userId, create a playlist in spotify with the name, and then add the tracks to it | |
//options is whether to addDiscography | |
//Note that Spotify is very picky about what counts as an 'active device' so likely don't have permission to press | |
//play. Also only works if the user has premium. Also spotify will only let you add 100 tracks | |
//per addTracksToPlaylist request, so need to split the trackUris up if more than 100 tracks | |
const maxTracksToAddInEachRequest = 100; | |
const userInfoResponse = await spotifyApi.getMe(); | |
const name = `${playlistName} - Album Shuffled - ${ | |
addRelatedDiscography === "true" ? "with related discography" : "" | |
}`; | |
const description = | |
"Made with the oldschoolshuffle app to enable shuffling by album, the way music was meant to be listened to"; | |
//es6 destructuring and renaming | |
const { id: userId } = userInfoResponse; | |
const playlistOptions = { name: name, description: description }; | |
//HERE IS WHERE WE DROP IN OUR NEW TEMPORARY CREATE PLAYLIST FUNCTION CALL | |
const newPlaylistResponse = await tempCreatePlaylist(userId, playlistOptions); | |
//const newPlaylistResponse = await spotifyApi.createPlaylist(userId, playlistOptions); | |
const trackUris = simplifiedTrackArray.map((trackObject) => { | |
return trackObject.trackUri; | |
}); | |
try { | |
if (trackUris.length < maxTracksToAddInEachRequest) { | |
return await spotifyApi.addTracksToPlaylist( | |
newPlaylistResponse.id, | |
trackUris | |
); | |
} else { | |
const chunkedTrackUris = chunk( | |
trackUris, | |
maxTracksToAddInEachRequest | |
); | |
const promisesOfChunkedUris = chunkedTrackUris.map( | |
async (chunkOfTrackUris) => { | |
//eslint-disable-next-line | |
const addTracksToPlaylistResponse = await spotifyApi.addTracksToPlaylist( | |
newPlaylistResponse.id, | |
chunkOfTrackUris | |
); | |
} | |
); | |
await Promise.all(promisesOfChunkedUris); | |
return await spotifyApi.play({ | |
context_uri: newPlaylistResponse.uri | |
}); | |
} | |
} catch (err) { | |
console.log( | |
"Oops - no spotify player is active so just made a playlist" | |
); | |
} | |
} | |
//HERE's THE NEW TEMPORARY FUNCTION | |
async function tempCreatePlaylist(userId, playlistOptions) { | |
//temp function while wait for spotify-web-api-js to get updated | |
// old API endpoint | |
//const url = `https://api.spotify.com/v1/users/${userId}/playlists`; | |
//new API endpoint | |
const url = `https://api.spotify.com/v1/me/playlists`; | |
const payload = { | |
name: playlistOptions.name, | |
description: playlistOptions.description | |
}; | |
try { | |
const response = await fetch(url, { | |
method: "POST", | |
body: JSON.stringify(payload), | |
headers: { | |
"Content-Type": "application/json", | |
Authorization: "Bearer " + globalAccessToken | |
} | |
}); | |
return response.json(); | |
} catch (err) { | |
console.error("Error: Issue in tempCreatePlaylist"); | |
console.error(err); | |
console.error(err.stack); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment