From 97256477e3308db34ee5ee114606f95a8c490330 Mon Sep 17 00:00:00 2001 From: nedko Date: Mon, 15 Apr 2024 13:25:46 +0300 Subject: [PATCH] Added Project --- go.mod | 3 ++ main.go | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 go.mod create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5182aa1 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.internedko.net/nedko/sofiatraffic_cache + +go 1.22.1 diff --git a/main.go b/main.go new file mode 100644 index 0000000..02aa828 --- /dev/null +++ b/main.go @@ -0,0 +1,126 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "log" + "net/http" + "os" + "strconv" + "time" +) + +type Favourite struct { + Name string `json:"name"` + Code string `json:"code"` + ID string `json:"id"` +} + +type CarFlags struct { + Disabled bool `json:"disabled"` + AirConditioner bool `json:"klimatik"` + BicycleRack bool `json:"bicycle_rack"` + DoubleDecker bool `json:"doubledecker"` + ByTimeTable bool `json:"by_timetable"` +} + +type Car struct { + DepartureTime string `json:"departure_time"` + Flags CarFlags `json:"flags"` + Destination int `json:"destination"` + CourseID int `json:"course_id"` + DestinationName string `json:"destination_name"` + CalculatedTime string `json:"calc_time"` + DirectionFlag int `json:"direction_flag"` + TTCourseStopID int `json:"ttcoursestop_id"` +} + +type Line struct { + Transport string `json:"transport"` + ID int `json:"id"` + Name string `json:"name"` + Cars []Car `json:"cars"` + Color string +} + +type VirtualPanelData struct { + ID int `json:"id"` + Lines []Line `json:"lines"` +} + +type SofiaTraffic struct { + Data VirtualPanelData `json:"virtual_panel_data"` +} + +type CacheData struct { + TargetID int `json:"target_id"` + DataID int `json:"data_id"` +} + +func getSofiaTrafficData(id string) (SofiaTraffic, error) { + var data SofiaTraffic + url := fmt.Sprintf("https://www.sofiatraffic.bg/interactivecard/virtual_panel?stop_id=%s", id) + + respAPI, err := http.Get(url) + if err != nil { + log.Println(err) + return data, errors.New("get failed") + } + if respAPI.StatusCode != 200 { + log.Printf("Status code for ID %v is %v", id, respAPI.StatusCode) + return data, errors.New("bad status code") + } + if respAPI.Body != nil { + defer respAPI.Body.Close() + } + + body, err := io.ReadAll(respAPI.Body) + if err != nil { + log.Println(err) + return data, errors.New("failed to read body") + } + + err = json.Unmarshal(body, &data) + if err != nil { + log.Println(err) + return data, errors.New("failed to unmarshal json") + } + + return data, nil +} + +func main() { + var cache []CacheData + for i := 1; i < 2750; i++ { + time.Sleep(time.Second) + if i % 10 == 0 { + fmt.Printf("Getting ID %d\n", i) + } + + data, err := getSofiaTrafficData(strconv.Itoa(i)) + if err != nil { + log.Printf("ID - %d - \"%v\"\n", i, err) + continue + } + var temp CacheData + temp.TargetID = data.Data.ID + temp.DataID = i + + if temp.DataID != 0 { + cache = append(cache, temp) + } + } + + data, err := json.MarshalIndent(cache, "", " ") + if err != nil { + log.Println(err) + return + } + + err = os.WriteFile("cache.json", data, 0644) + if err != nil { + log.Println(err) + } +}