-
-
Save marco79cgn/ecd1cfe3da06008da9fb9f5d1fa37a1d to your computer and use it in GitHub Desktop.
// insert your Spotify client id and secret here | |
let clientId = "xxx" | |
let clientSecret = "xxx" | |
// use your spotify country iso code to optimize search results | |
let spotifyCountry = "DE" | |
// optional: the ip of your node-sonos-http-api and room name; use "sonos" as parameter in your widget settings to activate it | |
let sonosUrl = "http://192.168.178.10:5005/Kitchen" | |
let openWith = args.widgetParameter | |
let widget = new ListWidget() | |
widget.setPadding(0,0,0,0) | |
widget.backgroundColor = new Color("#000000") | |
let searchToken = await getSpotifySearchToken() | |
await getRandomAlbum() | |
Script.setWidget(widget) | |
Script.complete() | |
async function getRandomAlbum() { | |
// load json from iCloud Drive | |
// source: https://gist.github.com/marco79cgn/92092f4a4f05434efe84f7191e55a8de | |
let fm = FileManager.iCloud() | |
let dir = fm.documentsDirectory() | |
let path = fm.joinPath(dir, "top_500_albums.json") | |
let contents = Data.fromFile(path) | |
let jsonTop500 = JSON.parse(contents.toRawString()) | |
let uri = "" | |
let externalUrl = "" | |
let coverUrl = "" | |
do { | |
let randomNumber = getRandomNumber(1, 500) | |
let result = await searchAlbumAtSpotify(jsonTop500[randomNumber-1].Album, jsonTop500[randomNumber-1].Artist) | |
// verify spotify result and query next album if empty/not available | |
if (result != null && result.albums != null && result.albums.items != null && result.albums.items.length == 1) { | |
let item = result.albums.items[0] | |
coverUrl = item.images[0].url | |
uri = item.uri | |
externalUrl = item.external_urls.spotify | |
} | |
} while(coverUrl.length == 0) | |
if(openWith != null && openWith === "sonos") { | |
widget.url = sonosUrl + "/spotify/now/" + uri | |
} else { | |
widget.url = externalUrl | |
} | |
await loadImage(coverUrl) | |
} | |
// gets a spotify search token | |
async function getSpotifySearchToken() { | |
let url = "https://accounts.spotify.com/api/token" | |
let req = new Request(url) | |
req.method = "POST" | |
req.body = "grant_type=client_credentials" | |
let authHeader = "Basic " + btoa(clientId+":"+clientSecret) | |
req.headers = {"Authorization": authHeader, "Content-Type":"application/x-www-form-urlencoded"} | |
let token = await req.loadJSON() | |
return token.access_token | |
} | |
// random number, min and max included | |
function getRandomNumber(min, max) { | |
return Math.floor(Math.random() * (max - min + 1) + min) | |
} | |
// search for the album at Spotify | |
async function searchAlbumAtSpotify(album, artist) { | |
let searchString = encodeURIComponent("album:" + album +" artist:" + artist) | |
let searchUrl = "https://api.spotify.com/v1/search?q=" + searchString + "&type=album&market=" + spotifyCountry + "&limit=1" | |
req = new Request(searchUrl) | |
req.headers = {"Authorization": "Bearer " + searchToken, "Content-Type":"application/json", "Accept":"application/json"} | |
let result = await req.loadJSON() | |
return result | |
} | |
// download and display the cover | |
async function loadImage(imageUrl) { | |
let req = new Request(imageUrl) | |
let image = await req.loadImage() | |
widget.backgroundImage = image | |
} |
Hey, great widget!
How did you make the json file out of the top 500 albums? To phrase my question differently, could I do this with a selection of albums I want or did you type this all out?
Someone provided a csv file, I downloaded it and converted it with a tool to json.
Another approach would be to query the Spotify search api, e.g. with a shell script and jq parser. Do you have some kind of a list with your desired albums?
I don’t have a list ready, just curious if it was possible
This is my first project to learn coding, it’s giving me a null error from lines 29 to 39. Was wondering if anyone could help me sort it out?
This is my first project to learn coding, it’s giving me a null error from lines 29 to 34. Was wondering if anyone could help me sort it out?
Did you download the json file and put it in the Scriptable iCloud folder as I wrote in the description? Is it properly named?
The json file containing the Top 500
Please download it and put it in your iCloud Drive Scriptable folder with the file name 'top_500_albums.json'
This is my first project to learn coding, it’s giving me a null error from lines 29 to 34. Was wondering if anyone could help me sort it out?
Did you download the json file and put it in the Scriptable iCloud folder as I wrote in the description? Is it properly named?
The json file containing the Top 500 Please download it and put it in your iCloud Drive Scriptable folder with the file name 'top_500_albums.json'
I made sure I named it right, although I didn’t keep the quotations in the name, could that be an issue?
The file is the right size and I have the ] at the very bottom
Could you please copy the error log inside the scriptable app when you run it and paste it here?
2021-01-17 16:30:51: Error on line 29:39: TypeError: null is not an object (evaluating 'contents.toRawString')
It‘s definitely a problem with the json file. It seems to be not available.
Could you please add this line here between line number 27 and 28? Then try to run the script again.
fm.downloadFileFromiCloud(path)
Wild guess: It could be that you saved the file on another device than the one you‘re running the script and it‘s not yet fully downloaded/synced. The line above should solve this. If that‘s the case, another possible solution is to open the files app on the same device as the scriptable app and check that there is no cloud symbol beside the file. If there is one, just click on it so the file will be downloaded immediately.
I’ve worked on this from the start on my phone, but I put the line in and it’s now giving me another error.
2021-01-17 16:54:12: Error on line 29: SyntaxError: Unexpected string literal "top_500_albums.json". Expected ')' to end an argument list.
I’ve worked on this from the start on my phone, but I put the line in and it’s now giving me another error.
2021-01-17 16:54:12: Error on line 29: SyntaxError: Unexpected string literal "top_500_albums.json". Expected ')' to end an argument list.
Something seems to be broken in your script. Either a copy/paste error or an edit by accident. The log claims that there is no closing perenthesis in line 29
let path = fm.joinPath(dir, "top_500_albums.json")
I would recommend to start from scratch and copy it again.
Will do, do you think I should redownload the top 500 json as well as redoing the Spotify stuff?
Intro
iOS 14 Custom Widget made with the help of the Scriptable app.
It shuffles a random album of the „Top 500 albums of all time“ (according to the Rolling Stone magazine) and shows the cover art.
Upon tapping on the widget it opens the album either in Spotify or plays it on your Sonos system.
Requirements
Join the beta here
Please create a client to get your client id and client_secret credentials. They are needed for the search.
Insert them at the top of the script.
Please download it and put it in your iCloud Drive Scriptable folder with the file name 'top_500_albums.json'
The node-sonos-http-api in order to play the album on your Sonos system.
Insert your ip address in the script and set the run script command to 'sonos' in the widget settings)
Thanks
A big Thank you to @simonbs for making great apps like Scriptable, DataJar or Jayson.