Skip to content

Instantly share code, notes, and snippets.

@djsnipa1
Created February 15, 2023 03:35
Show Gist options
  • Save djsnipa1/2c9705e17d1a767c6aecd649573a3e14 to your computer and use it in GitHub Desktop.
Save djsnipa1/2c9705e17d1a767c6aecd649573a3e14 to your computer and use it in GitHub Desktop.
Scriptable AppHookup
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: orange; icon-glyph: magic;
function cleanTitle(title) {
return title
.replace(/\[ios(\s*universal)?\]\s*/i, "")
.replace(/\[macos\]\s*/i, "")
.replace(/^\s*/, "")
.replace(/\[Windows\]\s*/i, "🎮")
.trim()
}
function getAppType(flair) {
if (flair.match(/ios(\s*universal )?/i)) {
return "📱"
} else if (flair.match(/macos/i)) {
return "🖥"
} else if (flair.match(/multi-platform/i)) {
return "⌨️"
} else if (flair.match(/Windows/i)) {
return "🎮"
} else {
return null
}
}
function getCreationDate(epoch) {
let date = new Date(0)
date.setUTCSeconds(epoch)
return date
}
async function getApps() {
// [reddit.com: api documentation](https://www.reddit.com/dev/api/#section_subreddits)
let url = "https://old.reddit.com/search.json?q=subreddit%3AAppHookup+%28flair%3Aios+OR+flair%3Amacos+OR+flair%3Amulti-platform+OR+flair%3AWindows%29&include_over_18=on&t=week&sort=new&raw_json=1&t=month"
let request = new Request(url)
log(request)
let json = await request.loadJSON()
var objects = json.data.children
var str = JSON.stringify(objects, null, 2); // spacing level = 2
// log(str)
var apps = json.data.children
.map(x => ({
title: cleanTitle(x.data.title),
//title: x.data.title,
image: x.data.thumbnail,
type: getAppType(x.data.link_flair_text),
url: x.data.url,
flair: x.data.link_flair_text,
permalink: x.data.permalink,
creationDate: getCreationDate(x.data.created_utc),
ups: x.data.ups
}))
.sort(x => x.creationDate)
return apps
}
async function getTable() {
const df = new RelativeDateTimeFormatter()
df.locale = "en"
df.useNamedDateTimeStyle()
let table = new UITable()
table.dismissOnSelect = false
table.showSeparators = true
let apps = await getApps()
//
for (let app of apps) {
let row = new UITableRow()
row.dismissOnSelect = false
row.onSelect = () => {
}
row.height = 45
let subtitle = df.string(app.creationDate, new Date())
// let ups = app.ups
log(app.ups)
if (app.ups < 10) {
var spacing = " "
} else if (app.ups > 99) {
var spacing = ""
} else {
var spacing = " "
}
let fullTitle = app.ups + spacing + " ▲ " + "│ " + app.title
let cell = UITableCell.text(fullTitle, subtitle)
let proFont = new Font("ProFontWindows", 14.0)
let proFontSmaller = new Font("ProFontWindows", 10.0)
row.addCell(cell)
cell.widthWeight = 100
cell.titleFont = proFont
cell.subtitleFont = proFontSmaller
let btn = row.addButton(app.type || "❓")
btn.widthWeight = 7
btn.dismissOnTap = false
btn.onTap = () => {
new CallbackURL(`apollo://reddit.com${app.permalink}`)
.open()
}
row.onSelect = (number) => {
Safari.open(app.url)
}
table.addRow(row)
}
return table
}
async function getWidget() {
let widget = new ListWidget()
let title = widget.addText("AppHookup")
let iosevkaBold = new Font("Iosevka-Term-Slab-Bold", 24)
title.font = iosevkaBold
title.centerAlignText()
title.textColor = new Color("fe8019")
widget.addSpacer()
let apps = await getApps()
for (let app of apps.slice(0, 5)) {
if (app.ups < 10) {
var spacing = " "
} else if (app.ups > 99) {
var spacing = ""
} else {
var spacing = " "
}
let fullTitle = app.ups + spacing + " ▲ " + "│ " + app.title
let wt = widget.addText(fullTitle)
wt.textColor = new Color("e9d9b3")
let iosevka = new Font("Iosevka-Term-Slab", 12)
wt.lineLimit = 1
wt.font = iosevka
}
const df = new DateFormatter()
df.locale = "en-US"
df.useLongDateStyle()
df.useShortTimeStyle()
widget.addSpacer()
let grad1 = "362f2d"
let grad2 = "262524"
let gradient = new LinearGradient()
gradient.locations = [0, 1]
gradient.colors = [
new Color(grad1),
new Color(grad2)
]
widget.backgroundGradient = gradient
let updateText = widget.addText(df.string(new Date()))
let iosevka = new Font("Iosevka-Term-Slab-Italic", 12)
updateText.font = iosevka
updateText.centerAlignText()
updateText.textColor = new Color("b6bb27")
return widget
}
let widget = await getWidget();
if (config.runsInWidget) {
new Notification()
Script.setWidget(widget)
} else if (config.runsInApp) {
widget.presentMedium();
let table = await getTable()
QuickLook.present(table, false)
}
Script.complete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment