Last active
April 21, 2022 04:07
-
-
Save marco79cgn/7b5ec1d8fdaaa0a2a4c723c98b24330d to your computer and use it in GitHub Desktop.
A scriptable widget that counts down the days until the new Adele album "30" is released and plays her latest single until then
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
// Once you tap the widget, it plays her new single "Easy on me" on YouTube (default). | |
// You can configure "apple" or "spotify" as widget parameter | |
// to open the song in the corresponding app | |
const PARAM = args.widgetParameter | |
const DATE = "2021-11-19T08:00:00Z" | |
const BG_IMG_URL = "https://i.imgur.com/CycspeY.jpg" | |
const YOUTUBE_LINK = "https://www.youtube.com/watch?v=U3ASj1L6_sY" | |
const SPOTIFY_SONG_LINK = "https://open.spotify.com/track/0gplL1WMoJ6iYaPgMCL0gX?si=ea4b8de949d042dc" | |
const APPLE_ALBUM_LINK = "https://music.apple.com/de/album/30/1590035691" | |
let widget = await createWidget() | |
if (config.runsInWidget) { | |
Script.setWidget(widget) | |
Script.complete() | |
} else { | |
await widget.presentSmall() | |
} | |
async function createWidget() { | |
let eventDate = createEventDate() | |
let bgImg = await getImage("30-cover.jpg") | |
let widget = new ListWidget() | |
widget.setPadding(14, 8, 6, 12) | |
widget.backgroundImage = bgImg | |
const textStack = widget.addStack() | |
textStack.addSpacer() | |
let wtitle = textStack.addText("ADELE ") | |
wtitle.font = Font.lightMonospacedSystemFont(10) | |
wtitle.textColor = Color.white() | |
const album = textStack.addText("30") | |
album.font = Font.lightMonospacedSystemFont(10) | |
album.textColor = new Color("#EABF27") | |
widget.addSpacer() | |
const today = Date.now() | |
if (today < eventDate) { | |
let wdate = widget.addDate(eventDate) | |
wdate.applyRelativeStyle() | |
wdate.font = Font.semiboldRoundedSystemFont(14) | |
wdate.textColor = Color.white() | |
wdate.centerAlignText() | |
} | |
if (PARAM != null) { | |
if (PARAM.toLowerCase() === "spotify") { | |
widget.url = SPOTIFY_SONG_LINK | |
} else if (PARAM.toLowerCase() === "apple") { | |
widget.url = APPLE_ALBUM_LINK | |
} else { | |
widget.url = YOUTUBE_LINK | |
} | |
} | |
return widget | |
} | |
function createEventDate() { | |
let dateFormatter = new DateFormatter() | |
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" | |
dateFormatter.locale = "en_US" | |
return dateFormatter.date(DATE) | |
} | |
// get image from local filestore or download it only once | |
async function getImage(image) { | |
let fm = FileManager.local() | |
let dir = fm.documentsDirectory() | |
let path = fm.joinPath(dir, image) | |
if (fm.fileExists(path)) { | |
return fm.readImage(path) | |
} else { | |
// download once | |
let imageUrl | |
switch (image) { | |
case '30-cover.jpg': | |
imageUrl = BG_IMG_URL | |
break | |
default: | |
console.log(`Sorry, couldn't find ${image}.`); | |
} | |
let iconImage = await loadImage(imageUrl) | |
fm.writeImage(path, iconImage) | |
return iconImage | |
} | |
} | |
// helper function to download an image from a given url | |
async function loadImage(imgUrl) { | |
const req = new Request(imgUrl) | |
return await req.loadImage() | |
} | |
// Copyright (C) 2021 by marco79 <[email protected]> | |
// | |
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL | |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | |
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER | |
// IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
// OF THIS SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment