Added project

This commit is contained in:
nedko 2024-04-15 13:23:21 +03:00
commit 8a13989ba3
6 changed files with 10538 additions and 0 deletions

10186
cache.json Normal file

File diff suppressed because it is too large Load Diff

22
favourites.json Normal file
View File

@ -0,0 +1,22 @@
[
{
"name": "Държавна печатница",
"code": "0555",
"id": "505"
},
{
"name": "Метростанция \"Цариградско шосе\"",
"code": "1016",
"id": "916"
},
{
"name": "Комплекс на БАН",
"code": "0084",
"id": "80"
},
{
"name": "УМБАЛ Св. Анна",
"code": "1196",
"id": "1079"
}
]

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.internedko.net/nedko/sofiatraffic
go 1.22.1

28
index.html Normal file
View File

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>SofiaTraffic EZ Table</title>
</head>
<body>
<h1 style="text-align: center;">
SofiaTraffic EZ Table
</h1>
<form style="text-align: center;">
<input type="text" name="find" style="width: 20em; height: 2em;"/>
<input type="submit" value="Find" style="width: 10em; height: 2em;"/>
</form>
<table style="margin-left: auto; margin-right: auto;">
{{ range .Favourites }}
<tr style="height: 2em; vertical-align: center;">
<td style="width: 30em; text-align: center;">
<a href="/{{ .ID }}">
{{ .Name }} ({{ .Code }})
</a>
</td>
</tr>
{{ end }}
</table>
</body>
</html>

251
main.go Normal file
View File

@ -0,0 +1,251 @@
package main
import (
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"sort"
"strconv"
"strings"
)
var stopIDs map[int] int;
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 OutputData struct {
ID string
Lines []Line
}
type CacheData struct {
TargetID int `json:"target_id"`
DataID int `json:"data_id"`
}
func lessLine(left Line, right Line) bool {
val := strings.Compare(left.Transport, right.Transport)
if val != 0 {
return val < 0
} else {
l, lerr := strconv.Atoi(left.Name)
r, rerr := strconv.Atoi(right.Name)
switch {
case lerr == nil && rerr == nil:
return l < r
case lerr == nil && rerr != nil:
return true
case lerr != nil && rerr == nil:
return false
case lerr != nil && rerr != nil:
val = strings.Compare(left.Name, right.Name)
return val < 0
}
}
return true
}
func handle(resp http.ResponseWriter, req *http.Request) {
id := strings.TrimPrefix(req.URL.Path, "/")
find_id := req.URL.Query().Get("find")
if len(find_id) != 0 {
find_int, err := strconv.Atoi(find_id)
if err != nil {
log.Println(err)
show500(resp)
return
}
target_id := stopIDs[find_int]
http.Redirect(resp, req, fmt.Sprintf("/%d", target_id), http.StatusFound)
} else if len(id) == 0 {
showFavourites(resp)
} else {
showTable(resp, id)
}
}
func show500(resp http.ResponseWriter) {
resp.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(resp, "<h1 style=\"text-align:center;\">500 Internal Server Error</h1>")
}
func setLineColor(line *Line) {
switch {
case line.Transport == "A":
line.Color = "FF0000"
case line.Transport == "ТБ":
line.Color = "0000FF"
case line.Transport == "ТМ":
line.Color = "FF8000"
}
}
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 showTable(resp http.ResponseWriter, id string) {
data, err := getSofiaTrafficData(id)
if err != nil {
show500(resp)
return
}
// Sort Lines
sort.Slice(data.Data.Lines, func (i int, j int) bool {
return lessLine(data.Data.Lines[i], data.Data.Lines[j])
})
// Set Line Colors
for idx := range(data.Data.Lines) {
setLineColor(&(data.Data.Lines[idx]))
}
var dataOut OutputData
dataOut.ID = fmt.Sprintf("%04d", data.Data.ID)
dataOut.Lines = data.Data.Lines
templ := template.Must(template.ParseFiles("stop.html"))
templ.Execute(resp, dataOut)
}
func readFavourites() []Favourite {
var favourites []Favourite
jsonFile, err := os.Open("favourites.json")
if err != nil {
log.Println(err)
return favourites
}
defer jsonFile.Close()
bytes, err := io.ReadAll(jsonFile)
if err != nil {
log.Println(err)
return favourites
}
err = json.Unmarshal(bytes, &favourites)
if err != nil {
log.Println(err)
}
return favourites
}
func showFavourites(resp http.ResponseWriter) {
// Read And Parse JSON
favourites := readFavourites()
data := make(map[string] []Favourite)
data["Favourites"] = favourites
// Read And Execute Template
templ := template.Must(template.ParseFiles("index.html"))
templ.Execute(resp, data)
}
func loadIDs() {
stopIDs = make(map[int]int)
var cache []CacheData
jsonFile, err := os.Open("cache.json")
if err != nil {
log.Println(err)
return
}
defer jsonFile.Close()
bytes, err := io.ReadAll(jsonFile)
if err != nil {
log.Println(err)
return
}
err = json.Unmarshal(bytes, &cache)
if err != nil {
log.Println(err)
return
}
for _, val := range(cache) {
stopIDs[val.TargetID] = val.DataID
}
}
func main() {
loadIDs()
http.HandleFunc("/", handle)
log.Println("Server Running")
http.ListenAndServe(":8000", nil)
}

48
stop.html Normal file
View File

@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>SofiaTraffic EZ Table</title>
</head>
<body>
<table style="margin-left: auto; margin-right: auto; text-align: center;">
<tr>
<td>
<a href="/" style="display:block">
Back
</a>
</td>
<td colspan="2">
{{ .ID }}
</td>
</tr>
{{ range .Lines }}
<tr>
<td style="background-color: #{{ .Color }}; color: #FFFFFF;">
<div style="width: 2em; display: inline-block; margin: 0 0 0 0; padding: 0 0 0 0;">
{{ .Transport }}
</div><div style="width: 3em; display: inline-block; margin: 0 0 0 0; padding: 0 0 0 0;">
{{ .Name }}
</div>
</td>
{{ range .Cars }}
<td>
<div style="width: 1em; display: inline-block; margin: 0 0 0 0; padding: 0 0 0 0;">
{{ if .Flags.AirConditioner }}
<svg stroke="#2c54a4" fill="#2c54a4" stroke-width="0" viewBox="0 0 448 512" class="icon" style="vertical-align: baseline; margin-left: 0px;" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg">
<path d="M440.3 345.2l-33.8-19.5 26-7c8.2-2.2 13.1-10.7 10.9-18.9l-4-14.9c-2.2-8.2-10.7-13.1-18.9-10.9l-70.8 19-63.9-37 63.8-36.9 70.8 19c8.2 2.2 16.7-2.7 18.9-10.9l4-14.9c2.2-8.2-2.7-16.7-10.9-18.9l-26-7 33.8-19.5c7.4-4.3 9.9-13.7 5.7-21.1L430.4 119c-4.3-7.4-13.7-9.9-21.1-5.7l-33.8 19.5 7-26c2.2-8.2-2.7-16.7-10.9-18.9l-14.9-4c-8.2-2.2-16.7 2.7-18.9 10.9l-19 70.8-62.8 36.2v-77.5l53.7-53.7c6.2-6.2 6.2-16.4 0-22.6l-11.3-11.3c-6.2-6.2-16.4-6.2-22.6 0L256 56.4V16c0-8.8-7.2-16-16-16h-32c-8.8 0-16 7.2-16 16v40.4l-19.7-19.7c-6.2-6.2-16.4-6.2-22.6 0L138.3 48c-6.3 6.2-6.3 16.4 0 22.6l53.7 53.7v77.5l-62.8-36.2-19-70.8c-2.2-8.2-10.7-13.1-18.9-10.9l-14.9 4c-8.2 2.2-13.1 10.7-10.9 18.9l7 26-33.8-19.5c-7.4-4.3-16.8-1.7-21.1 5.7L2.1 145.7c-4.3 7.4-1.7 16.8 5.7 21.1l33.8 19.5-26 7c-8.3 2.2-13.2 10.7-11 19l4 14.9c2.2 8.2 10.7 13.1 18.9 10.9l70.8-19 63.8 36.9-63.8 36.9-70.8-19c-8.2-2.2-16.7 2.7-18.9 10.9l-4 14.9c-2.2 8.2 2.7 16.7 10.9 18.9l26 7-33.8 19.6c-7.4 4.3-9.9 13.7-5.7 21.1l15.5 26.8c4.3 7.4 13.7 9.9 21.1 5.7l33.8-19.5-7 26c-2.2 8.2 2.7 16.7 10.9 18.9l14.9 4c8.2 2.2 16.7-2.7 18.9-10.9l19-70.8 62.8-36.2v77.5l-53.7 53.7c-6.3 6.2-6.3 16.4 0 22.6l11.3 11.3c6.2 6.2 16.4 6.2 22.6 0l19.7-19.7V496c0 8.8 7.2 16 16 16h32c8.8 0 16-7.2 16-16v-40.4l19.7 19.7c6.2 6.2 16.4 6.2 22.6 0l11.3-11.3c6.2-6.2 6.2-16.4 0-22.6L256 387.7v-77.5l62.8 36.2 19 70.8c2.2 8.2 10.7 13.1 18.9 10.9l14.9-4c8.2-2.2 13.1-10.7 10.9-18.9l-7-26 33.8 19.5c7.4 4.3 16.8 1.7 21.1-5.7l15.5-26.8c4.3-7.3 1.8-16.8-5.6-21z">
</path>
</svg>
{{ end }}
</div><div style="width: 4em; display: inline-block; margin: 0 0 0 0; padding: 0 0 0 0;">
{{ .DepartureTime }}
</span>
</td>
{{ end }}
</tr>
{{ end }}
</table>
</body>
</html>