rockhill_myride.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package traffic
  5. import (
  6. "bufio"
  7. "encoding/csv"
  8. "fmt"
  9. "io"
  10. "net/http"
  11. "os"
  12. "path/filepath"
  13. "time"
  14. "golang.org/x/text/transform"
  15. )
  16. type RockHillMyRide struct {
  17. client http.Client
  18. }
  19. func (RockHillMyRide) getTimezone() *time.Location {
  20. l, _ := time.LoadLocation("America/New_York")
  21. return l
  22. }
  23. func (g RockHillMyRide) ConvertVehicles() ([]Vehicle, error) {
  24. return []Vehicle{}, nil
  25. }
  26. func (z RockHillMyRide) GetVersions(_ time.Time, timezone *time.Location) ([]Version, error) {
  27. version, err := MakeVersionTimezone("00000101_99991231", timezone)
  28. if err != nil {
  29. return nil, err
  30. }
  31. version.Link = "https://rapid.nationalrtap.org/GTFSFileManagement/UserUploadFiles/11718/myriderockhill-sc-us.zip"
  32. return []Version{version}, nil
  33. }
  34. func (RockHillMyRide) String() string {
  35. return "rockhill_myride"
  36. }
  37. func (RockHillMyRide) RealtimeFeeds() map[RealtimeFeedType]string {
  38. return map[RealtimeFeedType]string{}
  39. }
  40. func (RockHillMyRide) Transformer() transform.Transformer {
  41. return transform.Nop
  42. }
  43. func (RockHillMyRide) Name() string {
  44. return "Rock Hill, SC My Ride"
  45. }
  46. func (RockHillMyRide) Flags() FeedFlags {
  47. return FeedFlags{
  48. Headsign: HeadsignTripHeadsing,
  49. StopIdFormat: "{{stop_code}}",
  50. StopName: "{{stop_name}} [{{stop_code}}]",
  51. LineName: "{{route_short_name}} {{route_long_name}}",
  52. }
  53. }
  54. func (RockHillMyRide) FeedPrepareZip(path string) error {
  55. // NOTE add timepoint
  56. stopTimesFile, err := os.Open(filepath.Join(path, "stop_times.txt"))
  57. if err != nil {
  58. return fmt.Errorf("while opening stop_times file: %w", err)
  59. }
  60. defer stopTimesFile.Close()
  61. stopTimes2File, err := os.OpenFile(filepath.Join(path, "stop_times2.txt"), os.O_RDWR|os.O_CREATE, 0644)
  62. if err != nil {
  63. return fmt.Errorf("while opening stop_times2 file: %w", err)
  64. }
  65. defer stopTimes2File.Close()
  66. r := csv.NewReader(bufio.NewReader(stopTimesFile))
  67. w := csv.NewWriter(stopTimes2File)
  68. header, err := r.Read()
  69. if err != nil {
  70. return fmt.Errorf("while reading stop_times header: %w", err)
  71. }
  72. fields := map[string]int{}
  73. for i, headerField := range header {
  74. fields[headerField] = i
  75. }
  76. header = append(header, "timepoint")
  77. err = w.Write(header)
  78. if err != nil {
  79. return fmt.Errorf("while writing stop_times header: %w", err)
  80. }
  81. for {
  82. record, err := r.Read()
  83. if err == io.EOF {
  84. break
  85. }
  86. if err != nil {
  87. return fmt.Errorf("while reading a stop_times record: %w", err)
  88. }
  89. arrivalTime := record[fields["arrival_time"]]
  90. if arrivalTime != "" {
  91. record = append(record, "1")
  92. } else {
  93. record = append(record, "0")
  94. }
  95. err = w.Write(record)
  96. if err != nil {
  97. return fmt.Errorf("while writing a stop_times record: %w", err)
  98. }
  99. }
  100. w.Flush()
  101. err = os.Remove(filepath.Join(path, "stop_times.txt"))
  102. if err != nil {
  103. return fmt.Errorf("while removing stop_times: %w", err)
  104. }
  105. err = os.Rename(filepath.Join(path, "stop_times2.txt"), filepath.Join(path, "stop_times.txt"))
  106. if err != nil {
  107. return fmt.Errorf("while renaming stop_times: %w", err)
  108. }
  109. return nil
  110. }
  111. func (RockHillMyRide) QRInfo() (string, QRLocation, string) {
  112. return "", QRLocationNone, ""
  113. }