Skip to content

Instantly share code, notes, and snippets.

View gate5th's full-sized avatar

Michael Bruns gate5th

View GitHub Profile
@gate5th
gate5th / spotifyFunctions.js
Created September 12, 2018 18:59
oldschoolshuffle
function dynamicSort(property) {
var sortOrder = 1;
if(property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a,b) {
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
@gate5th
gate5th / NA
Created September 12, 2018 18:57
oldschoolshuffle
//Start out with a playlist with songs from multiple albums in no particular order
[{albumId: A, trackname: "Dream Maker", trackNumber: 3},
{albumId: C, trackname: "Mess", trackNumber: 1},
{albumId: B, trackname: "Dont", trackNumber: 9},
{albumId: W, trackname: "Me", trackNumber: 5},
{albumId: A, trackname: "Heartbreaker", trackNumber: 2},
{albumId: W, trackname: "With", trackNumber: 2},
{albumId: C, trackname: "Around", trackNumber: 6},
{albumId: A, trackname: "Love Taker", trackNumber: 6}]
@gate5th
gate5th / spotifyFunctions.js
Created September 12, 2018 18:56
oldschoolshuffle
function convertPlaylistToObjectByProperty(simplifiedPlaylist, propertyName){
//use to group large array of tracks into smaller arrays of tracks from same album
//this lets us easily sort tracks for a single album, then do a for..in loop
//over each albumId as an object key and combine the arrays back together
let playlistObject = {};
//eslint-disable-next-line
simplifiedPlaylist.map((track) => {
if (!playlistObject[track[propertyName]]) {
playlistObject[track[propertyName]] = [];
}
@gate5th
gate5th / spotifyFunctions.js
Created September 12, 2018 18:55
oldschoolshuffle
async function identifyAlbumsByArtistId(artistId, returnSimpleArray=true) {
//returns an array of albumIds that appear in the playlist, without duplicates
const arrayOfAlbumObjects = await spotifyApi.getArtistAlbums(artistId)
let albumIds = arrayOfAlbumObjects.items.map((albumObj) => {
if (returnSimpleArray) {
return albumObj.id
} else {
return ({albumId: albumObj.id, albumName: albumObj.name, albumUri: albumObj.uri})
}
})
@gate5th
gate5th / spotifyFunctions.js
Created September 12, 2018 18:54
oldschoolshuffle
function shuffleArray(input) {
for (let i = input.length-1; i >=0; i--) {
const randomIndex = Math.floor(Math.random()*(i+1));
const itemAtIndex = input[randomIndex];
input[randomIndex] = input[i];
input[i] = itemAtIndex;
}
return input;
}
@gate5th
gate5th / spotifyFunctions.js
Created September 12, 2018 18:52
oldschoolshuffle
function identifyAlbumsInPlaylist(simplifiedPlaylist, returnSimpleArray=true) {
//returns an array of albumIds that appear in the playlist, without duplicates
let albumIds = simplifiedPlaylist.map((track) => {
if (returnSimpleArray) {
return track.albumId
} else {
return ({albumId: track.albumId, albumName: track.albumName, albumUri: track.albumUri})
}
})
albumIds = uniq(albumIds);
@gate5th
gate5th / spotifyFunctions.js
Created September 12, 2018 18:51
oldschoolshuffle
async function getSimpleAlbumTracks(albumId, albumName, albumUri){
//track_number is what track number a song is on the album
try {
const tracks = await spotifyApi.getAlbumTracks(albumId);
const simpleTracks = tracks.items.map((track) => {
const artist = track.artists[0];
return {
trackId: track.id,
trackName: track.name,
trackUri: track.uri,
@gate5th
gate5th / spotifyFunctions.js
Created September 12, 2018 18:49
oldschoolshuffle
async function getSimplePlaylistTracks(playlistId){
//track_number is what track number a song is on the album
try {
const tracks = await spotifyApi.getPlaylistTracks(playlistId);
//getPlaylistTracks has a bunch of meta data about the playlist we don't need
//once again items is the property we really want. It's an array of tracks
const simpleTracks = tracks.items.map((trackObject) => {
const track = trackObject.track;
const album = trackObject.track.album;
const artist = trackObject.track.artists[0]
@gate5th
gate5th / spotifyFunctions.js
Created September 12, 2018 18:47
oldschoolshuffle
export async function getUserPlaylists() {
//returns an array of objects with playlist name (like "Favorite Smashing Pumpkins jamz")
//and the id of the playlist. Use this to feed the playlists selection list
try {
const playlistsResponse = await spotifyApi.getUserPlaylists();
//playlistsResponse.items are the actual playlist objects
const playlists = playlistsResponse.items.map((playlistObject) => {
const {id, name} = playlistObject;
return {id: id, playlistName: name}
})
@gate5th
gate5th / spotifyFunctions.js
Created September 12, 2018 18:45
oldschoolshuffle
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);
}