Last active
October 28, 2020 22:02
-
-
Save darthsoup/e9b3b87a8854d090cadd8a44d075351c to your computer and use it in GitHub Desktop.
Scriptable EDG Dortmund
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: brown; icon-glyph: magic; | |
const API_URL = 'https://www.edg.de/JsonHandler.ashx?cmd=findtrash&feiertag=0' | |
const TITLE_ICON = '🗑️' | |
const TITLE = 'EDG' | |
const ENTRIES = 3 | |
const getTrashTypeColor = type => { | |
if (type === 'Wertstoffe') { | |
return Color.yellow() | |
} | |
if (type === 'Bioabfall') { | |
return Color.brown() | |
} | |
if (type === 'Altpapier') { | |
return Color.blue() | |
} | |
if (type === 'Restabfall') { | |
return Color.gray() | |
} | |
return | |
} | |
const addCollectionDate = (stack, element) => { | |
const { date, fraktion, wochentag } = element | |
const weekday = wochentag[0].toUpperCase() | |
const entryStack = stack.addStack() | |
entryStack.layoutVertically() | |
entryStack.useDefaultPadding() | |
const dateLabel = entryStack.addText(`${weekday} | ${date}`) | |
dateLabel.font = Font.mediumSystemFont(13) | |
dateLabel.minimumScaleFactor = 0.8 | |
const trashStack = entryStack.addStack() | |
trashStack.useDefaultPadding() | |
trashStack.layoutHorizontally() | |
fraktion.forEach(ft => { | |
const collect = trashStack.addText(`${ft} `) | |
collect.font = Font.systemFont(10) | |
collect.textColor = getTrashTypeColor(ft) | |
}) | |
stack.addSpacer() | |
} | |
let items = await loadCollectionDates() | |
let widget = await createWidget(items) | |
if (!config.runsInWidget) { | |
await widget.presentSmall() | |
} | |
Script.setWidget(widget) | |
Script.complete() | |
async function createWidget(items) { | |
const list = new ListWidget() | |
list.useDefaultPadding() | |
const bgGradient = new LinearGradient() | |
bgGradient.locations = [0, 1] | |
bgGradient.colors = Device.isUsingDarkAppearance() | |
? [new Color('111'), new Color('222')] | |
: [new Color('fff'), new Color('FAFAFA')] | |
list.backgroundGradient = bgGradient | |
try { | |
// Header | |
const headerRow = list.addStack() | |
headerRow.layoutHorizontally() | |
const headerLabel = headerRow.addText(TITLE) | |
headerLabel.font = Font.mediumSystemFont(13) | |
headerLabel.leftAlignText() | |
headerRow.addSpacer() | |
const headerIcon = headerRow.addText(TITLE_ICON) | |
headerIcon.font = Font.mediumSystemFont(12) | |
headerIcon.rightAlignText() | |
list.addSpacer() | |
// TrashElements | |
const listStack = list.addStack() | |
listStack.layoutVertically() | |
listStack.topAlignContent() | |
items.slice(0, ENTRIES).forEach(element => addCollectionDate(listStack, element)) | |
} catch(err) { | |
list.addText(err) | |
} | |
return list | |
} | |
async function loadCollectionDates() { | |
let fm = FileManager.local() | |
let dir = fm.documentsDirectory() | |
let path = fm.joinPath(dir, 'scriptable-edg.json') | |
let parameter = args.widgetParameter ? args.widgetParameter : 'Kampstr.,4' | |
let [ street, num ] = parameter.split(',') | |
street = encodeURIComponent(street) | |
num = encodeURIComponent(num) | |
let request = new Request(API_URL + `&street=${street}&nr=${num}`) | |
let data, fresh = 0 | |
try { | |
data = await request.loadJSON() | |
// Write JSON to iCloud file | |
fm.writeString(path, JSON.stringify(data, null, 2)) | |
fresh = 1 | |
return data.data | |
} catch (err) { | |
// Read data from iCloud file | |
let cache = JSON.parse(fm.readString(path), null) | |
return cache.data | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Scriptable iOS - EDG Dortmund Widget
Script lässt sich hinzufügen wie auch schon das Widget für Corona.
Damit die Straße gefunden wird, muss als Parameter eure Straße hinterlegt werden. Als Beispiel für die Saarlandstraße 3 dafür
Saarlandstr.,3
hinterlegen.Die passenden Straßennamen findet ihr auf https://www.edg.de/de/entsorgungsdienstleistungen/rein-damit/info-service.htm
Datenquelle
Für das Widget wird die API von der EDG genutzt.