Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@marco79cgn 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

This comment has been minimized.

Copy link

@scottfwalter scottfwalter commented Sep 11, 2020

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

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