-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
263 lines (245 loc) · 6.69 KB
/
collector.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
package main
import (
"time"
"encoding/json"
"log"
"net/http"
"net/url"
"fmt"
"strings"
"strconv"
"math/rand"
)
type Traveller struct {
Bc uint8 `json:"bc"`
Typ string `json:"typ"`
Alter uint8 `json:"alter"`
}
type DBQuery struct {
S int `json:"s"`
D int `json:"d"`
Dt string `json:"dt"`
T string `json:"t"`
C uint8 `json:"c"`
WithoutICE bool `json:"ohneICE"`
Tct int `json:"tct"`
Dur int `json:"dur"`
Travellers []Traveller `json:"travellers"`
Sv bool `json:"sv"`
V string `json:"v"`
Dir string `json:"dir"`
Bic bool `json:"bic"`
Device string `json:"device"`
Os string `json:"os"`
}
type Offer struct {
T string `json:"t"`
C string `json:"c"`
P string `json:"p"`
Tt string `json:"tt"`
Zb string `json:"zb"`
Arq string `json:"arq"`
Ff string `json:"ff"`
Aix string `json:"aix"`
Sids []string `json:"sids"`
// RisIDs []string `json:"risids"` //?
Pky string `json:"pky"`
Angnm string `json:"angnm"`
KoTxt string `json:"kotxt"`
// ueid string `json:"ueid"`
// uepr1 string `json:"uepr1"`
// uepr2 string `json:"uepr2"`
// uepr3 string `json:"uepr3"`
// uentg string `json:"uentg"`
}
type DBTime struct {
Day string `json:"d"`
Time string `json:"t"`
UTC string `json:"m"`
}
type Train struct {
Tid string `json:"tid"`
Lt string `json:"lt"`
LtShort string `json:"ltShort"`
S string `json:"s"`
Sn string `json:"sn"`
D string `json:"d"`
Dn string `json:"dn"`
Tn string `json:"tn"`
Eg string `json:"eg"`
Dep DBTime `json:"dep"`
Arr DBTime `json:"arr"`
Pd string `json:"pd"`
Pa string `json:"pa"`
Rp bool `json:"rp"`
Re bool `json:"re"`
Sp bool `json:"sp"`
}
type TrainConnection struct {
Dir string `json:"dir"`
SID string `json:"sid"`
Dt string `json:"dt"`
Dur string `json:"dur"`
Nt string `json:"nt"`
NRConn bool `json:"NZVerb"`
Eg string `json:"eg"`
Trains []Train `json:"trains"`
}
type PeText struct {
Name string `json:"name"`
Text string `json:"hinweis"`
}
type Station struct {
Number string `json:"nummer"`
Name string `json:"name"`
}
type DBResponse struct {
Dir string `json:"dir"`
Offers map[string]Offer `json:"angebote"`
Connections map[string]TrainConnection `json:"verbindungen"`
PeTexts map[string]PeText `json:"peTexte"`
Sbf []Station `json:"sbf"`
Dbf []Station `json:"dbf"`
Durs map[string]string `json:"durs"`
Prices map[string]string `json:"prices"`
Sp bool `json:"sp"`
Device string `json:"device"`
}
var stations = map[string]int{
"Frankfurt(Main)Hbf": 8000105,
"Berlin Hbf (tief)": 8098160,
"Hamburg Hbf": 8098549,
"Muenchen Hbf": 8000261,
"Dresden Hbf": 8010085,
"Erfurt Hbf": 8010101,
}
func newQuery(from, to int, t time.Time) DBQuery {
tlers := [1]Traveller{{
Bc: 0, //no bahncard
Typ: "E", //adult
Alter: 25, //age=25
}}
return DBQuery{
S: from, //start station id
D: to, //dest station id
Dt: t.Format("02.01.06"), //date in DD.MM.YY
T: t.Format("15:04"), //time in HH:mm
C: 2, //class
WithoutICE: false, //without ICE (express train)
Tct: 5, //transfer time
Dur: 1440, //search within t+dur in minutes
Travellers: tlers[:],
Sv: true, //prefer fast routes
V: "16040000", //fixed
Dir: "1", //fixed
Bic: false, //fixed
Device: "HANDY", //fixed
Os: "iOS_9.3.1", //fixed
}
}
func getConnections(from, to string, day time.Time) *DBResponse {
day = day.Add(-12 * time.Hour).Round(24 * time.Hour) //round to full day
q := newQuery(stations[from], stations[to], day)
endpoint := "http://ps.bahn.de/preissuche/preissuche/psc_service.go" //https is slow
b, err := json.Marshal(q)
if err != nil {
log.Fatal("geht nich,JSONify:", err)
}
values := url.Values{}
values.Add("lang", "en")
values.Add("service", "pscangebotsuche")
values.Add("data", string(b))
rq, e := http.NewRequest(http.MethodGet, endpoint+"?"+values.Encode(), nil)
if e != nil {
log.Fatal("geht nich,Request:", e)
}
rq.Header.Add("Accept", "application/json")
resp, err0 := http.DefaultClient.Do(rq)
if err0 != nil {
log.Fatal("geht nich,RequestDo:", err0)
}
decoder := json.NewDecoder(resp.Body)
r := DBResponse{}
err1 := decoder.Decode(&r)
if err1 != nil {
log.Fatal("geht nich,RespDecode:", err1)
}
return &r
}
type SlimConnection struct {
now int64
s string
ss string
d string
ds string
dep int64
arr int64
price float32
transfers uint8
}
func NewSlimConnection(offer *Offer, conn TrainConnection) *SlimConnection {
var first, last Train
for i, t := range conn.Trains {
if i != 0 {
if strings.Compare(t.Dep.UTC, first.Dep.UTC) < 0 {
first = t
}
if strings.Compare(t.Arr.UTC, last.Arr.UTC) > 0 {
last = t
}
} else {
first = t
last = t
}
}
dep, _ := strconv.ParseInt(first.Dep.UTC, 10, 64)
arr, _ := strconv.ParseInt(first.Arr.UTC, 10, 64)
price, _ := strconv.ParseFloat(strings.Replace(offer.P, ",", ".", -1), 32)
return &SlimConnection{
now: time.Now().UTC().Unix(),
s: first.S,
ss: first.Sn,
d: last.D,
ds: last.Dn,
dep: dep / 1000,
arr: arr / 1000,
price: float32(price),
transfers: uint8(len(conn.Trains) - 1),
}
}
func main() {
d := [3][2]string{
{"Frankfurt(Main)Hbf", "Dresden Hbf"},
{"Berlin Hbf (tief)", "Muenchen Hbf"},
{"Hamburg Hbf", "Erfurt Hbf"},
}
t, e := time.Parse("02.01.2006 15:04", "16.07.2018 01:00")
if e != nil {
log.Fatal("timeerr:", e)
}
for _, city := range d {
//wait for 1-4 minutes
time.Sleep(time.Minute + time.Duration(rand.Intn(int(time.Minute)*3)))
for i := time.Duration(0); i < 7; i++ {
time.Sleep(3 * time.Second)
r := getConnections(city[0], city[1], t.Add(time.Hour*24*i))
for _, o := range r.Offers {
for _, sid := range o.Sids {
sc := NewSlimConnection(&o, r.Connections[sid])
fmt.Printf("%d,%d,%d,%d,%d,%.2f,%s,%s,%s,%s,%d\n",
stations[city[0]],
stations[city[1]],
sc.dep,
sc.arr,
sc.now,
sc.price,
sc.s,
sc.d,
sc.ss,
sc.ds,
sc.transfers)
}
}
}
}
}