feeds.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. "nl_ovapi": NlOvapi{},
  56. "jastrzebie-zdroj_mzk": JaztrzebieZdrojMZK{},
  57. "skp_bana": SkpBana{},
  58. }
  59. }
  60. func MakeVersion(s string) (Version, error) {
  61. version := Version{}
  62. versionDates := strings.Split(s, "_")
  63. if len(versionDates) != 2 {
  64. return version, fmt.Errorf("invalid version string %s, not /.*_.*/", s)
  65. }
  66. versionDates[0] += "000000"
  67. versionDates[1] += "235900"
  68. validFrom, err := time.Parse(ValidityFormatExtended, versionDates[0])
  69. if err != nil {
  70. return version, fmt.Errorf("invalid first part in %s: %w", s, err)
  71. }
  72. validTill, err := time.Parse(ValidityFormatExtended, versionDates[1])
  73. if err != nil {
  74. return version, fmt.Errorf("invalid second part in %s: %w", s, err)
  75. }
  76. version.ValidFrom = validFrom
  77. version.ValidTill = validTill
  78. return version, nil
  79. }
  80. func MakeVersionTimezone(s string, timezone *time.Location) (Version, error) {
  81. version := Version{}
  82. versionDates := strings.Split(s, "_")
  83. if len(versionDates) != 2 {
  84. return version, fmt.Errorf("invalid version string %s, not /.*_.*/", s)
  85. }
  86. versionDates[0] += "000000"
  87. versionDates[1] += "235900"
  88. validFrom, err := time.ParseInLocation(ValidityFormatExtended, versionDates[0], timezone)
  89. if err != nil {
  90. return version, fmt.Errorf("invalid first part in %s: %w", s, err)
  91. }
  92. validTill, err := time.ParseInLocation(ValidityFormatExtended, versionDates[1], timezone)
  93. if err != nil {
  94. return version, fmt.Errorf("invalid second part in %s: %w", s, err)
  95. }
  96. version.ValidFrom = validFrom
  97. version.ValidTill = validTill
  98. return version, nil
  99. }
  100. func ListVersions(cfg config.Config, f Feed) ([]string, error) {
  101. return file.ListVersions(FeedPath(cfg, f))
  102. }
  103. func ListVersionsTimezone(cfg config.Config, f Feed, timezone *time.Location) ([]Version, error) {
  104. versions := []Version{}
  105. validities, err := ListVersions(cfg, f)
  106. if err != nil {
  107. return versions, fmt.Errorf("while listing versions: %w", err)
  108. }
  109. for _, validity := range validities {
  110. version, err := MakeVersionTimezone(validity, timezone)
  111. if err != nil {
  112. return versions, fmt.Errorf("while making version of %s: %w", validity, err)
  113. }
  114. versions = append(versions, version)
  115. }
  116. return versions, nil
  117. }
  118. func FindValidVersions(versions []Version, now time.Time) []Version {
  119. result := []Version{}
  120. if len(versions) == 0 {
  121. return versions
  122. }
  123. if len(versions) == 1 {
  124. if versions[0].ValidTill.IsZero() || (now.Before(versions[0].ValidTill) && now.After(versions[0].ValidFrom)) {
  125. return versions
  126. } else {
  127. return []Version{}
  128. }
  129. }
  130. sort.Slice(versions, func(i, j int) bool {
  131. return versions[i].ValidFrom.After(versions[j].ValidFrom)
  132. })
  133. for _, version := range versions {
  134. if version.ValidTill.IsZero() || now.Before(version.ValidFrom) {
  135. result = append([]Version{version}, result...)
  136. continue
  137. }
  138. if now.After(version.ValidFrom) { // TODO and before validTill??
  139. result = append([]Version{version}, result...)
  140. break
  141. }
  142. }
  143. return result
  144. }