suwako / norikae アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

master
ブランチ master
norikae / src / routedetail.go
3.2 KB、104行 生 Blame 履歴
1package src
2
3import (
4 "strings"
5 "regexp"
6
7 "github.com/gocolly/colly"
8)
9
10func getSummary(e *colly.HTMLElement) Route {
11 r := Route{}
12 if e.Attr("class") == "icnPriTime" { r.Badges = append(r.Badges, 1) }
13 if e.Attr("class") == "icnPriFare" { r.Badges = append(r.Badges, 2) }
14 if e.Attr("class") == "icnPriTrans" { r.Badges = append(r.Badges, 3) }
15
16 return r
17}
18
19func handleFare(el *colly.HTMLElement, f Fare, s Stop) Fare {
20 fixTrain := strings.ReplaceAll(
21 el.ChildText("li.transport div"), "[train]", "【電車】",
22 )
23 fixBus := strings.ReplaceAll(fixTrain, "[bus]", "【バス】")
24 fixAir := strings.ReplaceAll(fixBus, "[air]", "【空路】")
25 fixEki := strings.ReplaceAll(fixAir, "当駅始発", "【当駅始発】")
26 pattern := regexp.MustCompile(`(^.*線)(.*[行|方面]$)`)
27
28 fixeki := pattern.ReplaceAllString(fixEki, "$1 $2")
29
30 f.Train = fixeki
31 f.Platform = el.ChildText("li.platform")
32 f.Color = strings.ReplaceAll(el.ChildAttr("span", "style"), "border-color:#", "")
33 el.ForEach("li.stop ul", func (js int, els *colly.HTMLElement) {
34 s.Time = els.ChildText("li dl dt")
35 s.Name = strings.ReplaceAll(els.ChildText("li dl dd"), "○", "")
36 f.Stops = append(f.Stops, s)
37 })
38
39 return f
40}
41
42func handleWalk(el *colly.HTMLElement, f Fare) Fare {
43 f.Train = strings.ReplaceAll(
44 el.ChildText("li.transport"), "[line][walk]", "",
45 )
46 f.Platform = ""
47 f.Color = "a8a8a8"
48
49 return f
50}
51
52func getRouteDetail(e *colly.HTMLElement) Route {
53 r := Route{}
54 onDivs := "div.routeSummary div ul.priority li span"
55 e.ForEach(onDivs, func(i int, el *colly.HTMLElement) {
56 summary := getSummary(el)
57 r.Badges = append(r.Badges, summary.Badges...)
58 })
59
60 base := e.ChildText("ul.summary li.time span")
61 time := strings.ReplaceAll(base, e.ChildText("ul.summary li.time span.small"), "")
62 time2 := strings.Split(time, "着")
63 r.Time = time2[0] + "着"
64 durabase := e.ChildText("ul.summary li.time")
65 durasi := strings.Index(durabase, "着") + len("着")
66 duraei := strings.Index(durabase[durasi:], "分") + len("分") + durasi
67
68 r.Duration = durabase[durasi:duraei]
69 r.TransitCunt = strings.ReplaceAll(
70 e.ChildText("ul.summary li.transfer"), "乗換:", "",
71 )
72 r.Fare = strings.ReplaceAll(
73 e.ChildText("ul.summary li.fare"), "[priic]IC優先:", "",
74 )
75
76 onDivs = "div.routeDetail div.station"
77 e.ForEach(onDivs, func (j int, el *colly.HTMLElement) {
78 station := Station{}
79 station.Time = el.ChildText("ul.time li")
80 if el.ChildText("p.icon span") == "[dep]" { station.Time += "発" }
81 if el.ChildText("p.icon span") == "[arr]" { station.Time += "着" }
82 station.Name = el.ChildText("dl dt a")
83
84 fares := []Fare{}
85 onDivs = "div.routeDetail div.fareSection div.access"
86 e.ForEach(onDivs, func (jf int, elf *colly.HTMLElement) {
87 fare := Fare{}
88 fare = handleFare(elf, fare, Stop{})
89 fares = append(fares, fare)
90 })
91
92 onDivs = "div.routeDetail div.walk ul.info"
93 e.ForEach(onDivs, func (jw int, elw *colly.HTMLElement) {
94 fare := Fare{}
95 fare = handleWalk(elw, fare)
96 fares = append(fares, fare)
97 })
98
99 station.Fares = fares
100 r.Stations = append(r.Stations, station)
101 })
102
103 return r
104}