Skip to content

Commit

Permalink
Get a working spotify history spelunker
Browse files Browse the repository at this point in the history
  • Loading branch information
Whoaa512 committed Nov 18, 2019
1 parent 46de0f0 commit 510a9e5
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
coverage/
.access_token
86 changes: 86 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
global.XMLHttpRequest = require("node-http-xhr");
global.window = global;
var Spotify = require("spotify-web-api-js");
var moment = require("moment");
var fs = require("fs");
var chunk = require("lodash/chunk");
var spotifyApi = new Spotify();

assert(fs.existsSync(".access_token"), "Missing `.access_token` file");

spotifyApi.setAccessToken(fs.readFileSync("./.access_token", "utf8"));

async function getLastSongs({ limit = 50 } = {}) {
let result = [];
let before = Date.now();
while (result.length < limit) {
console.log("result.length", result.length);
const { items, cursors } = await spotifyApi.getMyRecentlyPlayedTracks({
limit: 50,
before: before
});
console.log("items.length", items.length);
if (items.length > 0) {
result = result.concat(items);
}
if (cursors && cursors.before) {
before = cursors.before;
} else {
console.log("out of cursors");
break;
}
}
return result;
}

async function getLastSongsByHours({ hours = 24 } = {}) {
const limit = Infinity;
let result = [];
let after = moment()
.subtract(hours, "hours")
.valueOf();

while (result.length < limit) {
console.log("result.length", result.length);
const { items, cursors } = await spotifyApi.getMyRecentlyPlayedTracks({
limit: 50,
after
});
console.log("items.length", items.length);
if (items.length > 0) {
result = result.concat(items);
}
if (cursors && cursors.after) {
after = cursors.after;
} else {
console.log("out of cursors");
break;
}
}
return result;
}

async function addSongsToPlaylist(playlistId, trackUris) {
const responses = [];
for (const tracks of chunk(trackUris, 100)) {
responses.push(await spotifyApi.addTracksToPlaylist(playlistId, tracks));
}
return responses;
}

(async () => {
// const result = await getLastSongs({ limit: 100 })
try {
const result = await getLastSongsByHours({ hours: 2 * 24 });
const trackUris = result.map(({ track }) => track.uri);
const { id: playlistId } = await spotifyApi.createPlaylist({
name: "my-cool-playlist"
});
const next = await addSongsToPlaylist(playlistId, trackUris);
// console.log('result', result);
console.log("next", next);
// console.log('result', result.length);
} catch (error) {
console.error(error.response || error);
}
})();
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,14 @@
"version": "1.0.0",
"main": "index.js",
"author": "C.J. Winslow <whoaa512@gmail.com> (https://github.com/whoaa512)",
"license": "MIT"
"license": "MIT",
"dependencies": {
"lodash": "^4.17.15",
"moment": "^2.24.0",
"node-http-xhr": "^1.3.4",
"spotify-web-api-js": "^1.2.0"
},
"devDependencies": {
"prettier": "^1.19.1"
}
}
28 changes: 28 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


lodash@^4.17.15:
version "4.17.15"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==

moment@^2.24.0:
version "2.24.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b"
integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==

node-http-xhr@^1.3.4:
version "1.3.4"
resolved "https://registry.yarnpkg.com/node-http-xhr/-/node-http-xhr-1.3.4.tgz#0dbf827c2858f40c57a79375fc4be07c8ff46f55"
integrity sha512-0bA08/2RKWxw6pMkOVd3KP+0F5+ifQLMMTDxrCgxlgkoU1N8DhCbCSAYEqpgaVYM2smvbVVewiXjW+8AyoLfxQ==

prettier@^1.19.1:
version "1.19.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.19.1.tgz#f7d7f5ff8a9cd872a7be4ca142095956a60797cb"
integrity sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==

spotify-web-api-js@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/spotify-web-api-js/-/spotify-web-api-js-1.2.0.tgz#edfbbcf5a2727c1873c5a310a40f8f1f2b4ecb8a"
integrity sha512-S5Awpk/uDwbn5WQbZmnU3RG/Hi68yX6ElQy9ewUu+ZSWiswufp50Vfg7vvXPaRfflL5/p/SOaU2x1idNRrzADA==

0 comments on commit 510a9e5

Please sign in to comment.