Skip to content

Instantly share code, notes, and snippets.

@florentmorin
Last active April 9, 2021 11:14
Show Gist options
  • Save florentmorin/64989c329cb78aada582dafdbb4c7792 to your computer and use it in GitHub Desktop.
Save florentmorin/64989c329cb78aada582dafdbb4c7792 to your computer and use it in GitHub Desktop.
Script « Vite ma dose de vaccin » pour Scriptable

Installation

  1. Télécharger l'app gratuite Scriptable.
  2. Coller le code de vaccin.js dans un nouveau script Scriptable
  3. Configurer le script (instructions dans le code)

Configurer le widget

  1. Ajouter un widget Scriptable
  2. Configurer le widget en sélectionnant votre script
  3. (Optionnel) Ajouter en paramètre du widget un code de département

Source

Les informations sont issues du site Vite ma dose de vaccin !

// ----------
// CONSTANTES
// ----------
const WIDGET_FAMILY_SMALL = "small"
const WIDGET_FAMILY_MEDIUM = "medium"
const WIDGET_FAMILY_LARGE = "large"
// -------------
//
// CONFIGURATION
//
// -------------
// SEMI-AUTOMATIQUE
// 1. Lancer le script
// 2. Sélectionner votre département
// 2. Coller ci-dessous le code de votre département (remplacer le code actuel)
// MANUELLE
// Remplacer ci-dessous
// par le code de de votre département
//
// Par exemple, si votre code est 75
// const defaultDepCode = "75"
//
const defaultDepCode = undefined
// ------------------------
//
// NE PAS MODIFIER CI-APRÈS
//
// ------------------------
// Mode debug du widget
const debugWidget = false
// Widget family size
const debugWidgetFamily = WIDGET_FAMILY_MEDIUM
const deps = await retrieveDeps()
function depWithCode(deps, code) {
return deps.filter((dep) => {
return dep["code_departement"] == code
})[0]
}
async function retrieveDeps() {
var request = new Request("https://raw.githubusercontent.com/CovidTrackerFr/vitemadose/data-auto/data/output/departements.json")
request.headers = {
"Cache-Control": "no-cache"
}
return request
.loadJSON()
}
async function retrieveDep(code) {
var request = new Request("https://raw.githubusercontent.com/CovidTrackerFr/vitemadose/data-auto/data/output/" + code + ".json")
request.headers = {
"Cache-Control": "no-cache"
}
return request
.loadJSON()
}
function sortedDeps(json) {
if (json.centres_disponibles.length == 0) {
return []
}
return json.centres_disponibles.sort((a, b) => {
if (!a || !b) {
return 1
} else {
return Date(a.prochain_rdv) < Date(b.prochain_rdv) ? -1 : 1
}
})
}
function title(json) {
let items = json.centres_disponibles
if (!items || items.length == 0) {
return "💉 Aucun centre disponible"
} else if (items.length == 1) {
return "💉 Un centre disponible"
} else {
return "💉" + items.length + " centres disponibles"
}
}
function createSmallWidget(dep, json) {
let w = new ListWidget()
w.backgroundColor = Color.dynamic(Color.white(), new Color("#222"))
let titleTxt = w.addText(title(json))
titleTxt.font = Font.boldSystemFont(15)
titleTxt.textColor = Color.dynamic(Color.black(), Color.white())
let subtitleTxt = w.addText(dep.nom_departement)
subtitleTxt.font = Font.systemFont(15)
subtitleTxt.textColor = Color.dynamic(Color.black(), Color.white())
w.addSpacer()
let sourceTxt = w.addText("Vite ma dose de vaccin !")
sourceTxt.font = Font.boldSystemFont(9)
sourceTxt.textColor = Color.dynamic(Color.black(), Color.white())
sourceTxt.url = "https://covidtracker.fr/vitemadose/"
w.addSpacer(4)
let df = new DateFormatter()
df.locale = "fr-FR"
df.useShortDateStyle()
df.useShortTimeStyle()
let lastUpdate = new Date(json.last_updated)
let lastUpdateTxt = w.addText(df.string(lastUpdate))
lastUpdateTxt.font = Font.systemFont(10)
lastUpdateTxt.textColor = Color.dynamic(Color.black(), Color.white())
return w
}
function createMediumWidget(dep, json) {
let df = new DateFormatter()
df.locale = "fr-FR"
df.useShortDateStyle()
df.useShortTimeStyle()
let w = new ListWidget()
w.backgroundColor = Color.dynamic(Color.white(), new Color("#222"))
let titleTxt = w.addText(title(json))
titleTxt.font = Font.boldSystemFont(15)
titleTxt.textColor = Color.dynamic(Color.black(), Color.white())
let subtitleTxt = w.addText(dep.nom_departement)
subtitleTxt.font = Font.systemFont(15)
subtitleTxt.textColor = Color.dynamic(Color.black(), Color.white())
w.addSpacer()
let deps = sortedDeps(json)
if (deps.length > 0) {
let item = deps[0]
let stack = w.addStack()
stack.layoutVertically()
let txt = stack.addText(df.string(new Date(item.prochain_rdv)))
txt.font = Font.boldSystemFont(12)
txt.textColor = Color.dynamic(Color.black(), Color.white())
let subtxt = stack.addText(item.nom)
subtxt.font = Font.systemFont(12)
subtxt.textColor = Color.dynamic(Color.black(), Color.white())
stack.url = item.url
w.addSpacer()
}
let sourceTxt = w.addText("Vite ma dose de vaccin !")
sourceTxt.font = Font.boldSystemFont(10)
sourceTxt.textColor = Color.dynamic(Color.black(), Color.white())
sourceTxt.url = "https://covidtracker.fr/vitemadose/"
w.addSpacer(4)
let lastUpdate = new Date(json.last_updated)
let lastUpdateTxt = w.addText("Mise à jour : " + df.string(lastUpdate))
lastUpdateTxt.font = Font.systemFont(12)
lastUpdateTxt.textColor = Color.dynamic(Color.black(), Color.white())
return w
}
function createLargeWidget(dep, json) {
let df = new DateFormatter()
df.locale = "fr-FR"
df.useShortDateStyle()
df.useShortTimeStyle()
let w = new ListWidget()
w.backgroundColor = Color.dynamic(Color.white(), new Color("#222"))
let titleTxt = w.addText(title(json))
titleTxt.font = Font.boldSystemFont(15)
titleTxt.textColor = Color.dynamic(Color.black(), Color.white())
let subtitleTxt = w.addText(dep.nom_departement)
subtitleTxt.font = Font.systemFont(15)
subtitleTxt.textColor = Color.dynamic(Color.black(), Color.white())
w.addSpacer()
let deps = sortedDeps(json)
if (deps.length > 0) {
let stack = w.addStack()
stack.layoutVertically()
let item = deps[0]
let txt = stack.addText(df.string(new Date(item.prochain_rdv)))
txt.font = Font.boldSystemFont(12)
txt.textColor = Color.dynamic(Color.black(), Color.white())
let subtxt = stack.addText(item.nom)
subtxt.font = Font.systemFont(12)
subtxt.textColor = Color.dynamic(Color.black(), Color.white())
stack.url = item.url
if (deps.length > 1) {
w.addSpacer(10)
let stack = w.addStack()
stack.layoutVertically()
let item = deps[1]
let txt = stack.addText(df.string(new Date(item.prochain_rdv)))
txt.font = Font.boldSystemFont(12)
txt.textColor = Color.dynamic(Color.black(), Color.white())
let subtxt = stack.addText(item.nom)
subtxt.font = Font.systemFont(12)
subtxt.textColor = Color.dynamic(Color.black(), Color.white())
stack.url = item.url
}
if (deps.length > 2) {
w.addSpacer(10)
let stack = w.addStack()
stack.layoutVertically()
let item = deps[2]
let txt = stack.addText(df.string(new Date(item.prochain_rdv)))
txt.font = Font.boldSystemFont(12)
txt.textColor = Color.dynamic(Color.black(), Color.white())
let subtxt = stack.addText(item.nom)
subtxt.font = Font.systemFont(12)
subtxt.textColor = Color.dynamic(Color.black(), Color.white())
stack.url = item.url
}
if (deps.length > 3) {
w.addSpacer(10)
let stack = w.addStack()
stack.layoutVertically()
let item = deps[3]
let txt = stack.addText(df.string(new Date(item.prochain_rdv)))
txt.font = Font.boldSystemFont(12)
txt.textColor = Color.dynamic(Color.black(), Color.white())
let subtxt = stack.addText(item.nom)
subtxt.font = Font.systemFont(12)
subtxt.textColor = Color.dynamic(Color.black(), Color.white())
stack.url = item.url
}
if (deps.length > 4) {
w.addSpacer(10)
let stack = w.addStack()
stack.layoutVertically()
let item = deps[4]
let txt = stack.addText(df.string(new Date(item.prochain_rdv)))
txt.font = Font.boldSystemFont(12)
txt.textColor = Color.dynamic(Color.black(), Color.white())
let subtxt = stack.addText(item.nom)
subtxt.font = Font.systemFont(12)
subtxt.textColor = Color.dynamic(Color.black(), Color.white())
stack.url = item.url
}
w.addSpacer()
}
let sourceTxt = w.addText("Vite ma dose de vaccin !")
sourceTxt.font = Font.boldSystemFont(10)
sourceTxt.textColor = Color.dynamic(Color.black(), Color.white())
sourceTxt.url = "https://covidtracker.fr/vitemadose/"
w.addSpacer(4)
let lastUpdate = new Date(json.last_updated)
let lastUpdateTxt = w.addText("Mise à jour : " + df.string(lastUpdate))
lastUpdateTxt.font = Font.systemFont(12)
lastUpdateTxt.textColor = Color.dynamic(Color.black(), Color.white())
return w
}
function presentList(dep, json) {
let table = new UITable()
let df = new DateFormatter()
df.locale = "fr-FR"
df.useShortDateStyle()
df.useShortTimeStyle()
let items = sortedDeps(json)
let trow = new UITableRow()
let ttxt = trow.addText(title(json), dep.nom_departement)
trow.height = 100
trow.cellSpacing = 10
table.addRow(trow)
for (item of items) {
let row = new UITableRow()
let txt = row.addText(df.string(new Date(item.prochain_rdv)) + " - via " + item.plateforme, item.nom)
row.onSelect = (idx) => {
Safari.open(items[idx].url)
}
row.height = 80
row.cellSpacing = 10
table.addRow(row)
}
let row = new UITableRow()
let txt = row.addText("Vite ma dose de vaccin !", df.string(new Date(json.last_updated)))
row.onSelect = (idx) => {
Safari.open("https://covidtracker.fr/vitemadose/")
}
row.height = 100
row.cellSpacing = 10
table.addRow(row)
QuickLook.present(table)
}
function presentDepsList(deps) {
let table = new UITable()
let trow = new UITableRow()
let ttxt = trow.addText("Configuration du code de votre département", "Collez le code à la place de \"const defaultDepCode = undefined\" au début du script")
trow.height = 100
trow.cellSpacing = 10
table.addRow(trow)
for (item of deps) {
let row = new UITableRow()
let txt = row.addText(item.code_departement + " - " + item.nom_departement, "const defaultDepCode = \"" + item.code_departement + "\"")
row.onSelect = (idx) => {
let code = deps[idx].code_departement
Pasteboard.copyString("const defaultDepCode = \"" + code + "\"")
}
row.height = 80
row.cellSpacing = 10
table.addRow(row)
}
let row = new UITableRow()
let txt = row.addText("Le code sera copié dans le presse-papier")
row.height = 100
row.cellSpacing = 10
table.addRow(row)
QuickLook.present(table)
}
if (config.runsInWidget) {
var depCode = args.widgetParameter
if (!depCode) {
depCode = defaultDepCode
}
let json = await retrieveDep(depCode)
switch (config.widgetFamily) {
case WIDGET_FAMILY_SMALL: {
let widget = createSmallWidget(depWithCode(deps, depCode), json)
Script.setWidget(widget)
break
}
case WIDGET_FAMILY_MEDIUM: {
let widget = createMediumWidget(depWithCode(deps, depCode), json)
Script.setWidget(widget)
break
}
case WIDGET_FAMILY_LARGE: {
let widget = createLargeWidget(depWithCode(deps, depCode), json)
Script.setWidget(widget)
}
}
} else if (config.runsInApp) {
if (!defaultDepCode) {
presentDepsList(deps)
} else {
let json = await retrieveDep(defaultDepCode)
if (debugWidget) {
switch(debugWidgetFamily) {
case WIDGET_FAMILY_SMALL: {
let widget = createSmallWidget(depWithCode(deps, defaultDepCode), json)
widget.presentSmall()
break
}
case WIDGET_FAMILY_MEDIUM: {
let widget = createMediumWidget(depWithCode(deps, defaultDepCode), json)
widget.presentMedium()
break
}
default: {
let widget = createLargeWidget(depWithCode(deps, defaultDepCode), json)
widget.presentLarge()
}
}
} else {
presentList(depWithCode(deps, defaultDepCode), json)
}
}
} else if (config.runsWithSiri) {
let json = await retrieveDep(defaultDepCode).slice(0, 5)
presentList(depWithCode(deps, defaultDepCode), json)
}
Script.complete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment