diff --git a/script/big-brother.js b/script/big-brother.js index f6d3e69..dad7aec 100644 --- a/script/big-brother.js +++ b/script/big-brother.js @@ -13,6 +13,8 @@ var albums = {}; var albumDataStructure = { savedTracks: [], totalTracks: 0, + name: "", + artistsNames: [] }; var accessToken; @@ -31,25 +33,67 @@ async function getOffsetSavedTracks(href=`https://api.spotify.com/v1/me/tracks?o albumData.savedTracks = []; albumData.totalTracks = t.track.album.total_tracks; albums[t.track.album.id] = albumData; + albumData.name = t.track.album.name; + albumData.artistsNames = []; + t.track.album.artists.forEach(artist => albumData.artistsNames.push(artist.name)); } albums[t.track.album.id].savedTracks.push(t.track.id); } }); if(response.data.next) await getOffsetSavedTracks(response.data.next); - } catch (error) { webError("Get user id", error) } + } catch (error) { webError("Get saved tracks", error) } } async function getSavedTracks() { await getOffsetSavedTracks(); - console.log(albums); } //#endregion //#region TRESHOLD ALGORITHM -function tresholdAlgorithm() { - for(let album in albums) - if(albums[album].savedTracks.length >= albums[album].totalTracks * thresholdLove) - console.log(album); +async function checkAlbum(idsString,idsList) { + try { + const response = await axios.get(`https://api.spotify.com/v1/me/albums/contains?ids=${idsString}`, { headers: { 'Authorization': 'Bearer ' + accessToken, } }); + + var idsToSaveString = ""; + for(var i in response.data) { + if(!response.data[i]) { + idsToSaveString = idsToSaveString.concat(idsList[i],','); + + var artistsNames = ""; + albums[idsList[i]].artistsNames.forEach(name => artistsNames = artistsNames.concat(name,' & ')); + artistsNames = artistsNames.substring(0, artistsNames.length - 3); + console.log(`New saved album: ${albums[idsList[i]].name} from ${artistsNames}`); + } + } + + await axios.put(`https://api.spotify.com/v1/me/albums?ids=${idsToSaveString}`, { x: 'x' } , { headers: { 'Authorization': 'Bearer ' + accessToken, } }); + } catch (error) { webError("Check album", error) } +} + +async function tresholdAlgorithm() { + var lovedAlbum = [] + for(let album in albums) { + if(albums[album].savedTracks.length >= albums[album].totalTracks * thresholdLove) { + lovedAlbum.push(album); + } + } + + var idsString = ""; + var idsList = []; + var tmp = 0; + for(var album of lovedAlbum ) { + idsList.push(album); + idsString = idsString.concat(album,','); + tmp = tmp+1; + + if(tmp == 20){ + await checkAlbum(idsString,idsList); + idsString = ""; + idsList = []; + tmp = 0; + } + } + await checkAlbum(idsString,idsList); } //#endregion @@ -78,10 +122,10 @@ async function stepExecution(stepName, stepFunc) { //#region MAIN async function main() { const step1 = "Get liked tracks"; - const step2 = "Apply treshold algortihm"; + const step2 = "Apply treshold algorithm"; try { await stepExecution(step1, getSavedTracks); - stepExecution(step2, tresholdAlgorithm); + await stepExecution(step2, tresholdAlgorithm); } catch (error) { } } //#endregion diff --git a/web/src/app.js b/web/src/app.js index 0944c28..e11f4c5 100644 --- a/web/src/app.js +++ b/web/src/app.js @@ -14,7 +14,7 @@ const port = 3000 const clientId = process.env.CLIENT_ID; const clientSecret = process.env.CLIENT_SECRET; const redirectUri = 'http://localhost:3000/callback'; -const scope = 'user-read-private user-read-email user-library-read'; +const scope = 'user-read-private user-read-email user-library-read user-library-modify'; //#endregion //#region APP INIT