From 99baece742fe0c37d8f1921dfc8c99ffb8cd3768 Mon Sep 17 00:00:00 2001 From: Stanford Lin Date: Fri, 29 Jul 2022 15:03:02 +0000 Subject: [PATCH] Added more error handling for title and date --- server/routes/bandsintown.js | 2 + server/routes/index.js | 90 ++++++++++++++++++++++-------------- 2 files changed, 58 insertions(+), 34 deletions(-) diff --git a/server/routes/bandsintown.js b/server/routes/bandsintown.js index d62c603..3691746 100644 --- a/server/routes/bandsintown.js +++ b/server/routes/bandsintown.js @@ -1,3 +1,5 @@ +// TODO: Is this even being used anymore? + var express = require('express'); var router = express.Router(); const https = require('https') diff --git a/server/routes/index.js b/server/routes/index.js index cdeb6dd..d4489df 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -46,13 +46,13 @@ router.get("/login", function (req, res, next) { var scope = "user-read-private user-top-read"; res.redirect( "https://accounts.spotify.com/authorize?" + - new URLSearchParams({ - response_type: "code", - client_id: client_id, - scope: scope, - redirect_uri: redirect_uri, - state: state, - }).toString() + new URLSearchParams({ + response_type: "code", + client_id: client_id, + scope: scope, + redirect_uri: redirect_uri, + state: state, + }).toString() ); }); @@ -67,9 +67,9 @@ router.get("/callback", async function (req, res, next) { if (state === null || state !== storedState) { res.redirect( "/#" + - new URLSearchParams({ - error: "state_mismatch", - }).toString() + new URLSearchParams({ + error: "state_mismatch", + }).toString() ); } else { res.clearCookie(stateKey); @@ -149,8 +149,8 @@ router.get("/callback", async function (req, res, next) { if ( currentSpotifyImports.length > 0 && new Date().getTime() - - currentSpotifyImports[0].timeOfImport.getTime() < - 24 * 60 * 60 * 1000 + currentSpotifyImports[0].timeOfImport.getTime() < + 24 * 60 * 60 * 1000 ) { //console.log("no need to update imports, last import was done less than a day ago") throw new Error( @@ -163,7 +163,7 @@ router.get("/callback", async function (req, res, next) { const artistPromises = []; for (const artist of listOfArtists) { - console.log(artist); + // console.log(artist); artistPromises.push( Artist.findOneAndUpdate( { @@ -225,9 +225,9 @@ router.get("/callback", async function (req, res, next) { } else { res.redirect( "/#" + - new URLSearchParams({ - error: "invalid_token", - }).toString() + new URLSearchParams({ + error: "invalid_token", + }).toString() ); } }); @@ -241,6 +241,8 @@ const getSpotifyTopArtists = async (access_token) => { try { const spotifyResponse = await axios.get( + // TODO: STANFORD LIMIT TO 50 BABYY + // "https://api.spotify.com/v1/me/top/artists?limit=50", "https://api.spotify.com/v1/me/top/artists", { headers: { @@ -286,25 +288,45 @@ const getBandsInTownEvents = async (listOfArtists) => { if (promise.status == "rejected") { failedToFindArtistList.push(listOfArtists[index]); } else if (promise.status == "fulfilled") { - promise.value.forEach((eventValue) => { - const foundEvent = eventsList.find( - (eventInCurrentList) => - eventInCurrentList.title === eventValue.title - ); - if (foundEvent) { - if ( - !foundEvent.lineup.find((element) => { - return ( - element.name.toLowerCase() === - listOfArtists[index].name.toLowerCase() - ); - }) - ) - foundEvent.lineup.push(listOfArtists[index]); + promise.value.forEach((bandsInTownEvent, index2) => { + + // Find any events that are already present based on the event name and datetime + // If it exists, the artist to it's lineup, otherwise create a new event + // Some event "title"'s are blank, so need to error handle for that and replace with "venue.name" + const existingEvent = eventsList.find( + (eventInCurrentList) => { + + const eventInCurrentListTitle = eventInCurrentList.title === "" ? eventInCurrentList.venue.name : eventInCurrentList.title + + const queryEventTitle = bandsInTownEvent.title === "" ? bandsInTownEvent.venue.name : bandsInTownEvent.title + + return eventInCurrentListTitle === queryEventTitle && eventInCurrentList.datetime === bandsInTownEvent.datetime + + }); + + if (typeof existingEvent !== "undefined") { + // For debugging + // console.log("The index is", index) + // console.log("The running total index2", index2) + // console.log("The listOfArtists[index].name", listOfArtists[index].name) + // console.log("The existingEvent.title is", existingEvent.title) + // console.log("The bandsInTownEvent.title", bandsInTownEvent.title) + + // Event that's found has same name and datetime, just add artist to lineup + + const existingLineup = existingEvent.lineup.find((element) => { + return ( + element.name.toLowerCase() === + listOfArtists[index].name.toLowerCase() + ); + }) + + if (typeof existingLineup === "undefined") + existingEvent.lineup.push(listOfArtists[index]); } else { - eventValue.lineup = [listOfArtists[index]]; - delete eventValue.artist; - eventsList.push(eventValue); + bandsInTownEvent.lineup = [listOfArtists[index]]; + delete bandsInTownEvent.artist; + eventsList.push(bandsInTownEvent); } }); }