Skip to content

Instantly share code, notes, and snippets.

@moxmlb
Last active March 29, 2022 10:27
Show Gist options
  • Save moxmlb/a66f1592fb145ff5517d6028bc697880 to your computer and use it in GitHub Desktop.
Save moxmlb/a66f1592fb145ff5517d6028bc697880 to your computer and use it in GitHub Desktop.
Scriptable iOS widget that shows the current price of your favorite movie on iTunes Store
let movieId, country, quality
if (country == null && movieId == null && quality == null) {
country = 'de'
movieId = 991367195 //example id for Minions movie
quality = 'hd'
}
// api link
let link = 'https://itunes.apple.com/lookup?'
let widgetInputRAW = args.widgetParameter;
let widgetInput = null;
// get widget input, seperated by |
if (widgetInputRAW !== null) {
[movieId, country, quality] = widgetInputRAW.toString().split("|");
}
var backgroundColor, textColor
if (Device.isUsingDarkAppearance()) {
backgroundColor = Color.black()
textColor = Color.white()
} else {
backgroundColor = Color.white()
textColor = Color.black()
}
const widget = new ListWidget()
const result = await fetchiTunesInfo()
await createWidget()
if (!config.runsInWidget) {
Safari.open(result.results[0].trackViewUrl.toString())
await widget.presentSmall()
}
Script.setWidget(widget)
Script.complete()
async function createWidget() {
widget.addSpacer(4)
widget.setPadding(10, 10, 10, 10)
let titleText = widget.addText(result.results[0].trackName)
titleText.textColor = textColor
titleText.font = Font.boldSystemFont(20)
titleText.minimumScaleFactor = 0.5
titleText.centerAlignText()
widget.addSpacer()
const artwork = await getImage('movieArtwork.jpg')
let image = widget.addImage(artwork)
image.centerAlignImage()
widget.addSpacer()
var priceBuyText = ''
if (quality == 'sd') {
priceBuyText = widget.addText(result.results[0].trackPrice.toString() + " " + result.results[0].currency.toString())
}
else {
priceBuyText = widget.addText(result.results[0].trackHdPrice.toString() + " " + result.results[0].currency.toString())
}
priceBuyText.font = Font.mediumRoundedSystemFont(13)
priceBuyText.textColor = textColor
priceBuyText.centerAlignText()
}
async function fetchiTunesInfo() {
let url
var price = 0.0
url = link + 'country=' + country + '&id=' + movieId
const req = new Request(url)
const apiResult = await req.loadJSON()
return apiResult
}
async function getImage(image) {
let imageUrl = result.results[0].artworkUrl100.replace('100x100', '1000x1000')
let movieImage = await loadImage(imageUrl)
return movieImage
}
async function loadImage(imgUrl) {
const req = new Request(imgUrl)
return await req.loadImage()
}
@Leibinger015
Copy link

Leibinger015 commented Nov 15, 2020

The indicator of the promotion Wednesday film would be great.

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