123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- // SPDX-FileCopyrightText: Adam Evyčędo
- //
- // SPDX-License-Identifier: AGPL-3.0-or-later
- package traffic
- import (
- "apiote.xyz/p/szczanieckiej/config"
- "apiote.xyz/p/szczanieckiej/transformers"
- "bufio"
- "encoding/csv"
- "fmt"
- "io"
- "net/http"
- "os"
- "path/filepath"
- "regexp"
- "time"
- "golang.org/x/text/transform"
- )
- type GzmZtm struct {
- client http.Client
- }
- func (GzmZtm) getTimezone() *time.Location {
- l, _ := time.LoadLocation("Europe/Warsaw")
- return l
- }
- func (GzmZtm) ConvertVehicles() ([]Vehicle, error) {
- return []Vehicle{}, nil
- }
- func (z GzmZtm) GetVersions(date time.Time, timezone *time.Location) ([]Version, error) {
- url := "https://otwartedane.metropoliagzm.pl/dataset/rozklady-jazdy-i-lokalizacja-przystankow-gtfs"
- response, err := z.client.Get(url)
- if err != nil {
- return []Version{}, fmt.Errorf("GetVersions: cannot GET ‘%s’: %w", url, err)
- }
- doc, err := io.ReadAll(response.Body)
- if err != nil {
- return []Version{}, fmt.Errorf("GetVersions: cannot read whole html: %w", err)
- }
- regex, err := regexp.Compile("https://otwartedane.metropoliagzm.pl/dataset/86b5ce0c-daea-4b40-bc60-af2c80477d21/resource/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/download/schedule_([0-9]{4}).([0-9]{2}).([0-9]{2})_[0-9]{13}_[0-9]{4}.ext_gtfs.zip")
- urls := regex.FindAllStringSubmatch(string(doc), -1)
- versions := []Version{}
- for _, u := range urls {
- version, err := MakeVersionTimezone(u[1]+u[2]+u[3]+"_99991231", timezone)
- if err != nil {
- return nil, err
- }
- version.Link = u[0]
- versions = append(versions, version)
- }
- return versions, nil
- }
- func (GzmZtm) String() string {
- return "gzm_ztm"
- }
- func (GzmZtm) RealtimeFeeds() map[RealtimeFeedType]string {
- return map[RealtimeFeedType]string{
- TRIP_UPDATES: "https://gtfsrt.transportgzm.pl:5443/gtfsrt/gzm/tripUpdates",
- VEHICLE_POSITIONS: "https://gtfsrt.transportgzm.pl:5443/gtfsrt/gzm/vehiclePositions",
- ALERTS: "https://gtfsrt.transoprtgzm.pl:5443/gtfsrt/gzm/info",
- }
- }
- func (GzmZtm) LuaUpdatesScript(config.Auth) string {
- return ""
- }
- func (GzmZtm) Transformer() transform.Transformer {
- return transformers.TransformerPL
- }
- func (GzmZtm) Name() string {
- return "Metropolia GZM ZTM"
- }
- func (GzmZtm) Flags() FeedFlags {
- return FeedFlags{
- Headsign: HeadsignTripLastStop,
- StopIdFormat: "{{stop_id}}",
- StopName: "{{stop_name}} | {{stop_code}}",
- LineName: "{{route_long_name}}",
- }
- }
- func (GzmZtm) FeedPrepareZip(path string) error {
- // rename stops_ext to stops
- err := os.Remove(filepath.Join(path, "stops.txt"))
- if err != nil {
- return fmt.Errorf("while removing stops: %w", err)
- }
- err = os.Rename(filepath.Join(path, "stops_ext.txt"), filepath.Join(path, "stops.txt"))
- if err != nil {
- return fmt.Errorf("while renaming stops: %w", err)
- }
- // onDemand to pickup_type
- stopsFile, err := os.Open(filepath.Join(path, "stops.txt"))
- if err != nil {
- return fmt.Errorf("while opening stops file: %w", err)
- }
- defer stopsFile.Close()
- r := csv.NewReader(bufio.NewReader(stopsFile))
- header, err := r.Read()
- if err != nil {
- return fmt.Errorf("while reading stops header: %w", err)
- }
- fields := map[string]int{}
- for i, headerField := range header {
- fields[headerField] = i
- }
- stopTypes := map[string]string{}
- for {
- record, err := r.Read()
- if err == io.EOF {
- break
- }
- if err != nil {
- return fmt.Errorf("while reading a stops record: %w", err)
- }
- stopTypeID := record[fields["stop_type_id"]]
- stopID := record[fields["stop_id"]]
- stopTypes[stopID] = stopTypeID
- }
- stopTimesFile, err := os.Open(filepath.Join(path, "stop_times.txt"))
- if err != nil {
- return fmt.Errorf("while opening stop_times file: %w", err)
- }
- defer stopTimesFile.Close()
- stopTimes2File, err := os.OpenFile(filepath.Join(path, "stop_times2.txt"), os.O_RDWR|os.O_CREATE, 0644)
- if err != nil {
- return fmt.Errorf("while opening stop_times2 file: %w", err)
- }
- defer stopTimes2File.Close()
- r = csv.NewReader(stopTimesFile)
- w := csv.NewWriter(stopTimes2File)
- header, err = r.Read()
- if err != nil {
- return fmt.Errorf("while reading stop_times header: %w", err)
- }
- header = append(header, "pickup_type")
- header = append(header, "drop_off_type")
- fields = map[string]int{}
- for i, headerField := range header {
- fields[headerField] = i
- }
- err = w.Write(header)
- if err != nil {
- return fmt.Errorf("while writing the stops header: %w", err)
- }
- for {
- record, err := r.Read()
- if err == io.EOF {
- break
- }
- if err != nil {
- return fmt.Errorf("while reading stop_times record: %w", err)
- }
- stopID := record[fields["stop_id"]]
- switch stopTypes[stopID] {
- case "2":
- record = append(record, "3")
- record = append(record, "3")
- case "3":
- record = append(record, "0")
- record = append(record, "0")
- default:
- return fmt.Errorf("unknown stop_type_id: %s for stop: %s", stopTypes[stopID], stopID)
- }
- err = w.Write(record)
- if err != nil {
- return fmt.Errorf("while writing a stops record: %w", err)
- }
- }
- w.Flush()
- err = w.Error()
- if err != nil {
- return fmt.Errorf("while flushing stops: %w", err)
- }
- err = os.Remove(filepath.Join(path, "stop_times.txt"))
- if err != nil {
- return fmt.Errorf("while removing stop_times: %w", err)
- }
- err = os.Rename(filepath.Join(path, "stop_times2.txt"), filepath.Join(path, "stop_times.txt"))
- if err != nil {
- return fmt.Errorf("while renaming stop_times: %w", err)
- }
- // route_type 800 -> 11
- routesFile, err := os.Open(filepath.Join(path, "routes.txt"))
- if err != nil {
- return fmt.Errorf("while opening routes file: %w", err)
- }
- defer routesFile.Close()
- routes2File, err := os.OpenFile(filepath.Join(path, "routes2.txt"), os.O_RDWR|os.O_CREATE, 0644)
- if err != nil {
- return fmt.Errorf("while opening routes2 file: %w", err)
- }
- defer routes2File.Close()
- r = csv.NewReader(bufio.NewReader(routesFile))
- w = csv.NewWriter(routes2File)
- header, err = r.Read()
- if err != nil {
- return fmt.Errorf("while reading routes header: %w", err)
- }
- fields = map[string]int{}
- for i, headerField := range header {
- fields[headerField] = i
- }
- err = w.Write(header)
- if err != nil {
- return fmt.Errorf("while writing routes header: %w", err)
- }
- for {
- record, err := r.Read()
- if err == io.EOF {
- break
- }
- if err != nil {
- return fmt.Errorf("while reading a route record: %w", err)
- }
- if record[fields["route_type"]] == "800" {
- record[fields["route_type"]] = "11"
- }
- err = w.Write(record)
- if err != nil {
- return fmt.Errorf("while writing a route record: %w", err)
- }
- }
- w.Flush()
- err = w.Error()
- if err != nil {
- return fmt.Errorf("while flushing routes: %w", err)
- }
- err = os.Remove(filepath.Join(path, "routes.txt"))
- if err != nil {
- return fmt.Errorf("while removing routes: %w", err)
- }
- err = os.Rename(filepath.Join(path, "routes2.txt"), filepath.Join(path, "routes.txt"))
- if err != nil {
- return fmt.Errorf("while renaming routes: %w", err)
- }
- return nil
- }
- func (GzmZtm) QRInfo() (string, QRLocation, string) {
- return "rj.metropoliaztm.pl", QRLocationPath, "/redir/stop/(?<stop>[^/]+)"
- }
|