gzm_ztm.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package traffic
  5. import (
  6. "apiote.xyz/p/szczanieckiej/config"
  7. "apiote.xyz/p/szczanieckiej/transformers"
  8. "fmt"
  9. "io"
  10. "net/http"
  11. "regexp"
  12. "time"
  13. "golang.org/x/text/transform"
  14. )
  15. type GzmZtm struct {
  16. client http.Client
  17. }
  18. func (GzmZtm) getTimezone() *time.Location {
  19. l, _ := time.LoadLocation("Europe/Warsaw")
  20. return l
  21. }
  22. func (GzmZtm) ConvertVehicles() ([]Vehicle, error) {
  23. return []Vehicle{}, nil
  24. }
  25. func (z GzmZtm) GetVersions(date time.Time, timezone *time.Location) ([]Version, error) {
  26. url := "https://otwartedane.metropoliagzm.pl/dataset/rozklady-jazdy-i-lokalizacja-przystankow-gtfs-wersja-rozszerzona"
  27. response, err := z.client.Get(url)
  28. if err != nil {
  29. return []Version{}, fmt.Errorf("GetVersions: cannot GET ‘%s’: %w", url, err)
  30. }
  31. doc, err := io.ReadAll(response.Body)
  32. if err != nil {
  33. return []Version{}, fmt.Errorf("GetVersions: cannot read whole html: %w", err)
  34. }
  35. regex, err := regexp.Compile(`/dataset/rozklady-jazdy-i-lokalizacja-przystankow-gtfs-wersja-rozszerzona/resource/([0-9a-f\-]+)" title="schedule_ZTM_([0-9]{4}).([0-9]{2}).([0-9]{2})_([0-9]+)_([0-9]{4}).zip"`)
  36. urls := regex.FindAllStringSubmatch(string(doc), -1)
  37. versions := []Version{}
  38. for _, u := range urls {
  39. versions = append(versions, Version{
  40. Link: "https://otwartedane.metropoliagzm.pl/dataset/rozklady-jazdy-i-lokalizacja-przystankow-gtfs-wersja-rozszerzona/resource/" + u[1] + "/download/schedule_ztm_" + u[2] + "." + u[3] + "." + u[4] + "_" + u[5] + "_" + u[6] + "zip",
  41. })
  42. }
  43. return versions, nil
  44. }
  45. func (GzmZtm) String() string {
  46. return "gzm_ztm"
  47. }
  48. func (GzmZtm) RealtimeFeeds() map[RealtimeFeedType]string {
  49. return map[RealtimeFeedType]string{
  50. TRIP_UPDATES: "https://gtfsrt.transportgzm.pl:5443/gtfsrt/gzm/tripUpdates",
  51. VEHICLE_POSITIONS: "https://gtfsrt.transportgzm.pl:5443/gtfsrt/gzm/vehiclePositions",
  52. ALERTS: "https://gtfsrt.transoprtgzm.pl:5443/gtfsrt/gzm/info",
  53. }
  54. }
  55. func (GzmZtm) LuaUpdatesScript(config.Auth) string {
  56. return ""
  57. }
  58. func (GzmZtm) Transformer() transform.Transformer {
  59. return transformers.TransformerPL
  60. }
  61. func (GzmZtm) Name() string {
  62. return "Metropolia GZM ZTM"
  63. }
  64. func (GzmZtm) Flags() FeedFlags {
  65. return FeedFlags{
  66. Headsign: HeadsignTripHeadsing,
  67. StopIdFormat: "{{stop_id}}",
  68. StopName: "{{stop_name}} [{{stop_code}}]",
  69. LineName: "{{route_short_name}}",
  70. }
  71. }
  72. func (GzmZtm) FeedPrepareZip(path string) error {
  73. return nil
  74. }
  75. func (GzmZtm) QRInfo() (string, QRLocation, string) {
  76. return "rj.metropoliaztm.pl", QRLocationPath, "/redir/stop/(?<stop>[^/]+)"
  77. }