Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created March 27, 2018 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/01c5891d83dfb3ba13be671e3840135b to your computer and use it in GitHub Desktop.
Save tmcw/01c5891d83dfb3ba13be671e3840135b to your computer and use it in GitHub Desktop.
const fs = require('fs');
const fetch = require('node-fetch');
const dsv = require('d3-dsv');
const {timeFormat} = require('d3-time-format');
let tracks = {};
const formatTime = timeFormat("%Y-%m-%d");
async function download(d) {
const dateString = formatTime(d);
console.log(dateString);
const csv = dsv.csvParse(await (await fetch(`https://spotifycharts.com/regional/global/daily/${dateString}/download`)).text());
csv.forEach(row => {
const id = row.URL.match(/\/([^\/]*)$/)[1];
if (!tracks[id]) {
tracks[id] = {
track: row['Track Name'],
artist: row.Artist,
streams: []
};
}
tracks[id].streams.push({ date: dateString, streams: +row.Streams, position: +row.Position });
});
}
async function run() {
const start = new Date('2018-01-01');
const end = new Date('2018-03-27');
const now = new Date(start);
while (now <= end) {
console.log(`Downloading ${now}`);
await download(new Date(now));
now.setDate(now.getDate() + 1);
}
tracks = Object.entries(tracks).map(([id, track]) => ({id, track}));
fs.writeFileSync('tracks.json', JSON.stringify({
tracks,
range: [formatTime(start), formatTime(end)]
}, null, 2));
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment