Created
September 25, 2020 10:14
-
-
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
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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.