-
Notifications
You must be signed in to change notification settings - Fork 1
/
fraijanes.go
72 lines (62 loc) · 1.47 KB
/
fraijanes.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
package multasgt
import (
"net/http"
"net/url"
"fmt"
"github.com/PuerkitoBio/goquery"
)
// Ensure interface implementation.
var _ TicketChecker = &Fraijanes{}
const (
fraijanesURL = "http://190.149.238.69/consultapmt/daemonpt.php"
fraijanesEntity = "FRAIJANES"
)
// Fraijanes implementation.
type Fraijanes struct {
Client *http.Client
}
// Check retrieves all tickes and aditional information.
func (f *Fraijanes) Check(plateType, plateNumber string) ([]Ticket, error) {
resp, err := f.Client.PostForm(fraijanesURL, url.Values{
"cbxtipo": {plateType},
"txtidplaca": {plateNumber},
"cmdsmpmt": {"Consultar"},
})
if err != nil {
return nil, err
}
var tickets []Ticket
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
return nil, err
}
rows := doc.Find("table tbody tr")
rl := rows.Length()
rows.EachWithBreak(func(idx int, s *goquery.Selection) bool {
if idx == 0 {
return true
}
if rl < 3 || idx >= rl-2 {
return false
}
ticket := Ticket{Entity: fraijanesEntity}
ticket.Info = ""
s.Find("td").Each(func(i int, sel *goquery.Selection) {
switch i {
case 0:
ticket.ID = CleanStrings(sel.Text())
case 2:
ticket.Info = CleanStrings(sel.Text())
case 3:
ticket.Info = fmt.Sprintf("%v, %v", ticket.Info, CleanStrings(sel.Text()))
case 4:
val := CleanStrings(sel.Text())
ticket.Amount = val
ticket.Total = val
}
})
tickets = append(tickets, ticket)
return true
})
return tickets, nil
}