Skip to content

Instantly share code, notes, and snippets.

@electerious
Last active March 17, 2021 18:32
Show Gist options
  • Save electerious/8b7c00cf89bdfe7d88bb69b49b69da87 to your computer and use it in GitHub Desktop.
Save electerious/8b7c00cf89bdfe7d88bb69b49b69da87 to your computer and use it in GitHub Desktop.
Ackee iOS widget for Scriptable.app
// On first run only, uncomment line bellow and replace '888...' with the token returned after login
// Keychain.set("ACKEE_KEY", "88888888-8888-8888-8888-888888888888")
// Ackee configuration
const ACKEE_KEY = Keychain.get('ACKEE_KEY')
const ACKEE_API = 'https://ackee.example.com/api'
const TIME_ZONE = 'Europe/Berlin'
const createWidget = async () => {
const viewsToday = await getViewsToday()
const list = new ListWidget()
list.backgroundColor = new Color('#333838')
list.refreshAfterDate = new Date(Date.now() + 60 * 60 * 1000)
const header = list.addText('📈 Ackee'.toUpperCase())
header.font = Font.mediumSystemFont(13)
list.addSpacer()
if (viewsToday) {
const label = list.addText(viewsToday)
label.font = Font.boldSystemFont(24)
label.textColor = new Color('#73FAC8')
list.addText('Views Today')
} else {
list.addText('Data not available')
}
return list
}
const getViewsToday = async () => {
const body = {
query: `
{
facts {
viewsToday
}
}
`,
variables: {}
}
const headers = {
'authorization': `Bearer ${ ACKEE_KEY }`,
'content-type': 'application/json',
'time-zone': TIME_ZONE
}
const r = new Request(ACKEE_API)
r.headers = headers
r.body = JSON.stringify(body)
r.method = 'post'
try {
const result = await r.loadJSON()
return String(result.data.facts.viewsToday)
} catch (err) {
return undefined
}
}
const widget = await createWidget()
if (!config.runsInWidget) await widget.presentSmall()
Script.setWidget(widget)
Script.complete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment