-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainmtc.go
67 lines (60 loc) · 1.54 KB
/
mainmtc.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
package main
import (
"errors"
"fmt"
"os"
)
const buswebsite = `http://www.mtcbus.org/Routes.asp`
const prebusurl = `http://www.mtcbus.org/Routes.asp?cboRouteCode=`
const postbusurl = `&submit=Search`
const busdb = "bus.db"
const bustable = "buss"
const buscolmns = "route TEXT, servtype TEXT, origin TEXT, dest TEXT, jtime INTEGER"
var (
ErrBusListAccess = errors.New("Cannot download the buslist ")
ErrCreateDbase = errors.New("Cannot open the database ")
ErrTableCreate = errors.New("Cannot create table ")
ErrAddBuss = errors.New("Unable to add bus data to database ")
ErrUnableClose = errors.New("Unable to close the connection to the database ")
ErrGetBus = errors.New("Unable to get bus data from remote server ")
ErrAddBusPath = errors.New("Unable to add bus path data to database ")
)
func main() {
bl, e := getBusListfrmMTC(buswebsite)
if e != nil {
fmt.Println(ErrBusListAccess)
os.Exit(-1)
}
dbc, e := createDB(busdb)
if e != nil {
fmt.Println(ErrCreateDbase, busdb)
os.Exit(-1)
}
e = createTable(dbc, bustable, buscolmns)
if e != nil {
fmt.Println(ErrTableCreate, bustable)
os.Exit(-1)
}
for _, v := range bl[:5] {
b, e := newBus(prebusurl, v, postbusurl)
if e != nil {
fmt.Println(ErrGetBus, v)
continue
}
e = addBus(b, bustable, dbc)
if e != nil {
fmt.Println(ErrAddBuss, v)
continue
}
e = addBusPath(b, dbc)
if e != nil {
fmt.Print(ErrAddBusPath, v)
continue
}
fmt.Println("added: ", v)
}
e = dbc.Close()
if e != nil {
fmt.Println(ErrUnableClose)
}
}