-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
232 lines (189 loc) · 5.02 KB
/
main.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
package swrailway
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
type (
obj map[string]interface{}
stationStruct struct {
ID string `json:"id"`
Info string `json:"info"`
Label string `json:"label"`
}
sheduleStruct struct {
ID string // td[0]
Period string // td[1]
Route string // td[2]
ArrivalFrom string // td[3]
ArrivalStationFrom string // td[4]
DepartureFrom string // td[5]
ArrivalTo string // td[6]
ArrivalStationTo string // td[7]
DepartureTo string // td[8]
TimeInTrip string // td[9]
Distance string // td[10]
ActiveFrom string // td[11]
ActiveTo string // td[12]
}
)
func GetStation(id, lang string) stationStruct {
return apiGetStation(obj{
"JSON": "station",
"lng": lang,
"id": id,
})
}
func GetStations(name, lang string) []stationStruct {
return apiGetStations(obj{
"JSON": "station",
"lng": lang,
"term": name,
})
}
func GetShedule(date, lang, from, to string, onlyRemaining bool) []sheduleStruct {
dateR := "0"
if onlyRemaining {
dateR = "1"
}
ret := parseShedule(apiGetShedule(obj{
"sid1": from,
"sid2": to,
"startPicker2": date,
"dateR": dateR, // 0 - all days, 1 - today
"lng": lang, // _ru, _ua, _en
}))
if onlyRemaining {
ret = removeMissed(ret)
}
return ret
}
func removeMissed(shedule []sheduleStruct) []sheduleStruct {
var ret []sheduleStruct
loc, err := time.LoadLocation("Europe/Kiev")
if err != nil {
panic(err)
}
hoursNow := time.Now().In(loc).Format("15")
minutesNow := time.Now().In(loc).Format("04")
for _, s := range shedule {
splitedDepartureFrom := strings.Split(s.DepartureFrom, ":")
if hoursNow < splitedDepartureFrom[0] ||
(hoursNow == splitedDepartureFrom[0] && minutesNow < splitedDepartureFrom[1]) {
ret = append(ret, s)
}
}
return ret
}
func parseShedule(body []byte) []sheduleStruct {
var ret []sheduleStruct
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(body))
if err != nil {
log.Fatal(err)
}
selector := "#geo2g > tbody"
doc.Find(selector).Each(func(i int, s *goquery.Selection) {
s.Find("tr:not(.pix)").Each(func(j int, row *goquery.Selection) {
if j > 2 {
var tmp sheduleStruct
row.Find("td").Each(func(k int, col *goquery.Selection) {
switch k {
case 0:
tmp.ID = strings.TrimSpace(col.Text())
case 1:
tmp.Period = strings.TrimSpace(col.Text())
case 2:
tmp.Route = strings.TrimSpace(col.Text())
case 3:
tmp.ArrivalFrom = strings.TrimSpace(col.Text())
case 4:
tmp.ArrivalStationFrom = strings.TrimSpace(col.Text())
case 5:
tmp.DepartureFrom = strings.TrimSpace(col.Text())
case 6:
tmp.ArrivalTo = strings.TrimSpace(col.Text())
case 7:
tmp.ArrivalStationTo = strings.TrimSpace(col.Text())
case 8:
tmp.DepartureTo = strings.TrimSpace(col.Text())
case 9:
tmp.TimeInTrip = strings.TrimSpace(col.Text())
case 10:
tmp.Distance = strings.TrimSpace(col.Text())
case 11:
tmp.ActiveFrom = strings.TrimSpace(col.Text())
case 12:
tmp.ActiveTo = strings.TrimSpace(col.Text())
}
})
if tmp.ID != "" {
ret = append(ret, tmp)
}
}
})
})
return ret
}
// http://swrailway.gov.ua/timetable/eltrain/?JSON=station&lng=_ua&id=88
func apiGetStation(request obj) stationStruct {
if request["lng"] == "_ua" {
request["lng"] = ""
}
var result stationStruct
ret := apiRequest("GET", request)
json.Unmarshal(ret, &result)
return result
}
// http://swrailway.gov.ua/timetable/eltrain/?JSON=station&lng=_ua&term=%D0%A1%D0%B2%D1%8F
func apiGetStations(request obj) []stationStruct {
if request["lng"] == "_ua" {
request["lng"] = ""
}
var result []stationStruct
ret := apiRequest("GET", request)
json.Unmarshal(ret, &result)
return result
}
// http://swrailway.gov.ua/timetable/eltrain/?sid1=85&sid2=88&startPicker2=2018-07-18&dateR=0&lng=_ua
func apiGetShedule(request obj) []byte {
return apiRequest("GET", request)
}
func apiRequest(httpMethod string, request obj) []byte {
var requestStr string
for k, v := range request {
requestStr = requestStr + fmt.Sprintf("&%v=%v", k, v)
}
if len(requestStr) > 0 {
requestStr = "?" + requestStr[1:len(requestStr)]
}
reqURL := "https://swrailway.gov.ua/timetable/eltrain/" + requestStr
req, err := http.NewRequest(httpMethod, reqURL, bytes.NewBuffer([]byte{}))
req.Header.Set("Upgrade-Insecure-Requests", "1")
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36")
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := &http.Client{
Transport: tr,
}
resp, err := client.Do(req)
if err != nil {
log.Println(err.Error())
return []byte{}
}
defer resp.Body.Close()
result, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode >= 300 {
log.Println(string(result))
}
return result
}