-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMykiBalance.js
238 lines (199 loc) · 7.21 KB
/
MykiBalance.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* Myki Balance V2.0
* Run on Scriptable
* Created by Ricky Li on 2020/10/12
* Last modified on 2022/12/09
* https://github.com/imchlorine/MykiBalance.git
*
* This Script is used for feching Myki Balance from MyKi website.
*
* For fetching PTV Timetable Widget visit:
* https://github.com/imchlorine/PTVTimetable.git
*
* The passenger type (Concession, Child etc) may not accurate, cos I only have a full fare Card.
* Important! This Widget is not affiliated to PTV or Myki. For personal use only.
*/
// ================================
// ================================
// Do not edit the code below
let cardNumber = args.widgetParameter
//let cardNumber = "308420123456789"
let ptvWebAuth = await getMykitoken()
console.log(ptvWebAuth)
let card = await getCard()
let widget = await createWidget(card)
if (!config.runsInWidget) {
await widget.presentMedium()
}
Script.setWidget(widget)
Script.complete()
async function createWidget(card) {
let widget = new ListWidget()
if (card["code"] != 1) {
let alertMessage = widget.addText(card["message"])
alertMessage.font = new Font("AppleSDGothicNeo-bold", 14)
alertMessage.textColor = new Color("#ff0000")
return widget
}
card = card["data"][0]
let currentTime = new Date()
let df = new DateFormatter()
df.useMediumDateStyle()
df.useShortTimeStyle()
let dateTxt = df.string(currentTime)
let mykiBalance = card["mykiBalance"]
// force balance to be 2 decimal places
let mykiBalanceValue = parseFloat(mykiBalance).toFixed(2).toString()
let balanceSign = mykiBalance < 0 ? "- $" : "$"
let balanceTxt = balanceSign + mykiBalance.replace(/-/, "")
let mykiPass = card["Product"] || []
if (config.runsWithSiri) {
Speech.speak("You have " + balanceTxt + " left in your Myki money!")
if (mykiPass.length > 0) {
Speech.speak("You have " + mykiPass[0]["daysRemaining"] + " days remaining in your Myki pass!")
}
}
let passengerCode = card["passengerCode"]
let passengerTxt = ""
switch (passengerCode) {
case 1: // Verified
passengerTxt = "Full Fare"
break;
case 2: // Unverified
passengerTxt = "Concession"
break;
case 3: // Unverified
passengerTxt = "Child"
break;
case 4: // Unverified
passengerTxt = "Senior"
break;
case 7: // Verified
passengerTxt = "Student Concession"
default:
passengerTxt = "Full Fare"
break;
}
// Set gradient background
let startColor = new Color("333434")
let midColor = new Color("333434")
let endColor = new Color("#ffffff")
let gradient = new LinearGradient()
gradient.colors = [startColor, midColor, endColor]
gradient.locations = [0.0, 0.72, 0.721]
widget.backgroundGradient = gradient
widget.addSpacer()
let mykiUpdateTime = widget.addStack()
let updateTxt = mykiUpdateTime.addText("")
// updateTxt.font = new Font("AppleSDGothicNeo-bold", 10)
updateTxt.font = new Font("AppleSDGothicNeo-Light", 10)
updateTxt.textColor = new Color("#eeeeee")
let updateTime = mykiUpdateTime.addText(dateTxt)
updateTime.font = new Font("AppleSDGothicNeo-bold", 10)
updateTime.textColor = new Color("#eeeeee")
let mykiTitle = widget.addStack()
mykiTitle.centerAlignContent()
let mykiLogo = mykiTitle.addText("•••• " + cardNumber.slice(-5, -1) + " " + cardNumber.slice(-1))
mykiLogo.font = new Font("AppleSDGothicNeo-bold", 16)
mykiLogo.textColor = new Color("#ffffff")
mykiTitle.addSpacer()
let mykiCode = mykiTitle.addText("Top Up")
let mykiSymbol = mykiTitle.addText(" >")
mykiCode.font = new Font("AppleSDGothicNeo-bold", 16)
mykiSymbol.font = new Font("AppleSDGothicNeo-bold", 16)
mykiSymbol.textColor = new Color("#d92b26")
mykiCode.textColor = new Color("#ffffff")
mykiCode.url = "googlechrome://www.ptv.vic.gov.au/tickets/myki/#topup"
widget.addSpacer()
if (mykiPass.length > 0) {
let middleViewTitle = widget.addStack()
let moneyTitle = middleViewTitle.addText("myki money")
moneyTitle.font = new Font("AppleSDGothicNeo-bold", 12)
moneyTitle.textColor = new Color("#eeeeee")
middleViewTitle.addSpacer()
let passTitle = middleViewTitle.addText("myki pass")
passTitle.font = new Font("AppleSDGothicNeo-bold", 12)
passTitle.textColor = new Color("#eeeeee")
}
let middleView = widget.addStack()
let balanceTitle = middleView.addText(balanceTxt)
balanceTitle.font = new Font("AppleSDGothicNeo-bold", 40)
balanceTitle.textColor = new Color("#c2d840")
middleView.addSpacer()
if (mykiPass.length > 0) {
let daysRemaining = mykiPass[0]["daysRemaining"].toString()
let daysRemainingTitle = middleView.addText(daysRemaining)
daysRemainingTitle.font = new Font("AppleSDGothicNeo-bold", 40)
daysRemainingTitle.textColor = new Color("#eeeeee")
}
middleView.bottomAlignContent()
widget.addSpacer()
let bottomView = widget.addStack()
let expireText = bottomView.addText("Expiry: ")
expireText.textColor = new Color("#000000")
expireText.font = new Font("AppleSDGothicNeo-regular", 10)
expire_df = new DateFormatter()
expire_df.useMediumDateStyle()
let expireDateStr = expire_df.string(new Date(card["mykiCardExpiryDate"]))
let expireDate = bottomView.addText(expireDateStr)
expireDate.font = new Font("AppleSDGothicNeo-bold", 10)
expireDate.textColor = new Color("#000000")
bottomView.addSpacer()
addSymbol({
symbol: 'tram.fill',
stack: bottomView,
})
addSymbol({
symbol: 'tram',
stack: bottomView,
})
addSymbol({
symbol: 'bus',
stack: bottomView,
})
bottomView.addSpacer()
let travelType = bottomView.addText(passengerTxt)
travelType.font = new Font("AppleSDGothicNeo-bold", 10)
travelType.textColor = new Color("#000000")
widget.addSpacer()
return widget
}
function addSymbol({
symbol = 'applelogo',
stack,
color = Color.black(),
size = 12,
}) {
const _sym = SFSymbol.named(symbol)
const wImg = stack.addImage(_sym.image)
wImg.tintColor = color
wImg.imageSize = new Size(size, size)
}
async function getMykitoken() {
let url = "https://www.ptv.vic.gov.au/tickets/myki"
let req = new Request(url)
let result = await req.loadString()
let matchToken = result.match(/"mykiToken":"([^"]+)","mykiTime":([0-9]+)+/)
let mykiToken = matchToken[1].replace(/\\\//g, "/")
let mykiTime = matchToken[2]
return mykiTime + "-" + mykiToken
}
async function getCard() {
let url = "https://mykiapi.ptv.vic.gov.au/v2/myki/card"
let req = new Request(url)
req.method = "POST"
let defaultHeaders = {
Accept: "application/json",
"Content-Type": "application/json"
}
authHeader = { "x-ptvwebauth": ptvWebAuth }
req.headers = {
...defaultHeaders,
...authHeader
}
let data = { "0": { "mykiCardNumber": cardNumber } }
req.body = JSON.stringify(data)
let result = await req.loadJSON()
log(result)
return result
}