feeds.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/file"
  8. "fmt"
  9. "net/http"
  10. "path/filepath"
  11. "sort"
  12. "strings"
  13. "time"
  14. "golang.org/x/text/transform"
  15. )
  16. type Feed interface {
  17. fmt.Stringer
  18. ConvertVehicles() ([]Vehicle, error)
  19. GetVersions(time.Time, *time.Location) ([]Version, error)
  20. RealtimeFeeds() map[RealtimeFeedType]string
  21. Transformer() transform.Transformer
  22. Name() string
  23. Flags() FeedFlags
  24. FeedPrepareZip(string) error
  25. QRInfo() (string, QRLocation, string)
  26. getTimezone() *time.Location
  27. }
  28. type HeadsignSource uint
  29. const (
  30. HeadsignTripHeadsing HeadsignSource = iota
  31. HeadsignTripLastStop
  32. )
  33. type FeedFlags struct {
  34. Headsign HeadsignSource
  35. StopIdFormat string
  36. StopName string
  37. LineName string
  38. }
  39. func FeedPath(cfg config.Config, feed Feed) string {
  40. return filepath.Join(cfg.FeedsPath, feed.String())
  41. }
  42. func RegisterFeeds() map[string]Feed {
  43. return map[string]Feed{
  44. "poznan_ztm": PoznanZtm{
  45. client: http.Client{
  46. Timeout: 30 * time.Second,
  47. },
  48. },
  49. // "krakow_ztp": KrakowZtp{},
  50. "gzm_ztm": GzmZtm{},
  51. "berlin_vbb": BerlinVbb{},
  52. "brussels_stib_mivb": BrusselsStibMivb{},
  53. "calgary_transit": CalgaryTransit{},
  54. "rockhill_myride": RockHillMyRide{},
  55. "london_dft": LondonDft{},
  56. }
  57. }
  58. func MakeVersion(s string) (Version, error) {
  59. version := Version{}
  60. versionDates := strings.Split(s, "_")
  61. if len(versionDates) != 2 {
  62. return version, fmt.Errorf("invalid version string %s, not /.*_.*/", s)
  63. }
  64. versionDates[0] += "000000"
  65. versionDates[1] += "235900"
  66. validFrom, err := time.Parse(ValidityFormatExtended, versionDates[0])
  67. if err != nil {
  68. return version, fmt.Errorf("invalid first part in %s: %w", s, err)
  69. }
  70. validTill, err := time.Parse(ValidityFormatExtended, versionDates[1])
  71. if err != nil {
  72. return version, fmt.Errorf("invalid second part in %s: %w", s, err)
  73. }
  74. version.ValidFrom = validFrom
  75. version.ValidTill = validTill
  76. return version, nil
  77. }
  78. func MakeVersionTimezone(s string, timezone *time.Location) (Version, error) {
  79. version := Version{}
  80. versionDates := strings.Split(s, "_")
  81. if len(versionDates) != 2 {
  82. return version, fmt.Errorf("invalid version string %s, not /.*_.*/", s)
  83. }
  84. versionDates[0] += "000000"
  85. versionDates[1] += "235900"
  86. validFrom, err := time.ParseInLocation(ValidityFormatExtended, versionDates[0], timezone)
  87. if err != nil {
  88. return version, fmt.Errorf("invalid first part in %s: %w", s, err)
  89. }
  90. validTill, err := time.ParseInLocation(ValidityFormatExtended, versionDates[1], timezone)
  91. if err != nil {
  92. return version, fmt.Errorf("invalid second part in %s: %w", s, err)
  93. }
  94. version.ValidFrom = validFrom
  95. version.ValidTill = validTill
  96. return version, nil
  97. }
  98. func ListVersions(cfg config.Config, f Feed) ([]string, error) {
  99. return file.ListVersions(FeedPath(cfg, f))
  100. }
  101. func ListVersionsTimezone(cfg config.Config, f Feed, timezone *time.Location) ([]Version, error) {
  102. versions := []Version{}
  103. validities, err := ListVersions(cfg, f)
  104. if err != nil {
  105. return versions, fmt.Errorf("while listing versions: %w", err)
  106. }
  107. for _, validity := range validities {
  108. version, err := MakeVersionTimezone(validity, timezone)
  109. if err != nil {
  110. return versions, fmt.Errorf("while making version of %s: %w", validity, err)
  111. }
  112. versions = append(versions, version)
  113. }
  114. return versions, nil
  115. }
  116. func FindValidVersions(versions []Version, now time.Time) []Version {
  117. result := []Version{}
  118. if len(versions) == 0 {
  119. return versions
  120. }
  121. if len(versions) == 1 {
  122. if versions[0].ValidTill.IsZero() || (now.Before(versions[0].ValidTill) && now.After(versions[0].ValidFrom)) {
  123. return versions
  124. } else {
  125. return []Version{}
  126. }
  127. }
  128. sort.Slice(versions, func(i, j int) bool {
  129. return versions[i].ValidFrom.After(versions[j].ValidFrom)
  130. })
  131. for _, version := range versions {
  132. if version.ValidTill.IsZero() || now.Before(version.ValidFrom) {
  133. result = append([]Version{version}, result...)
  134. continue
  135. }
  136. if now.After(version.ValidFrom) { // TODO and before validTill??
  137. result = append([]Version{version}, result...)
  138. break
  139. }
  140. }
  141. return result
  142. }