Last active
December 4, 2018 21:02
-
-
Save lorenzoferrante/2a6db3481b0ddc49793fd533fcef0005 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let clientID = '<client_id>' | |
let clientSecret = '<client_secret>' | |
let cred = clientID + ':' + clientSecret | |
let credEncoded = Data.fromString(cred).toBase64String() | |
let auth = 'Basic ' + credEncoded | |
// Get token | |
let tokenURL = 'https://accounts.spotify.com/api/token' | |
var reqToken = new Request(tokenURL) | |
reqToken.method = 'POST' | |
reqToken.body = 'grant_type=client_credentials&undefined=' | |
reqToken.headers = { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Authorization': auth | |
} | |
let res = await reqToken.loadJSON() | |
let token = res['access_token'] | |
let auth2 = 'Bearer ' + token | |
// Params | |
var num = URLScheme.parameter('number') | |
var q = '' | |
if (parseInt(num) < 10 ) { | |
q = encodeURI(':00' + num + '/') | |
} else if (parseInt(num) < 100) { | |
q = encodeURI(':0' + num + '/') | |
} else if (parseInt(num) < 1000) { | |
q = encodeURI(':') + encodeURI(num) + '/' | |
} | |
let artist = encodeURI(':Die drei ???') | |
let endpoint = 'https://api.spotify.com/v1/search?q=album' + q + '&artist' + artist + '&type=album&limit=1' | |
let method = 'GET' | |
let headers = { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
'Authorization': auth2 | |
} | |
// Make the request | |
let req = new Request(endpoint) | |
req.method = method | |
req.headers = headers | |
// Get the track URL | |
let json = await req.loadJSON() | |
console.log(json) | |
var finalURL = json['albums']['items'][0]['external_urls']['spotify'] | |
// Open the track on Spotify | |
Safari.open(finalURL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment