-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbis.go
More file actions
574 lines (472 loc) · 19.8 KB
/
bis.go
File metadata and controls
574 lines (472 loc) · 19.8 KB
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package main
import (
"encoding/xml"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strings"
"sync"
)
const (
// CommonURL is a constant that stores the common URL of the restAPI.
CommonURL = "http://openapi.gbis.go.kr/ws/rest"
// BusStationURLPath is a constant that stores the URL Path to the bus station.
BusStationURLPath = "busstationservice"
// BusRouteURLPath is a constant that stores the URL Path to the bus route.
BusRouteURLPath = "busrouteservice"
// BusLocationURLPath is a constant that stores the URL Path to the bus location.
BusLocationURLPath = "buslocationservice"
// BusArrivalURLPath is a constant that stores the URL Path to the bus arrival.
BusArrivalURLPath = "busarrivalservice"
)
// ComMsgHeader is a structure that specifies the data format of the common header in the APIResponseBody.
type ComMsgHeader struct {
XMLName xml.Name `xml:"comMsgHeader"`
ErrMsg string `xml:"errMsg"`
ReturnCode int `xml:"returnCode"`
}
// MsgHeader is a structure that specifies the data format of the message header in the APIResponseBody.
type MsgHeader struct {
XMLName xml.Name `xml:"msgHeader"`
QueryTime string `xml:"queryTime"`
ResultCode int `xml:"resultCode"`
ResultMessage string `xml:"resultMessage"`
}
// StationResponseBody is a structure that specifies the data format to be responsed from the API.
type StationResponseBody struct {
XMLName xml.Name `xml:"response"`
ComMsgHeader ComMsgHeader `xml:"comMsgHeader"`
MsgHeader MsgHeader `xml:"msgHeader"`
MsgBody StationMsgBody `xml:"msgBody"`
}
// StationMsgBody is a structure that specifies the data format of the message body in the APIResponseBody.
type StationMsgBody struct {
XMLName xml.Name `xml:"msgBody"`
BusStationList BusStationList `xml:"busStationList"`
}
// BusStationList is an slice of BusStationes.
type BusStationList []BusStation
// BusStation is a structure that specifies the data format of the bus station in the MsgBody.
type BusStation struct {
XMLName xml.Name `xml:"busStationList" json:"-"`
CenterYn string `xml:"centerYn" json:"-"`
DistrictCd int `xml:"districtCd" json:"districtCd"`
MobileNo string `xml:"mobileNo" json:"stationNumber"`
RegionName string `xml:"regionName" json:"-"`
StationID string `xml:"stationId" json:"stationID"`
StationName string `xml:"stationName" json:"stationName"`
Y float32 `xml:"y" json:"longitude"`
X float32 `xml:"x" json:"latitude"`
StationDirect string `xml:"-" json:"stationDirect"`
}
// RouteResponseBody is a structure that specifies the data format to be responsed from the API.
type RouteResponseBody struct {
XMLName xml.Name `xml:"response"`
ComMsgHeader ComMsgHeader `xml:"comMsgHeader"`
MsgHeader MsgHeader `xml:"msgHeader"`
MsgBody RouteMsgBody `xml:"msgBody"`
}
// RouteMsgBody is a structure that specifies the data format of the message body in the APIResponseBody.
type RouteMsgBody struct {
XMLName xml.Name `xml:"msgBody"`
BusRouteList BusRouteList `xml:"busRouteList"`
}
// BusRouteList is an slice of BusRoutes.
type BusRouteList []BusRoute
// BusRoute is a structure that specifies the data format of the bus route in the MsgBody.
type BusRoute struct {
XMLName xml.Name `xml:"busRouteList" json:"-"`
DistrictCd int `xml:"districtCd" json:"districtCd"`
RegionName string `xml:"regionName" json:"regionName"`
RouteID string `xml:"routeId" json:"routeID"`
RouteName string `xml:"routeName" json:"routeNumber"`
RouteTypeName string `xml:"routeTypeName" json:"routeTypeName"`
StaOrder int `xml:"staOrder" json:"-"`
}
// RouteStationResponseBody is a structure that specifies the data format to be responsed from the API.
type RouteStationResponseBody struct {
XMLName xml.Name `xml:"response"`
ComMsgHeader ComMsgHeader `xml:"comMsgHeader"`
MsgHeader MsgHeader `xml:"msgHeader"`
MsgBody RouteStationMsgBody `xml:"msgBody"`
}
// RouteStationMsgBody is a structure that specifies the data format of the message body in the APIResponseBody.
type RouteStationMsgBody struct {
XMLName xml.Name `xml:"msgBody"`
BusRouteStationList BusRouteStationList `xml:"busRouteStationList"`
}
// BusRouteStationList is an slice of BusRouteStationes.
type BusRouteStationList []BusRouteStation
// BusRouteStation is a structure that specifies the data format of the bus route station in the MsgBody.
type BusRouteStation struct {
XMLName xml.Name `xml:"busRouteStationList" json:"-"`
CenterYn string `xml:"centerYn" json:"-"`
DistrictCd int `xml:"districtCd" json:"-"`
MobileNo string `xml:"mobileNo" json:"stationNumber"`
RegionName string `xml:"regionName" json:"-"`
StationID string `xml:"stationId" json:"stationID"`
StationName string `xml:"stationName" json:"stationName"`
X float32 `xml:"x" json:"-"`
Y float32 `xml:"y" json:"-"`
StationSeq int `xml:"stationSeq" json:"stationSeq"`
TurnYn string `xml:"turnYn" json:"-"`
}
// RouteInfoResponseBody is a structure that specifies the data format to be responsed from the API.
type RouteInfoResponseBody struct {
XMLName xml.Name `xml:"response"`
ComMsgHeader ComMsgHeader `xml:"comMsgHeader"`
MsgHeader MsgHeader `xml:"msgHeader"`
MsgBody RouteInfoMsgBody `xml:"msgBody"`
}
// RouteInfoMsgBody is a structure that specifies the data format of the message body in the APIResponseBody.
type RouteInfoMsgBody struct {
XMLName xml.Name `xml:"msgBody"`
BusRouteInfoItem BusRouteInfoItem `xml:"busRouteInfoItem"`
}
// BusRouteInfoItem is a structure that specifies the data format of the bus route information in the MsgBody.
type BusRouteInfoItem struct {
XMLName xml.Name `xml:"busRouteInfoItem" json:"-"`
DistrictCd int `xml:"districtCd" json:"-"`
DownFirstTime string `xml:"downFirstTime" json:"downFirstTime"`
DownLastTime string `xml:"downLastTime" json:"downLastTime"`
EndMobileNo string `xml:"endMobileNo" json:"-"`
EndStationID string `xml:"endStationId" json:"-"`
EndStationName string `xml:"endStationName" json:"endStationName"`
RegionName string `xml:"regionName" json:"regionName"`
RouteID string `xml:"routeId" json:"routeID"`
RouteName string `xml:"routeName" json:"routeNumber"`
RouteTypeName string `xml:"routeTypeName" json:"routeTypeName"`
StartMobileNo string `xml:"startMobileNo" json:"-"`
StartStationID string `xml:"startStationId" json:"-"`
StartStationName string `xml:"startStationName" json:"startStationName"`
UpFirstTime string `xml:"upFirstTime" json:"upFirstTime"`
UpLastTime string `xml:"upLastTime" json:"upLastTime"`
}
// LocationResponseBody is a structure that specifies the data format to be responsed from the API.
type LocationResponseBody struct {
XMLName xml.Name `xml:"response"`
ComMsgHeader ComMsgHeader `xml:"comMsgHeader"`
MsgHeader MsgHeader `xml:"msgHeader"`
MsgBody LocationMsgBody `xml:"msgBody"`
}
// LocationMsgBody is a structure that specifies the data format of the message body in the APIResponseBody.
type LocationMsgBody struct {
XMLName xml.Name `xml:"msgBody"`
BusLocationList BusLocationList `xml:"busLocationList"`
}
// BusLocationList is an slice of BusLocationes.
type BusLocationList []BusLocation
// BusLocation is a structure that specifies the data format of the bus location in the MsgBody.
type BusLocation struct {
XMLName xml.Name `xml:"busLocationList" json:"-"`
EndBus int `xml:"endBus" json:"endBus"`
LowPlate int `xml:"lowPlate" json:"lowPlate"`
PlateNo string `xml:"plateNo" json:"plateNo"`
RemainSeatCnt int `xml:"remainSeatCnt" json:"remainSeatCnt"`
StationID string `xml:"stationId" json:"stationID"`
StationSeq int `xml:"stationSeq" json:"stationSeq"`
}
// ArrivalResponseBody is a structure that specifies the data format to be responsed from the API.
type ArrivalResponseBody struct {
XMLName xml.Name `xml:"response"`
ComMsgHeader ComMsgHeader `xml:"comMsgHeader"`
MsgHeader MsgHeader `xml:"msgHeader"`
MsgBody ArrivalMsgBody `xml:"msgBody"`
}
// ArrivalMsgBody is a structure that specifies the data format of the message body in the APIResponseBody.
type ArrivalMsgBody struct {
XMLName xml.Name `xml:"msgBody"`
BusArrivalList BusArrivalList `xml:"busArrivalList"`
}
// BusArrivalList is an slice of BusArrivals.
type BusArrivalList []BusArrival
// BusArrival is a structure that specifies the data format of the bus arrival in the MsgBody.
type BusArrival struct {
XMLName xml.Name `xml:"busArrivalList" json:"-"`
LocationNo1 int `xml:"locationNo1" json:"locationNo1"`
LocationNo2 int `xml:"locationNo2" json:"locationNo2"`
LowPlate1 int `xml:"lowPlate1" json:"lowPlate1"`
LowPlate2 int `xml:"lowPlate2" json:"lowPlate2"`
PlateNo1 string `xml:"plateNo1" json:"plateNo1"`
PlateNo2 string `xml:"plateNo2" json:"plateNo2"`
PredictTime1 int `xml:"predictTime1" json:"predictTime1"`
PredictTime2 int `xml:"predictTime2" json:"predictTime2"`
RemainSeatCnt1 int `xml:"remainSeatCnt1" json:"remainSeatCnt1"`
RemainSeatCnt2 int `xml:"remainSeatCnt2" json:"remainSeatCnt2"`
RouteID string `xml:"routeId" json:"routeID"`
RouteNumber string `xml:"-" json:"routeNumber"`
RouteTypeName string `xml:"-" json:"routeTypeName"`
StaOrder int `xml:"staOrder" json:"stationSeq"`
}
// ArrivalItemResponseBody is a structure that specifies the data format to be responsed from the API.
type ArrivalItemResponseBody struct {
XMLName xml.Name `xml:"response"`
ComMsgHeader ComMsgHeader `xml:"comMsgHeader"`
MsgHeader MsgHeader `xml:"msgHeader"`
MsgBody ArrivalItemMsgBody `xml:"msgBody"`
}
// ArrivalItemMsgBody is a structure that specifies the data format of the message body in the APIResponseBody.
type ArrivalItemMsgBody struct {
XMLName xml.Name `xml:"msgBody"`
BusArrivalItem BusArrivalItem `xml:"busArrivalItem"`
}
// BusArrivalItem is a structure that specifies the data format of the bus arrival in the MsgBody.
type BusArrivalItem struct {
LocationNo1 int `xml:"locationNo1" json:"locationNo1"`
LocationNo2 int `xml:"locationNo2" json:"locationNo2"`
PlateNo1 string `xml:"plateNo1" json:"plateNo1"`
PlateNo2 string `xml:"plateNo2" json:"plateNo2"`
PredictTime1 int `xml:"predictTime1" json:"predictTime1"`
PredictTime2 int `xml:"predictTime2" json:"predictTime2"`
StaOrder int `xml:"staOrder" json:"-"`
}
func GetGoingBusList(sourceStationID string, destiStationID string) BusRouteList {
sourceRouteList := GetBusArrivalList(sourceStationID)
destiRouteList := GetBusArrivalList(destiStationID)
resultRouteList := BusRouteList{}
for _, sourceRoute := range sourceRouteList {
for _, destiRoute := range destiRouteList {
if (sourceRoute.RouteID == destiRoute.RouteID) && (sourceRoute.StaOrder < destiRoute.StaOrder) {
resultRouteList = append(resultRouteList, sourceRoute)
break
}
}
}
return resultRouteList
}
// SearchForStation is a function that searches for bus station using keywords.
func SearchForStation(keyword string) BusStationList {
URL := CommonURL + "/" + BusStationURLPath + "?serviceKey=" + config.ServiceKey + "&keyword=" + url.PathEscape(keyword)
responseBody, _ := getDataFromAPI(URL)
var data StationResponseBody
_ = xml.Unmarshal(responseBody, &data)
listLength := len(data.MsgBody.BusStationList)
if listLength > 50 {
return data.MsgBody.BusStationList
}
var wg sync.WaitGroup
for i := 0; i < listLength; i++ {
if data.MsgBody.BusStationList[i].MobileNo == " 00000" {
continue
} else {
wg.Add(1)
go func(index int, wg *sync.WaitGroup) {
data.MsgBody.BusStationList[index].StationDirect = GetStationDirect(data.MsgBody.BusStationList[index].StationID)
wg.Done()
}(i, &wg)
}
}
wg.Wait()
ret := data.MsgBody.BusStationList[:0]
for _, bs := range data.MsgBody.BusStationList {
if strings.TrimSpace(bs.MobileNo) == "00000" || bs.DistrictCd != 2 {
continue
}
ret = append(ret, bs)
}
return ret
}
// SearchForRoute is a function that searches for bus routes using keywords.
func SearchForRoute(keyword string) BusRouteList {
URL := CommonURL + "/" + BusRouteURLPath + "?serviceKey=" + config.ServiceKey + "&keyword=" + url.PathEscape(keyword)
responseBody, _ := getDataFromAPI(URL)
var data RouteResponseBody
_ = xml.Unmarshal(responseBody, &data)
if strings.Contains(fakeBus.GetBusRoute().RouteName, keyword) && keyword != "" {
data.MsgBody.BusRouteList = append(data.MsgBody.BusRouteList, fakeBus.GetBusRoute())
}
return data.MsgBody.BusRouteList
}
// GetRouteStationList is a function that takes a list of bus line stops.
func GetRouteStationList(routeID string) BusRouteStationList {
URL := CommonURL + "/" + BusRouteURLPath + "/station?serviceKey=" + config.ServiceKey + "&routeId=" + url.PathEscape(routeID)
responseBody, _ := getDataFromAPI(URL)
var data RouteStationResponseBody
_ = xml.Unmarshal(responseBody, &data)
if strings.Contains(fakeBus.GetBusRoute().RouteID, routeID) {
data.MsgBody.BusRouteStationList = fakeBus.GetBusRouteStationList()
}
return data.MsgBody.BusRouteStationList
}
// GetRouteNameFromRouteID is a function that get route name from routeID.
func GetRouteNameFromRouteID(routeID string) string {
data := GetRouteInfo(routeID)
return data.RouteName
}
// GetRouteTypeNameFromRouteID is a function that get route type from routeID.
func GetRouteTypeNameFromRouteID(routeID string) string {
data := GetRouteInfo(routeID)
return data.RouteTypeName
}
// GetStationNameFromStationID is a function that get station number from routeID and stationID.
func GetStationNameFromStationID(routeID string, stationID string) string {
rsList := GetRouteStationList(routeID)
for _, rs := range rsList {
if rs.StationID == stationID {
return rs.StationName
}
}
return ""
}
// GetRouteInfo is a function that get route information from routeID.
func GetRouteInfo(routeID string) BusRouteInfoItem {
URL := CommonURL + "/" + BusRouteURLPath + "/info?serviceKey=" + config.ServiceKey + "&routeId=" + url.PathEscape(routeID)
responseBody, _ := getDataFromAPI(URL)
var data RouteInfoResponseBody
_ = xml.Unmarshal(responseBody, &data)
if strings.Contains(fakeBus.GetBusRoute().RouteID, routeID) {
data.MsgBody.BusRouteInfoItem = fakeBus.GetBusRouteInfo()
}
return data.MsgBody.BusRouteInfoItem
}
// GetCurrentBusLocation is a function that takes the location of the current bus.
func GetCurrentBusLocation(routeID string) BusLocationList {
URL := CommonURL + "/" + BusLocationURLPath + "?serviceKey=" + config.ServiceKey + "&routeId=" + url.PathEscape(routeID)
responseBody, _ := getDataFromAPI(URL)
var data LocationResponseBody
_ = xml.Unmarshal(responseBody, &data)
if strings.Contains(fakeBus.GetBusRoute().RouteID, routeID) {
data.MsgBody.BusLocationList = fakeBus.GetCurrentBusLocation()
}
return data.MsgBody.BusLocationList
}
// GetBusArrivalOnlyOne is a function that gets the arrival time information of the station
func GetBusArrivalOnlyOne(routeID string, stationID string) BusArrivalItem {
URL := CommonURL + "/" + BusArrivalURLPath + "?serviceKey=" + config.ServiceKey + "&routeId=" + url.PathEscape(routeID) + "&stationId=" + url.PathEscape(stationID)
responseBody, _ := getDataFromAPI(URL)
var data ArrivalItemResponseBody
_ = xml.Unmarshal(responseBody, &data)
if routeID == fakeBus.GetBusRoute().RouteID {
arrival := fakeBus.GetBusArrival(stationID)
data.MsgBody.BusArrivalItem = BusArrivalItem{
arrival.LocationNo1,
arrival.LocationNo2,
arrival.PlateNo1,
arrival.PlateNo2,
arrival.PredictTime1,
arrival.PredictTime2,
arrival.StaOrder,
}
}
return data.MsgBody.BusArrivalItem
}
// GetBusArrivalTime is a function that gets the arrival time information of the station
func GetBusArrivalTime(stationID string) BusArrivalList {
URL := CommonURL + "/" + BusArrivalURLPath + "/station?serviceKey=" + config.ServiceKey + "&stationId=" + url.PathEscape(stationID)
responseBody, _ := getDataFromAPI(URL)
var data ArrivalResponseBody
_ = xml.Unmarshal(responseBody, &data)
data.MsgBody.BusArrivalList = FillRouteNumber(stationID, data.MsgBody.BusArrivalList)
for i := 0; i < len(data.MsgBody.BusArrivalList); i++ {
if data.MsgBody.BusArrivalList[i].RouteID == fakeBus.GetBusRoute().RouteID {
data.MsgBody.BusArrivalList[i] = fakeBus.GetBusArrival(stationID)
}
}
return data.MsgBody.BusArrivalList
}
// GetBusArrivalList is a function that gets the arrival list information of the station
func GetBusArrivalList(stationID string) BusRouteList {
URL := CommonURL + "/" + BusStationURLPath + "/route?serviceKey=" + config.ServiceKey + "&stationId=" + url.PathEscape(stationID)
responseBody, _ := getDataFromAPI(URL)
var data RouteResponseBody
_ = xml.Unmarshal(responseBody, &data)
if fakeBus.IsInBusList(stationID) {
data.MsgBody.BusRouteList = append(data.MsgBody.BusRouteList, fakeBus.GetBusRoute())
}
return data.MsgBody.BusRouteList
}
// FillRouteNumber is a function that fills the route numbers of BusArrivalList
func FillRouteNumber(stationID string, busArrivalList BusArrivalList) BusArrivalList {
busList := GetBusArrivalList(stationID)
var isDetected bool
for _, bus := range busList {
isDetected = false
for i := 0; i < len(busArrivalList); i++ {
if bus.RouteID == busArrivalList[i].RouteID {
busArrivalList[i].RouteNumber = bus.RouteName
busArrivalList[i].RouteTypeName = GetRouteTypeNameFromRouteID(bus.RouteID)
isDetected = true
break
}
}
if !isDetected {
busArrivalList = append(busArrivalList, BusArrival{
xml.Name{}, -1, -1, -1, -1, "", "", -1, -1, -1, -1,
bus.RouteID,
bus.RouteName,
bus.RouteTypeName,
0,
})
}
}
return busArrivalList
}
// GetStationDirect is a function that get stationDirect from a stationID
func GetStationDirect(stationID string) (stationDirect string) {
if direct := getStaDirect(stationID); direct != "" {
stationDirect = direct
return
}
busRouteList := GetBusArrivalList(stationID)
if len(busRouteList) == 0 {
return ""
}
nextStationNameList := make([]string, len(busRouteList), len(busRouteList))
var wg sync.WaitGroup
for i := 0; i < len(busRouteList); i++ {
wg.Add(1)
go func(index int, wg *sync.WaitGroup) {
busRouteStationList := GetRouteStationList(busRouteList[index].RouteID)
length := len(busRouteStationList)
currentStaOrder := busRouteList[index].StaOrder
if currentStaOrder >= length {
nextStationNameList[index] = "종점"
} else {
nextStationNameList[index] = busRouteStationList[currentStaOrder].StationName
}
wg.Done()
}(i, &wg)
}
wg.Wait()
directMap := map[string]int{}
for _, str := range nextStationNameList {
directMap[str]++
}
maxCnt := -1
for key, value := range directMap {
if maxCnt < value {
stationDirect = key
}
}
err := addStaDirect(stationID, stationDirect)
if err != nil {
ErrorLogger(err)
}
return stationDirect
}
// getDataFromAPI is a function that get data from GBUS API.
func getDataFromAPI(URL string) (responseBody []byte, funcErr error) {
response, err := http.Get(URL)
if err != nil {
funcErr = err
return
}
if response.StatusCode != 200 {
funcErr = errors.New("Not expected http.StatusCode: 200")
}
defer response.Body.Close()
responseBody, _ = ioutil.ReadAll(response.Body)
return
}
// GetStationName is a function that get stationID and stationNumber
func GetStationName(stationNumber string, stationID string) (stationName string) {
data := SearchForStation(stationNumber)
for _, st := range data {
if (st.StationID == stationID) && (strings.TrimSpace(st.MobileNo) == stationNumber) {
stationName = st.StationName
break
}
}
return
}