Skip to content

Instantly share code, notes, and snippets.

@marco79cgn
Last active March 28, 2023 10:28
Show Gist options
  • Save marco79cgn/ac9a8add1c7dc5a6749b751a1d2a05a4 to your computer and use it in GitHub Desktop.
Save marco79cgn/ac9a8add1c7dc5a6749b751a1d2a05a4 to your computer and use it in GitHub Desktop.
A Scriptable widget which shuffles a random Simpsons episode and plays it on Disney+ upon interaction
let openWith = args.widgetParameter
let widget = new ListWidget()
widget.setPadding(8, 10, 5, 10)
widget.backgroundColor = new Color("#111E6C")
await loadImage()
await getRandomEpisode()
Script.setWidget(widget)
Script.complete()
widget.presentLarge()
async function getRandomEpisode() {
// load json from iCloud Drive
let fm = FileManager.iCloud()
let dir = fm.documentsDirectory()
let path = fm.joinPath(dir, "simpsons.json")
let contents = Data.fromFile(path)
let episodes = JSON.parse(contents.toRawString())
var episodeList = [];
for(var i in episodes)
episodeList.push(episodes[i]);
let randomNumber = getRandomNumber(1, episodeList.length)
let chosenItem = episodeList[randomNumber-1]
let title = chosenItem.title
let season = chosenItem.seasonNumber
let episode = chosenItem.episodeNumber
widget.url = chosenItem.url
let titleText = widget.addText(title)
titleText.textColor = Color.white()
titleText.font = new Font("AvenirNext-DemiBold", 13)
titleText.lineLimit = 2
titleText.leftAlignText()
widget.addSpacer(2)
await loadMetadata(season, episode)
}
async function loadMetadata(season, episode) {
let tvMazeUrl = "http://api.tvmaze.com/shows/83/episodebynumber?season=" + season + "&number=" + episode
let tvMazeRequest = new Request(tvMazeUrl)
let tvMazeResponse = await tvMazeRequest.loadJSON()
let excerptText = widget.addText(tvMazeResponse.summary.replace(/<\/?[^>]+(>|$)/g, ""))
excerptText.textColor = Color.lightGray()
excerptText.font = new Font("AvenirNext-Medium", 13)
excerptText.lineLimit = 4
excerptText.leftAlignText()
widget.addSpacer(4)
let airDate = new Date(Date.parse(tvMazeResponse.airstamp))
let df = new DateFormatter()
df.useMediumDateStyle()
df.useNoTimeStyle()
let strDate = df.string(airDate)
let seasonEpiside = "S" + add_zero(season, 2) + "E" + add_zero(episode, 2)
let dateAddedText = widget.addText("📺 " + seasonEpiside+" 📅 " + strDate)
dateAddedText.textColor = Color.white()
dateAddedText.font = new Font("AvenirNext-DemiBold", 11)
dateAddedText.leftAlignText()
}
// helper function to download an image
async function loadImage() {
let req = new Request("https://assets.foxdcg.com/dpp-uploaded/images/the-simpsons/the-simpsons_30/logo-tab.png")
let image = await req.loadImage()
let widgetImage = widget.addImage(image)
widgetImage.imageSize = new Size(65,25)
widgetImage.rightAlignImage()
}
// random number, min and max included
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
// add leading zeros to a number
function add_zero(number, length) {
var num = '' + number;
while (num.length < length) {
num = '0' + num;
}
return num;
}
Copy link

ghost commented Dec 5, 2021

Zudem bekomme ich auf meinem iPad mini 4 mit iOS 14 eine Fehlermeldung, sofern ich das entsprechende Widget erstellen möchte:
Error on line 19:37: TypeError: null is not an object (evaluating 'contents.toRawString')

Wie kann ich den Fehler beheben? Auf meinem iPhone läuft das Widget wunderbar!

Um Hilfe bin ich sehr dankbar!

@Thedomestabot
Copy link

How did you make the json? What method to pull all that data? I want to make a similar widget but for Rick and Morty.

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