forked from dsymonds/opal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
349 lines (309 loc) · 8.24 KB
/
parse.go
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package opal
import (
"bytes"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// Overview represents an overview of an Opal account.
type Overview struct {
Cards []Card
}
// Card represents a single Opal card.
type Card struct {
Name string // either a name or number
Balance int // in cents
}
var (
amountRE = regexp.MustCompile(`^(-?)\$(\d+)\.(\d\d)$`)
)
// parseAmount parses something matching amountRE and returns the number of cents.
func parseAmount(amt string) (int, error) {
m := amountRE.FindStringSubmatch(amt)
if m == nil {
return 0, fmt.Errorf("does not match /%v/", amountRE)
}
dollars, err := strconv.ParseInt(m[2], 10, 32)
if err != nil {
return 0, err
}
cents, _ := strconv.ParseInt(m[3], 10, 8) // can't fail; it's exactly two digits
x := int(dollars)*100 + int(cents)
if m[1] == "-" {
x = -x
}
return x, nil
}
// parseCard parses card info from the name and bal TDs.
func parseCard(name, bal string) (Card, error) {
// Cards can be renamed to almost anything.
balance, err := parseAmount(bal)
if err != nil {
return Card{}, fmt.Errorf("bad balance %q: %v", bal, err)
}
return Card{
Name: name,
Balance: balance,
}, nil
}
// parseOverview parses a page fetched from https://www.opal.com.au/registered/index.
func parseOverview(input []byte) (*Overview, error) {
// TODO: check input is UTF-8
doc, err := html.Parse(bytes.NewReader(input))
if err != nil {
return nil, err
}
activeTable := findByAttr(doc, "id", "dashboard-active-cards")
if activeTable == nil || activeTable.DataAtom != atom.Table {
return nil, errors.New("did not find active table")
}
tbody := findByDataAtom(activeTable, atom.Tbody)
if tbody == nil {
return nil, errors.New("did not find tbody")
}
var cardRows [][]string // one per row, each row having two elements (number and balance)
eachByAtom(tbody, atom.Tr, func(n *html.Node) bool {
var tds []string
// The card name is the first TD with a <label> inside it.
eachByAtom(n, atom.Td, func(n *html.Node) bool {
eachByAtom(n, atom.Label, func(n *html.Node) bool {
if len(tds) == 0 {
tds = append(tds, text(n))
}
return false
})
return false
})
// The balance is the first TD whose contents start with a dollar sign.
eachByAtom(n, atom.Td, func(n *html.Node) bool {
if t := text(n); strings.HasPrefix(t, "$") {
tds = append(tds, text(n))
}
return false
})
if len(tds) == 2 {
cardRows = append(cardRows, tds)
return false
}
return false
})
o := new(Overview)
for _, row := range cardRows {
card, err := parseCard(row[0], row[1])
if err != nil {
return nil, fmt.Errorf("parsing card row: %v", err)
}
o.Cards = append(o.Cards, card)
}
return o, nil
}
// Activity represents a subset of activity for a single card.
type Activity struct {
CardName string
Transactions []*Transaction
}
// Transaction represents a single transaction on a card.
// This may be a single journey, or a top-up.
// Not all fields may be set.
type Transaction struct {
Number int
When time.Time
Mode string // "train", etc., if known
Details string
JourneyNumber int // if known; numbered within the week
FareApplied string // e.g. "Off-peak", "Travel Reward"
Fare, Discount, Amount int // in cents
}
func (t *Transaction) String() string { return fmt.Sprintf("%+v", *t) }
func parseActivity(input []byte) (*Activity, error) {
// Collapse hyphenation before parsing the HTML.
input = bytes.Replace(input, []byte("­"), nil, -1)
// TODO: check input is UTF-8
doc, err := html.Parse(bytes.NewReader(input))
if err != nil {
return nil, err
}
table := findByAttr(doc, "id", "transaction-data")
if table == nil || table.DataAtom != atom.Table {
return nil, errors.New("did not find transaction table")
}
a := new(Activity)
// The <caption> of the table will look like
// <caption><span>My Opal activity: 314159</span></caption>
caption := findByDataAtom(table, atom.Caption)
if caption == nil || caption.FirstChild == nil {
return nil, errors.New("did not find caption")
}
a.CardName = text(caption.FirstChild)
if i := strings.Index(a.CardName, ":"); i >= 0 {
a.CardName = strings.TrimSpace(a.CardName[i+1:])
}
tbody := findByDataAtom(table, atom.Tbody)
if tbody == nil {
return nil, errors.New("did not find tbody")
}
eachByAtom(tbody, atom.Tr, func(n *html.Node) bool {
if err != nil {
return false
}
var t *Transaction
t, err = parseTransaction(n)
a.Transactions = append(a.Transactions, t)
return false
})
if err != nil {
return nil, err
}
return a, nil
}
func parseTransaction(n *html.Node) (*Transaction, error) {
// Collate all the <TD> contents.
var tds []string
for kid := n.FirstChild; kid != nil; kid = kid.NextSibling {
if kid.FirstChild != nil && kid.FirstChild.DataAtom == atom.Img {
// <td><img alt="train" ...></td>
tds = append(tds, attrVal(kid.FirstChild, "alt"))
continue
}
tds = append(tds, text(kid))
}
if len(tds) != 9 {
return nil, fmt.Errorf("transaction row with %d TDs, want 9", len(tds))
}
// Parse the transaction.
t := new(Transaction)
var err error
t.Number, err = parseDecimal(tds[0])
if err != nil {
return nil, fmt.Errorf("bad transaction number %q: %v", tds[0], err)
}
t.When, err = time.ParseInLocation("Mon 02/01/2006 15:04", tds[1], sydneyZone)
if err != nil {
return nil, fmt.Errorf("bad time %q: %v", tds[1], err)
}
t.Mode, t.Details = tds[2], strings.TrimSpace(tds[3])
t.FareApplied = strings.TrimSpace(tds[5])
// The rest are all optional.
fields := []struct {
index int
dst *int
parser func(string) (int, error)
name string
}{
{4, &t.JourneyNumber, parseDecimal, "journey number"},
{6, &t.Fare, parseAmount, "fare"},
{7, &t.Discount, parseAmount, "discount"},
{8, &t.Amount, parseAmount, "amount"},
}
for _, f := range fields {
if s := tds[f.index]; s != "" {
*f.dst, err = f.parser(s)
if err != nil {
return nil, fmt.Errorf("bad %s %q: %v", f.name, s, err)
}
}
}
return t, nil
}
var sydneyZone *time.Location // every time is in Australia/Sydney
func init() {
var err error
sydneyZone, err = time.LoadLocation("Australia/Sydney")
if err != nil {
panic(err)
}
}
func parseDecimal(s string) (int, error) {
s = strings.TrimSpace(s)
x, err := strconv.ParseInt(s, 10, 0)
return int(x), err
}
func parseLogin(input []byte) (token string, err error) {
// TODO: check input is UTF-8
doc, err := html.Parse(bytes.NewReader(input))
if err != nil {
return "", err
}
node := findByAttr(doc, "name", "CSRFToken")
if node == nil {
return "", errors.New("did not find CSRFToken <input>")
}
for _, attr := range node.Attr {
if attr.Key == "value" {
return attr.Val, nil
}
}
return "", fmt.Errorf("unexpected form of CSRFToken: %s", render(node))
}
func text(n *html.Node) string {
var bits []string
each(n, func(n *html.Node) bool {
if n.Type == html.TextNode {
bits = append(bits, n.Data)
}
return true
})
return strings.Join(bits, " ")
}
func attrVal(n *html.Node, key string) string {
for _, attr := range n.Attr {
if attr.Key == key {
return attr.Val
}
}
return ""
}
func findByAttr(n *html.Node, key, val string) *html.Node {
return find(n, func(n *html.Node) bool {
for _, attr := range n.Attr {
if attr.Key == key && attr.Val == val {
return true
}
}
return false
})
}
func findByDataAtom(n *html.Node, a atom.Atom) *html.Node {
return find(n, func(n *html.Node) bool {
return n.DataAtom == a
})
}
func eachByAtom(n *html.Node, a atom.Atom, f func(*html.Node) bool) {
each(n, func(n *html.Node) bool {
if n.DataAtom == a {
return f(n)
}
return true
})
}
// each calls f for each node from n downwards in a BFS order.
// If f returns false then that node's children will be skipped.
func each(n *html.Node, f func(*html.Node) bool) {
if !f(n) {
return
}
for kid := n.FirstChild; kid != nil; kid = kid.NextSibling {
each(kid, f)
}
}
func find(n *html.Node, pred func(*html.Node) bool) *html.Node {
if pred(n) {
return n
}
for kid := n.FirstChild; kid != nil; kid = kid.NextSibling {
if n := find(kid, pred); n != nil {
return n
}
}
return nil
}
func render(n *html.Node) string {
var buf bytes.Buffer
html.Render(&buf, n)
return buf.String()
}