Created
November 2, 2020 21:49
-
-
Save rsalzer/23f091a4370212d9d17f094c9938e848 to your computer and use it in GitHub Desktop.
Scriptable-Script to show a random featured picture on Wikimedia Commons as a Widget
This file contains 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
//Showing random wikimedia-commons-featured-picture | |
const url = `https://randomincategory.toolforge.org/Featured_pictures_on_Wikimedia_Commons?server=commons.wikimedia.org&type=file&debug=1` | |
const req = new Request(url) | |
const res = await req.loadString() | |
let titleRegExp = new RegExp("Location: https://commons.wikimedia.org/wiki/(.*)") | |
let titleMatch = res.match(titleRegExp) | |
let title = titleMatch[1].replace("<br>", ""); | |
let titleEncoded = encodeURI(title) | |
let url2 = `https://commons.wikimedia.org/w/api.php?action=query&titles=${titleEncoded}&prop=pageimages&format=json&pithumbsize=500` | |
const req2 = new Request(url2) | |
const result = await req2.loadJSON() | |
var key = Object.keys(result.query.pages)[0] | |
var imageURL = result.query.pages[key].thumbnail.source | |
const i = new Request(imageURL); | |
const img = await i.loadImage(); | |
var imgUrl = "https://commons.wikimedia.org/wiki/"+titleEncoded | |
let widget = createWidget("", img, imgUrl) | |
if (config.runsInWidget) { | |
Script.setWidget(widget) | |
Script.complete() | |
} | |
else { | |
widget.presentLarge() | |
} | |
function createWidget(title, img, widgeturl) { | |
let w = new ListWidget() | |
w.setPadding(0, 0, 0, 0) | |
w.backgroundColor = new Color("#1A1A1A") | |
w.url = widgeturl | |
w.backgroundImage = img | |
return w | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment