-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscriptable-project-state.js
88 lines (75 loc) · 2.31 KB
/
scriptable-project-state.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: chart-bar;
const projectSlug = "achteintel-litfassmuseum"
const url = "https://wemakeit.com/projects/" + projectSlug + ".xml"
const locale = "de"
const request = new Request(url)
let response = await request.loadString()
let elementName = ""
let currentValue = null
let title = null
let items = []
let currentItem = null
const xmlParser = new XMLParser(response)
const widget = new ListWidget()
xmlParser.didStartElement = name => {
currentValue = ""
if (name == "project") {
currentItem = {}
}
}
xmlParser.didEndElement = name => {
const hasItem = currentItem != null
if (hasItem && name == "currency") {
currentItem["currency"] = currentValue
}
if (hasItem && name == locale && title == null) {
currentItem["projectName"] = currentValue
title = currentValue
}
if (hasItem && name == "goal") {
currentItem["goal"] = parseInt(currentValue, 10)
}
if (hasItem && name == "pledged-amount") {
currentItem["pledgedAmount"] = parseInt(currentValue,10)
}
if (hasItem && name == "ended-at") {
currentItem["endedAt"] = currentValue
}
if (hasItem && name == "backers-count") {
currentItem["backersCount"] = parseInt(currentValue,10)
}
if (name == "project") {
items.push(currentItem)
currentItem = {}
}
}
xmlParser.foundCharacters = str => {
currentValue += str
}
xmlParser.didEndDocument = () => {
let backers = null
let percentReached = null
let amountReached = null
let textToSpeak = null
let localeAppendix = null
let numberLocale = null
for (item of items) {
backers = item.backersCount + " Unterstützer*innen"
percentReached = item.pledgedAmount / item.goal * 100 + "%"
if (item.currency == 'EUR') {
localeAppendix = 'DE'
} else {
localeAppendix = 'CH'
}
numberLocale = locale + '-' + localeAppendix
amountReached = new Intl.NumberFormat(numberLocale, {style: 'currency', currency: item.currency }).format(item.pledgedAmount)
}
textToSpeak = "Dein Projekt hat " + percentReached + " mit der Hilfe von " + backers + " erreicht, das sind " + amountReached
QuickLook.present(textToSpeak)
if (config.runsWithSiri) {
Speech.speak(textToSpeak)
}
}
xmlParser.parse()