Created
October 8, 2020 18:32
-
-
Save mkj-is/1b9d1d3d3f798daa7b774efc27778865 to your computer and use it in GitHub Desktop.
Discogs collection widget
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
// This widgets show approximate value of your music collection on discogs.com | |
// and the last addition to the collection in the background. | |
// Configuration: | |
// Your Discogs personal token generated here: https://www.discogs.com/settings/developers | |
let token = "..." | |
// Your Discogs username | |
let username = "..." | |
// Widget: | |
let items = await loadItems() | |
let widget = await createWidget(items) | |
// Check if the script is running in | |
// a widget. If not, show a preview of | |
// the widget to easier debug it. | |
if (!config.runsInWidget) { | |
await widget.presentSmall() | |
} | |
// Tell the system to show the widget. | |
Script.setWidget(widget) | |
Script.complete() | |
async function createWidget(value) { | |
let gradient = new LinearGradient() | |
gradient.locations = [0, 1] | |
gradient.colors = [ | |
new Color("#101010aa"), | |
new Color("#050505aa") | |
] | |
let w = new ListWidget() | |
w.backgroundGradient = gradient | |
w.backgroundImage = await loadImage() | |
// Add spacer above content to center it vertically. | |
w.addSpacer() | |
let h = w.addStack() | |
h.layoutHorizontally() | |
let symbol = SFSymbol.named("opticaldisc") | |
symbol.applyFont(Font.systemFont(26)) | |
let image = h.addImage(symbol.image) | |
image.tintColor = Color.white() | |
image.resizable = false | |
h.addSpacer(8) | |
let v = h.addStack() | |
v.layoutVertically() | |
// MIN | |
let minHeaderText = v.addText("MIN") | |
minHeaderText.font = Font.mediumSystemFont(12) | |
minHeaderText.textColor = Color.white() | |
minHeaderText.textOpacity = 0.5 | |
let minText = v.addText(value.minimum) | |
minText.font = Font.systemFont(16) | |
minText.textColor = Color.white() | |
v.addSpacer(8) | |
// MEDIAN | |
let medHeaderText = v.addText("MED") | |
medHeaderText.font = Font.mediumSystemFont(12) | |
medHeaderText.textColor = Color.white() | |
medHeaderText.textOpacity = 0.5 | |
let medText = v.addText(value.median) | |
medText.font = Font.boldSystemFont(16) | |
medText.textColor = Color.white() | |
v.addSpacer(8) | |
// MAX | |
let maxHeaderText = v.addText("MAX") | |
maxHeaderText.font = Font.mediumSystemFont(12) | |
maxHeaderText.textColor = Color.white() | |
maxHeaderText.textOpacity = 0.5 | |
let maxText = v.addText(value.maximum) | |
maxText.font = Font.systemFont(16) | |
maxText.textColor = Color.white() | |
w.addSpacer() | |
return w | |
} | |
async function loadItems() { | |
let url = "https://api.discogs.com/users/" + username + "/collection/value" | |
let req = new Request(url) | |
req.headers = { | |
"Authorization": "Discogs token=" + token, | |
"User-Agent": "Scriptable/1.5.1 +https://scriptable.app" | |
} | |
return await req.loadJSON() | |
} | |
async function loadImage() { | |
let url = "https://api.discogs.com/users/" + username + "/collection/folders/0/releases?sort=added&sort_order=desc" | |
let req = new Request(url) | |
req.headers = { | |
"Authorization": "Discogs token=" + token, | |
"User-Agent": "Scriptable/1.5.1 +https://scriptable.app" | |
} | |
let json = await req.loadJSON() | |
let imgURL = json.releases[0].basic_information.cover_image | |
let request = new Request(imgURL) | |
return await request.loadImage() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Meant to be used in https://scriptable.app/