Skip to content

Instantly share code, notes, and snippets.

@riderx
Last active October 30, 2022 16:32
Show Gist options
  • Save riderx/f42979572d1733c7de86a478853bed90 to your computer and use it in GitHub Desktop.
Save riderx/f42979572d1733c7de86a478853bed90 to your computer and use it in GitHub Desktop.
Scriptable script to get info in widget ios
// By @riderx
// Prerequisite: install Scriptable @ https://apps.apple.com/us/app/scriptable/id1405459188
// Create an API key by navigating to Profile > View Profile. Then:
// Under API Keys, select New API Key.
// access Read-only
const API_TOKEN = '***'
const API_SECRET = '***'
// Recreating a basic auth with Scriptable lib
const auth = `Basic ${btoa(`${API_TOKEN}:${API_SECRET}`)}`
const currentYear = new Date().getYear()
const startDate = `${currentYear}-01-01`
const endDate = `${currentYear}-12-31`
const endpointTotal = `https://api.chartmogul.com/v1/metrics/mrr?start-date=${startDate}&end-date=${endDate}`
const endpointCapgo = `${endpointTotal}&plans=Solo,Team,Pay%20as%20you%20go,Maker`
const endpointCaptime = `${endpointTotal}&plans=Captime%20PRO,Captime%20PRO%20Monthly,Captime%20PRO%20yearly%20deal,ee.forgr.captime.pro2,ee.forgr.captime.pro_deal,ee.forgr.captime.pro_monthly`
// the only way to filter is using the plan name, you can find them here : https://app.chartmogul.com/#/admin/plans
async function loadItems(at) {
const req = new Request(at)
req.headers = { Authorization: auth }
const response = await req.loadJSON()
return response
}
function createProject(wid, title, val, s) {
const t1 = wid.addText(title)
t1.textColor = Color.white()
t1.font = new Font('Avenir-Heavy', s)
const formatedNumber = new Intl.NumberFormat('en-US').format(Math.floor(val / 100).toString())
const t2 = wid.addText(`€${formatedNumber}`)
t2.textColor = Color.white()
t2.font = new Font('Avenir-Heavy', s+6)
}
// Request the MRR data
const jsonTotal = await loadItems(endpointTotal)
const jsonCapgo = await loadItems(endpointCapgo)
const jsonCaptime = await loadItems(endpointCaptime)
// Create the widget
const w = new ListWidget()
w.backgroundColor = new Color('#3880ff')
createProject(w, 'Total MRR', jsonTotal.summary.current , 16)
w.addSpacer(4)
createProject(w, 'Capgo MRR', jsonCapgo.summary.current, 12)
createProject(w, 'Captime MRR', jsonCaptime.summary.current, 12)
Script.setWidget(w)
Script.complete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment