Added Project
This commit is contained in:
commit
97256477e3
3
go.mod
Normal file
3
go.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module git.internedko.net/nedko/sofiatraffic_cache
|
||||||
|
|
||||||
|
go 1.22.1
|
||||||
126
main.go
Normal file
126
main.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user