Skip to content

Instantly share code, notes, and snippets.

@ulybu
Last active March 3, 2017 11:43
Show Gist options
  • Save ulybu/9285b934f137939c04247260826529d8 to your computer and use it in GitHub Desktop.
Save ulybu/9285b934f137939c04247260826529d8 to your computer and use it in GitHub Desktop.
export spotify ids to text

What

Get title - artist list from spotify tracks id

How

  1. In Spotify, select tracks and ctrl + c
https://open.spotify.com/track/7qlglyHTqO3FtDg0WefvIE
https://open.spotify.com/track/2haoJgD4gq8AXvArFZASBG
https://open.spotify.com/track/1vygVzLAwQnGwmvG0j92dF
  1. In a text editor extracts comma-separated ids 7qlglyHTqO3FtDg0WefvIE,2haoJgD4gq8AXvArFZASBG,1vygVzLAwQnGwmvG0j92dF
  2. run js script
  3. run getTextFromIds("7qlglyHTqO3FtDg0WefvIE,2haoJgD4gq8AXvArFZASBG,1vygVzLAwQnGwmvG0j92dF")

It will print

Human Thing - Tiken Jah Fakoly
The Answer - Apollo Brown
La Celestina - Natalia Doco
function extractSpotify (data) {
var tracks = data.tracks;
var extract = tracks.map( track => ({
artist: track.artists[0].name,
title : track.name,
}));
return extract;
}
function formatSpotify (extracted) {
var out = extracted
.map( ex => ex.title + ' - ' + ex.artist)
.join('\n');
// copy(out);
return out;
}
function callAjax(url, callback){
var xmlhttp;
// compatible with IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
callback(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function promiseAjax (url) {
return new Promise(function(resolve) {
callAjax(url, resolve);
})
}
function loadInfo(ids) {
var urlBase = 'https://api.spotify.com/v1/tracks/?ids=';
var url = urlBase + ids;
return promiseAjax(url)
.then(JSON.parse);
}
function getTextFromIds (ids) {
loadInfo(ids)
.then(data => {
var extracted = extractSpotify(data);
var res = formatSpotify(extracted);
console.log(res)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment