Skip to content

Instantly share code, notes, and snippets.

@bdlowery
Created June 4, 2024 01:05
Show Gist options
  • Save bdlowery/6dfc97b15eeba3e5111e508c22de385e to your computer and use it in GitHub Desktop.
Save bdlowery/6dfc97b15eeba3e5111e508c22de385e to your computer and use it in GitHub Desktop.
WIP_streak
// Replace with your API key
const API_KEY = "wip_sk_{YOUR_API_KEY_HERE}";
const API_URL = `https://api.wip.co/v1/users/me.json?api_key=${API_KEY}`;
// Function to fetch data from the API
async function fetchStreakData() {
let request = new Request(API_URL);
let response = await request.loadJSON();
return response;
}
// Function to calculate hours left until midnight in a given time zone
function hoursLeftUntilMidnight(timeZone) {
let now = new Date();
let nowInUserTimeZone = new Date(now.toLocaleString("en-US", { timeZone: timeZone }));
let midnight = new Date(nowInUserTimeZone);
midnight.setHours(24, 0, 0, 0); // Set to midnight
let hoursLeft = (midnight - nowInUserTimeZone) / (1000 * 60 * 60); // Convert milliseconds to hours
return hoursLeft.toFixed(0);
}
// Function to create a widget displaying the streak info
async function createWidget() {
let data = await fetchStreakData();
let streakText;
let widget = new ListWidget();
streakText = widget.addText(`${data.streak} 🔥`);
streakText.font = Font.boldSystemFont(30);
streakText.textColor = Color.black();
streakText.centerAlignText();
widget.addSpacer(8);
let statusText;
if (data.streaking) {
statusText = widget.addText("Streaking");
widget.backgroundColor = new Color("#f9db00"); // Yellow
} else {
let hoursLeft = hoursLeftUntilMidnight(data.time_zone);
statusText = widget.addText(`${hoursLeft} hours left...`);
widget.backgroundColor = new Color("#F44336"); // Red
}
statusText.textColor = Color.black();
statusText.font = Font.boldSystemFont(16);
statusText.centerAlignText();
return widget;
}
// Main
let widget = await createWidget();
if (config.runsInWidget) {
Script.setWidget(widget);
} else {
widget.presentSmall();
}
Script.complete();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment