Skip to content

Instantly share code, notes, and snippets.

@rsalzer
Created September 25, 2020 10:14
Show Gist options
  • Save rsalzer/2ef5a6c8d3b36577582670e5819e7d72 to your computer and use it in GitHub Desktop.
Save rsalzer/2ef5a6c8d3b36577582670e5819e7d72 to your computer and use it in GitHub Desktop.
Code for a widget for the Scriptable-App (iOS) which shows you random wikipedia previews
const lang = "en" //use the language of your choice "en", "de", "it", "fr", etc.
const url = `https://${lang}.wikipedia.org/api/rest_v1/page/random/summary`
const req = new Request(url)
const res = await req.loadJSON()
const i = new Request(res.thumbnail.source);
const img = await i.loadImage();
let widget = createWidget(res.title, img, res.content_urls.mobile.page)
if (config.runsInWidget) {
// create and show widget
Script.setWidget(widget)
Script.complete()
}
else {
widget.presentSmall()
}
function createWidget(title, img, widgeturl) {
console.log("Title: "+title)
let w = new ListWidget()
w.backgroundColor = new Color("#1A1A1A")
let image = w.addImage(img);
image.centerAlignImage();
let titleTxt = w.addText(title)
titleTxt.textColor = Color.white()
titleTxt.font = Font.systemFont(12)
titleTxt.centerAlignText()
w.url = widgeturl
w.setPadding(0, 5, 0, 0)
return w
}
@Duckbilled
Copy link

Is it possible to assign a category to the random page. I saw you used the randomincategory api in the wikimedia scriptable, but it would be awesome to have it in this script as well :). I tried some stuff and changing some settings in the mediapedia, but I am either doing something wrong or trying something that is not possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment