initial commit
This commit is contained in:
37
cmd/tracker/main.go
Normal file
37
cmd/tracker/main.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"shellixa/puvetra/internal/maps"
|
||||
"shellixa/puvetra/internal/types"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
log.Printf("Starting up..\n")
|
||||
trips := maps.GetTrips()
|
||||
|
||||
var allTrams []types.MapTrip
|
||||
|
||||
for _, t := range trips {
|
||||
if t.Mode == "TRAM" {
|
||||
allTrams = append(allTrams, t)
|
||||
}
|
||||
}
|
||||
|
||||
var traminfo []map[string]string
|
||||
|
||||
for _, tr := range allTrams {
|
||||
data := map[string]string{}
|
||||
data["line"] = tr.Trips[0].RouteShortName
|
||||
data["currentStation"] = tr.From.Name
|
||||
data["nextStation"] = tr.To.Name
|
||||
data["departure"] = tr.Departure
|
||||
data["arrival"] = tr.Arrival
|
||||
traminfo = append(traminfo, data)
|
||||
}
|
||||
|
||||
log.Printf("%+v\n", traminfo)
|
||||
log.Printf("%d", len(traminfo))
|
||||
}
|
||||
|
||||
43
internal/maps/trips.go
Normal file
43
internal/maps/trips.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package maps
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"shellixa/puvetra/internal/types"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetTrips() []types.MapTrip {
|
||||
client := http.Client{}
|
||||
req, err := http.NewRequest("GET", "https://api.transitous.org/api/v1/map/trips", nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
q := req.URL.Query()
|
||||
|
||||
now := time.Now().Format(time.RFC3339)
|
||||
q.Add("startTime", now)
|
||||
q.Add("endTime", now)
|
||||
q.Add("min", "51.1626,13.5609")
|
||||
q.Add("max", "50.9973,13.8596")
|
||||
q.Add("zoom", "14")
|
||||
q.Add("precision", "2")
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
response, err := client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer response.Body.Close()
|
||||
|
||||
var trips []types.MapTrip
|
||||
if err := json.NewDecoder(response.Body).Decode(&trips); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return trips
|
||||
|
||||
}
|
||||
63
internal/types/types.go
Normal file
63
internal/types/types.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package types
|
||||
|
||||
|
||||
type TripMeta struct {
|
||||
TripId string
|
||||
RouteShortName string
|
||||
DisplayName string `json:"omitempty"`
|
||||
}
|
||||
|
||||
type MapTrip struct {
|
||||
Trips []TripMeta
|
||||
RouteColor string
|
||||
Mode string
|
||||
Distance float32
|
||||
Departure string
|
||||
Arrival string
|
||||
From Place
|
||||
To Place
|
||||
ScheduledDeparture string
|
||||
ScheduledArrival string
|
||||
RealTime bool
|
||||
Polyline string
|
||||
}
|
||||
|
||||
type Trip struct {
|
||||
Duration int64
|
||||
StartTime string
|
||||
EndTine string
|
||||
Transfers int
|
||||
Id string
|
||||
Legs []TripLeg
|
||||
}
|
||||
|
||||
type TripLeg struct {
|
||||
Mode string
|
||||
From Place
|
||||
To Place
|
||||
Duration int64
|
||||
StartTime string
|
||||
EndTime string
|
||||
ScheduledStartTime string
|
||||
ScheduledEndTime string
|
||||
RealTime bool
|
||||
Scheduled bool
|
||||
Distance float32
|
||||
Headsign string
|
||||
TripFrom Place
|
||||
TripTo Place
|
||||
}
|
||||
|
||||
type Place struct {
|
||||
Name string
|
||||
StopId string
|
||||
Lat float32
|
||||
Lon float32
|
||||
Arrival string
|
||||
Departure string
|
||||
Track string
|
||||
StopCode string
|
||||
Description string
|
||||
Cancelled bool
|
||||
Modes []string
|
||||
}
|
||||
@@ -6,10 +6,10 @@ if [[ $# -ne 1 ]]; then
|
||||
fi
|
||||
|
||||
|
||||
if [[ $1 -lt 1 || $1 -gt 13 || $1 -eq 5 ]]; then
|
||||
>&2 echo "$1 is not a valid Tram in Dresden"
|
||||
exit 1
|
||||
fi
|
||||
#if [[ $1 -lt 1 || $1 -gt 13 || $1 -eq 5 ]]; then
|
||||
# >&2 echo "$1 is not a valid Tram in Dresden"
|
||||
# exit 1
|
||||
#fi
|
||||
|
||||
if [[ ! -x "$(command -v jq)" ]]; then
|
||||
>&2 echo "You need to have 'jq' installed. You can download it here: https://jqlang.org/download/"
|
||||
@@ -18,16 +18,16 @@ fi
|
||||
line_num="$1"
|
||||
|
||||
current_date=$(date -Iseconds)
|
||||
window=$(date -Iseconds -d "+10 minutes")
|
||||
window=$(date -Iseconds -d "+5 minutes")
|
||||
|
||||
api="https://api.transitous.org/api"
|
||||
|
||||
response=$(curl -s --get \
|
||||
response=$(curl --get \
|
||||
--data-urlencode "startTime=$current_date" \
|
||||
--data-urlencode "endTime=$current_date" \
|
||||
--data-urlencode "min=51.1626,13.5609" \
|
||||
--data-urlencode "max=50.9973,13.8596" \
|
||||
--data-urlencode "zoom=15" \
|
||||
--data-urlencode "zoom=13" \
|
||||
--data-urlencode "precision=2" \
|
||||
"$api/v1/map/trips")
|
||||
|
||||
@@ -38,13 +38,13 @@ all_trams=$(jq -c "$query" <<< "$response")
|
||||
# echo "$all_trams"
|
||||
|
||||
tripId=$(jq -r '.[0].trips[0].tripId' <<< "$all_trams")
|
||||
response=$(curl -s --get \
|
||||
response=$(curl --get \
|
||||
--data-urlencode "tripId=$tripId" \
|
||||
"$api/v6/trip")
|
||||
|
||||
trip=$(jq -c '[.legs[0].from] + [.legs[0].intermediateStops[]] + [.legs[0].to] | map({stopId: .stopId, name: .name})' <<< $response)
|
||||
|
||||
# jq <<< $trip
|
||||
# jq -c <<< $trip
|
||||
|
||||
currentStops=$(jq -c 'map(.from.stopId[:-2])' <<< "$all_trams")
|
||||
|
||||
Reference in New Issue
Block a user