-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
190 lines (153 loc) · 4.67 KB
/
db.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
package main
import (
firebase "firebase.google.com/go"
"firebase.google.com/go/db"
"google.golang.org/api/option"
"log"
"context"
"strconv"
"fmt"
)
func dbConnect() (*db.Client, error){
ctx := context.Background()
config := &firebase.Config{
DatabaseURL: "https://mzdik-scrapper.firebaseio.com",
}
opt := option.WithCredentialsFile("../mzdik-scrapper.json")
app, err := firebase.NewApp(context.Background(), config, opt)
if err != nil {
log.Fatalf("error initializing app: %v", err)
}
client, err := app.Database(ctx)
if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}
return client, err;
}
func dbUpdateBusStation(busStation int, client *db.Client){
fmt.Println("Startuje: ", busStation)
firstWay, oppoWay := getBusStations(strconv.Itoa(busStation));
// One way
dataToSet := make(map[string]BusStation);
for index, element := range firstWay {
dataToSet[strconv.Itoa(index)] = element;
}
stationsRef := client.NewRef("ways/forward/buses/" + strconv.Itoa(busStation) + "/stations");
ctx := context.Background();
err := stationsRef.Set(ctx, dataToSet);
if err != nil {
log.Fatalln("error: ", err);
}
amountRef := client.NewRef("ways/forward/buses/" + strconv.Itoa(busStation) + "/amount");
err = amountRef.Set(ctx, len(dataToSet));
if err != nil {
log.Fatalln("error: ", err);
}
// Second way
dataToSet = make(map[string]BusStation);
for index, element := range oppoWay {
dataToSet[strconv.Itoa(index)] = element;
}
stationsRef = client.NewRef("ways/backward/buses/" + strconv.Itoa(busStation) + "/stations");
err = stationsRef.Set(ctx, dataToSet);
if err != nil {
log.Fatalln("error: ", err);
}
amountRef = client.NewRef("ways/backward/buses/" + strconv.Itoa(busStation) + "/amount");
err = amountRef.Set(ctx, len(dataToSet));
if err != nil {
log.Fatalln("error: ", err);
}
fmt.Println("Skonczylem bus: ", busStation);
}
func dbUpdateBusTimetable(busStation int, client *db.Client){
fmt.Println("Startuje: ", busStation)
ctx := context.Background();
stationsRef := client.NewRef("ways/forward/buses/" + strconv.Itoa(busStation) + "/timetable");
stationsBackRef := client.NewRef("ways/backward/buses/" + strconv.Itoa(busStation) + "/timetable");
amountRef := client.NewRef("ways/forward/buses/" + strconv.Itoa(busStation) + "/amount");
var amount int;
if err := amountRef.Get(ctx, &amount); err != nil {
log.Fatalln("error: ", err);
}
timetable, timetableBackwards := getBusTimetable(strconv.Itoa(busStation), amount)
err := stationsRef.Set(ctx, timetable);
if err != nil {
log.Fatalln("error: ", err);
}
err = stationsBackRef.Set(ctx, timetableBackwards);
if err != nil {
log.Fatalln("error: ", err);
}
}
type FewBusStations struct {
Stations []BusStation
}
func dbGetBusStations(bus int, client *db.Client) ([]BusStation, []BusStation){
// Remember about both side!
ctx := context.Background();
path := fmt.Sprintf("ways/backward/buses/%d/stations", bus);
ref := client.NewRef(path);
var backwardStations []BusStation;
if err := ref.Get(ctx, &backwardStations); err != nil {
log.Fatalln("Error reading value: ", err)
}
path = fmt.Sprintf("ways/forward/buses/%d/stations", bus);
ref = client.NewRef(path);
var forwardStations []BusStation;
if err := ref.Get(ctx, &forwardStations); err != nil {
log.Fatalln("Error reading value: ", err)
}
return backwardStations, forwardStations;
}
func dbGetBuses() []int{
var buses []int;
for i := 1; i <= 26; i++ {
buses = append(buses, i)
}
return buses;
}
func dbGetBusCertainTimetable(bus int, backwards bool, client *db.Client, dayType int64) []BusCourse{
// dayType
// 1 - CasualDay
// 2 - Saturday
// 3 - Saints
var basicPath string;
if backwards {
basicPath = fmt.Sprintf("ways/backward/buses/%d/timetable", bus);
} else {
basicPath = fmt.Sprintf("ways/forward/buses/%d/timetable", bus);
}
criterium := "/CasualDay";
switch(dayType){
case 1:
criterium = "/CasualDay"
case 2:
criterium = "/Saturday"
case 3:
criterium = "/Saints"
}
fullPath := fmt.Sprintf("%s%s", basicPath, criterium)
ref := client.NewRef(fullPath);
var table []BusCourse;
ctx := context.Background();
if err := ref.Get(ctx, &table); err != nil {
log.Fatalln("Error reading value: ", err)
}
return table;
}
func dbGetBusFullTimetable(bus int, backwards bool, client *db.Client) BusTimetable{
var path string;
if backwards {
path = fmt.Sprintf("ways/backward/buses/%d/timetable", bus);
} else {
path = fmt.Sprintf("ways/forward/buses/%d/timetable", bus);
}
ref := client.NewRef(path);
var table BusTimetable;
ctx := context.Background();
if err := ref.Get(ctx, &table); err != nil {
log.Fatalln("Error reading value: ", err)
}
return table;
}