Skip to content

Instantly share code, notes, and snippets.

@sangam14
Created February 23, 2023 20:01
Show Gist options
  • Save sangam14/241a471238dba534bf4224eaa6e44ac5 to your computer and use it in GitHub Desktop.
Save sangam14/241a471238dba534bf4224eaa6e44ac5 to your computer and use it in GitHub Desktop.
GitStar Counter - Scriptable App
const WIDGET_TITLE = "ThreatMapper"
const GITHUB_REPO = "deepfence/ThreatMapper"
const CACHED_DATA_HOURS = 1
async function createWidget() {
// Create new empty ListWidget instance
let listwidget = new ListWidget();
// Set new background color
startColor = new Color("#212121")
endColor = new Color("#121212")
let gradient = new LinearGradient()
gradient.colors = [startColor, endColor]
gradient.locations = [0, 1]
listwidget.backgroundGradient = gradient
listwidget.addSpacer(16);
// Add widget heading
let subheading = listwidget.addText("GitHub");
subheading.font = Font.mediumSystemFont(14);
subheading.textColor = new Color("#fff");
listwidget.addSpacer(68);
// Add widget heading
let heading = listwidget.addText(WIDGET_TITLE);
heading.font = Font.boldSystemFont(20);
heading.textColor = new Color("#fff");
listwidget.addSpacer(8);
// This logic allows to save data in cache to avoid doing multiple requests
const files = FileManager.local()
const path = files.joinPath(files.cacheDirectory(), "widget-apple-open-source")
// Check if a cache file exists
const cacheExists = files.fileExists(path)
// If cache file exists, retrieve last time it was modified
const cacheDate = cacheExists ? files.modificationDate(path) : 0
// Get current time
const currentTime = Date.now()
let stars = "?"
// If cache exists and it has been written within last 1 hour, retrieve that data
if (cacheExists && (currentTime - cacheDate) < (CACHED_DATA_HOURS * 60 * 60 * 1000)) {
// Get the data from cached file
data_cached = JSON.parse(files.readString(path))
if (GITHUB_REPO) {
stars = data_cached["stars"]
}
} else {
data_to_be_cached = {}
// Do a new data request
if (GITHUB_REPO) {
[stars] = await getGitHubStats(GITHUB_REPO);
data_to_be_cached["stars"] = stars
}
// Update cached file with such data
files.writeString(path, JSON.stringify(data_to_be_cached))
}
if (GITHUB_REPO) {
// Display stars from project
let githubStars = listwidget.addText(`★ ${stars}`);
githubStars.font = Font.semiboldSystemFont(16);
githubStars.textColor = new Color("#69FF69");
listwidget.addSpacer(16);
}
// Return the created widget
return listwidget;
}
async function getGitHubStats(repoName) {
// Query url
const url = `https://api.github.com/repos/${repoName}`;
// Initialize new request
const request = new Request(url);
// Execute the request and parse the response as json
const data = await request.loadJSON();
// Return GitHub stats
return [data.stargazers_count];
}
let widget = await createWidget();
// Check where the script is running
if (config.runsInWidget) {
// Runs inside a widget so add it to the homescreen widget
Script.setWidget(widget);
} else {
// Show the medium widget inside the app
widget.presentLarge();
}
Script.complete();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment