Skip to content

Instantly share code, notes, and snippets.

@gwthompson
Forked from rsalzer/Paintings.js
Created September 23, 2020 12:53
Show Gist options
  • Save gwthompson/a8ea152c5506492ca57fbf86ed80c194 to your computer and use it in GitHub Desktop.
Save gwthompson/a8ea152c5506492ca57fbf86ed80c194 to your computer and use it in GitHub Desktop.
Scriptable to show a random painting from the Metropolitan Museum of Art
//Department ID = 11 is paintings ; use other if you wish
const url = 'https://collectionapi.metmuseum.org/public/collection/v1/search?hasImages=true&departmentId=11&q=Painting'
const req = new Request(url)
const res = await req.loadJSON()
const max = res.total-1
console.log("Max: "+max);
const min = 0
const random = Math.floor(Math.random() * (max - min + 1)) + min
console.log("Random: "+random);
const picked = res.objectIDs[random]
console.log("Picked: "+picked);
const req2 = new Request('https://collectionapi.metmuseum.org/public/collection/v1/objects/'+picked)
const res2 = await req2.loadJSON()
const i = new Request(res2.primaryImageSmall);
const img = await i.loadImage();
let widget = createWidget(img, res2.objectURL)
if (config.runsInWidget) {
// create and show widget
Script.setWidget(widget)
Script.complete()
}
else {
widget.presentMedium()
}
function createWidget(img, widgeturl) {
let w = new ListWidget()
w.backgroundColor = new Color("#1A1A1A")
w.backgroundImage = img
w.url = widgeturl
return w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment