Skip to content

Instantly share code, notes, and snippets.

@marco79cgn
Last active March 28, 2023 10:28
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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;
}
@marco79cgn
Copy link
Author

marco79cgn commented Aug 29, 2020

Intro

iOS 14 Custom Widget made with the help of the Scriptable app.
It shuffles a random episode of The Simpsons. Upon tapping on the widget it plays the episode on Disney+.

Requirements

  • iOS 14 Developer Beta 5 (or above)
  • Scriptable TestFlight version 1.5 (156)
    Join the beta here
  • A Disney+ subscription
  • The json file containing all episodes
    Please download it and put it in your iCloud Drive Scriptable folder with the file name "simpsons.json"
  • Optional:
    I built a shortcut which automates the whole process. It downloads both the episodes metadata file (simpsons.json) and the widget script above. Just choose your Scriptable folder in iCloud Drive twice as destination. After that you'll find the script in your Scriptable app (named simpsonsRandomizer).

Thanks

A big Thank you to @simonbs for making great apps like Scriptable, DataJar or Jayson.

@scottfwalter
Copy link

Thanks for sharing. This widget has given me some inspiration!

Copy link

ghost commented Dec 4, 2021

Hallo,
kann ich die Beschreibung der Episode auch auf deutsch einstellen?
Und in welchem Intervall werden neue Episoden in dem Widget angezeigt?

Vielen Dank!

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