forked from schachmat/mvgo
/
mvgo.go
150 lines (137 loc) · 3.22 KB
/
mvgo.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
package main
import (
"bytes"
"fmt"
"strings"
// "io/ioutil"
"log"
"os"
"go/build"
"net/http"
"net/url"
"code.google.com/p/go-charset/charset"
// "code.google.com/p/go-charset/charsets"
"code.google.com/p/go.net/html"
hcs "code.google.com/p/go.net/html/charset"
)
const uri = "http://www.mvg-live.de/ims/dfiStaticAuswahl.svc?"
var params = []string{}
var gotstation = false
func parse_arg(t string) {
if t[0] == '-' {
for i := 1; i < len(t); i++ {
if t[i] == 'b' {
params = append(params, "bus=checked")
} else if t[i] == 's' {
params = append(params, "sbahn=checked")
} else if t[i] == 't' {
params = append(params, "tram=checked")
} else if t[i] == 'u' {
params = append(params, "ubahn=checked")
}
}
} else if !gotstation {
buf := new(bytes.Buffer)
w, err := charset.NewWriter("latin1", buf)
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, t)
w.Close()
fmt.Printf("%q\n", buf.Bytes())
params = append(params, "haltestelle=" + url.QueryEscape(string(buf.Bytes())))
gotstation = true
}
}
//func find_first(n *html.Node, tag atom.Atom, key, val string) *html.Node {
//}
func parse_departure_line(n *html.Node) {
var line, station, departure string
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type != html.ElementNode || c.Data != "td" {
continue
}
for _, a := range c.Attr {
if a.Key != "class" {
continue
}
if a.Val == "lineColumn" {
line = c.FirstChild.Data
break
}
if a.Val == "stationColumn" {
station = strings.TrimSpace(c.FirstChild.Data)
break
}
if a.Val == "inMinColumn" {
departure = c.FirstChild.Data
break
}
}
}
fmt.Println(line, station, departure)
}
func parse_departures(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "tr" {
for _, a := range n.Attr {
if a.Key == "class" && (a.Val == "rowOdd" || a.Val == "rowEven") {
parse_departure_line(n)
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
parse_departures(c)
}
}
func parse_stations(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "a" {
for _, a := range n.Attr {
if a.Key == "href" && strings.HasPrefix(a.Val, "/ims/dfiStatic") {
fmt.Println(a.Val, n.FirstChild.Data)
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
parse_stations(c)
}
}
func find_table(n *html.Node) *html.Node {
if n.Type == html.ElementNode && n.Data == "table" {
for _, a := range n.Attr {
if a.Key == "class" && a.Val == "departureTable departureView" {
parse_departures(n)
return n
} else if a.Key == "class" && a.Val == "departureTable header" {
parse_stations(n)
return n
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
t := find_table(c)
if t != nil {
return t
}
}
return nil
}
func main() {
charset.CharsetDir = build.Default.GOPATH + "/src/code.google.com/p/go-charset/datafiles"
for i := 1; i < len(os.Args); i++ {
parse_arg(os.Args[i])
}
res, err := http.Get(uri + strings.Join(params, "&"))
defer res.Body.Close()
if err != nil {
log.Fatal(err)
}
r, err := hcs.NewReader(res.Body, res.Header["Content-Type"][0])
if err != nil {
log.Fatal(err)
}
doc, err := html.Parse(r)
if err != nil {
log.Fatal(err)
}
find_table(doc)
}