Created
May 28, 2022 06:20
-
-
Save mikedemarais/4a41df9c54348a5f3a1522340be1656b to your computer and use it in GitHub Desktop.
Scriptable script for widget that displays NPM download count for a given package
This file contains 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
let package = '@rainbow-me/rainbowkit'; | |
const param = args.widgetParameter; | |
if (param != null && param.length > 0) { | |
package = param; | |
} | |
const upperFirst = s => s && s[0].toUpperCase() + s.slice(1); | |
async function getDownloads(timePeriod) { | |
let url = `https://api.npmjs.org/downloads/point/last-${timePeriod}/${package}`; | |
let req = new Request(url); | |
let data = await req.loadJSON(); | |
return `${upperFirst(timePeriod)}: ${data.downloads}`; | |
} | |
if (config.runsInWidget) { | |
let widget = await createWidget() | |
Script.setWidget(widget) | |
Script.complete() | |
} | |
async function createWidget() { | |
let widget = new ListWidget() | |
widget.backgroundColor = new Color("#CC3534") // this is NPM brand's red color | |
let title = widget.addText(`${package} stats`) | |
title.textColor = Color.white() | |
title.lineLimit = 2 | |
title.font = Font.heavyRoundedSystemFont(14) | |
widget.addSpacer(); | |
addText(await getDownloads("day"), widget) | |
widget.addSpacer(); | |
addText(await getDownloads("week"), widget) | |
widget.addSpacer(); | |
addText(await getDownloads("month"), widget) | |
return widget | |
} | |
async function addText(text, listWidget) { | |
let item = listWidget.addText(text) | |
item.font = Font.semiboldRoundedSystemFont(18) | |
item.textColor = Color.white() | |
item.textOpacity = 0.85 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment