-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgettingbusdetails.go
69 lines (61 loc) · 1.69 KB
/
gettingbusdetails.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
package main
import (
"github.com/PuerkitoBio/goquery"
"regexp"
"strconv"
)
type busdet struct {
rawdet string
routenum string
servtype string
origin string
dest string
jmin int
stname []string
}
//Contacts remote host with query and creates a struct with details of
//the bus
//INPUT : bus number
//OUTPUT: struct with the bus details
func newBus(bef, bus, aft string) (*busdet, error) {
var bd *busdet
url := bef + bus + aft
d, e := goquery.NewDocument(url)
if e != nil {
return nil, e
}
r := d.Find("[BGColor='#EAEAEA'],[BGColor='white']").
Not(":contains('Route')").Text()
bd = &busdet{rawdet: r, routenum: bus}
setStageNames(bd)
setOrigin(bd)
setDest(bd)
setServiceType(bd)
setJourneyTime(bd)
return bd, nil
}
func setServiceType(b *busdet) {
// regpat := regexp.MustCompile(b.routenum+"([A-Za-z ]+?)[A-Z]")
regpat := regexp.MustCompile(b.routenum + `([\w ]+?)` + b.origin)
b.servtype = regpat.FindStringSubmatch(b.rawdet)[1]
}
func setJourneyTime(b *busdet) {
// regpat := regexp.MustCompile(b.routenum+`[A-Za-z ]+?[A-Z. ]+?(\d+?)\d\.`)
regpat := regexp.MustCompile(b.routenum + b.servtype + b.origin + b.dest + `(\d+?)\d\.`)
b.jmin, _ = strconv.Atoi(regpat.FindStringSubmatch(b.rawdet)[1])
}
//Set the intermediate points between destination and origin
func setStageNames(b *busdet) {
actreg := regexp.MustCompile(b.routenum + `.+?(\d\..*)`)
temp := actreg.FindStringSubmatch(b.rawdet)[1]
regpat := regexp.MustCompile(`(.*?\.?)(?:\d\.|$)`)
for _, v := range regpat.FindAllStringSubmatch(temp, -1)[1:] {
b.stname = append(b.stname, v[1])
}
}
func setOrigin(b *busdet) {
b.origin = b.stname[0]
}
func setDest(b *busdet) {
b.dest = b.stname[len(b.stname)-1]
}