Skip to content

Instantly share code, notes, and snippets.

@marckohlbrugge
Created June 1, 2024 13:38
Show Gist options
  • Save marckohlbrugge/7536c230165c1c93934a34f6e4aea5f2 to your computer and use it in GitHub Desktop.
Save marckohlbrugge/7536c230165c1c93934a34f6e4aea5f2 to your computer and use it in GitHub Desktop.
Scriptable widget showing your streak. Make sure to set your own API key ( https://wip.co/my/api_keys )
{
"always_run_in_app" : false,
"icon" : {
"color" : "yellow",
"glyph" : "fire"
},
"name" : "WIP Streak",
"script" : "\/\/ Replace with your API key\nconst API_KEY = \"wip_sk_REPLACE_ME\";\nconst API_URL = `https:\/\/api.wip.co\/v1\/users\/me.json?api_key=${API_KEY}`;\n\n\/\/ Function to fetch data from the API\nasync function fetchStreakData() {\n let request = new Request(API_URL);\n let response = await request.loadJSON();\n return response;\n}\n\n\/\/ Function to calculate hours left until midnight in a given time zone\nfunction hoursLeftUntilMidnight(timeZone) {\n let now = new Date();\n let nowInUserTimeZone = new Date(now.toLocaleString(\"en-US\", { timeZone: timeZone }));\n let midnight = new Date(nowInUserTimeZone);\n midnight.setHours(24, 0, 0, 0); \/\/ Set to midnight\n\n let hoursLeft = (midnight - nowInUserTimeZone) \/ (1000 * 60 * 60); \/\/ Convert milliseconds to hours\n return hoursLeft.toFixed(0);\n}\n\n\/\/ Function to create a widget displaying the streak info\nasync function createWidget() {\n let data = await fetchStreakData();\n\n let streakText;\n let widget = new ListWidget();\n streakText = widget.addText(`${data.streak} 🔥`);\n streakText.font = Font.boldSystemFont(30);\n streakText.textColor = Color.black();\n streakText.centerAlignText()\n \n widget.addSpacer(8)\n \n let statusText;\n \n \n \n if (data.streaking) {\n statusText = widget.addText(\"Streaking\");\n widget.backgroundColor = new Color(\"#f9db00\"); \/\/ Yellow\n } else {\n let hoursLeft = hoursLeftUntilMidnight(data.time_zone);\n statusText = widget.addText(`${hoursLeft} hours left...`);\n widget.backgroundColor = new Color(\"#F44336\"); \/\/ Red\n }\n \n statusText.textColor = Color.black();\n statusText.font = Font.boldSystemFont(16)\n statusText.centerAlignText()\n\n return widget;\n}\n\n\/\/ Main\nlet widget = await createWidget();\nif (config.runsInWidget) {\n Script.setWidget(widget);\n} else {\n widget.presentSmall();\n}\n\nScript.complete();\n",
"share_sheet_inputs" : [
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment