brussels_stib_mivb.go 4.3 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/transformers"
  7. "bufio"
  8. "encoding/csv"
  9. "encoding/xml"
  10. "fmt"
  11. "io"
  12. "net/http"
  13. "os"
  14. "path/filepath"
  15. "time"
  16. "golang.org/x/text/transform"
  17. "golang.org/x/tools/blog/atom"
  18. )
  19. type BrusselsStibMivb struct {
  20. client http.Client
  21. }
  22. func (BrusselsStibMivb) getTimezone() *time.Location {
  23. l, _ := time.LoadLocation("Europe/Brussels")
  24. return l
  25. }
  26. func (BrusselsStibMivb) ConvertVehicles() ([]Vehicle, error) {
  27. return []Vehicle{}, nil
  28. }
  29. func (z BrusselsStibMivb) GetVersions(date time.Time, timezone *time.Location) ([]Version, error) {
  30. url := "https://stibmivb.opendatasoft.com/explore/dataset/gtfs-files-production/atom/"
  31. response, err := z.client.Get(url)
  32. if err != nil {
  33. return []Version{}, fmt.Errorf("GetVersions: cannot GET ‘%s’: %w", url, err)
  34. }
  35. decoder := xml.NewDecoder(response.Body)
  36. versionsFeed := atom.Feed{}
  37. decoder.Decode(&versionsFeed)
  38. if len(versionsFeed.Entry) == 0 {
  39. return []Version{}, nil
  40. }
  41. updated, err := time.Parse("2006-01-02T15:04:05.000000-07:00", string(versionsFeed.Entry[0].Updated))
  42. validityString := updated.Format("20060102") + "_99991231"
  43. version, err := MakeVersionTimezone(validityString, timezone)
  44. if err != nil {
  45. return nil, err
  46. }
  47. version.Link = "https://stibmivb.opendatasoft.com/api/explore/v2.1/catalog/datasets/gtfs-files-production/alternative_exports/gtfszip/"
  48. versions := []Version{version}
  49. return versions, nil
  50. }
  51. func (BrusselsStibMivb) String() string {
  52. return "brussels_stib_mivb"
  53. }
  54. func (BrusselsStibMivb) RealtimeFeeds() map[RealtimeFeedType]string {
  55. return map[RealtimeFeedType]string{}
  56. }
  57. func (BrusselsStibMivb) Transformer() transform.Transformer {
  58. return transform.Chain(transformers.TransformerFR, transformers.TransformerNL)
  59. }
  60. func (BrusselsStibMivb) Name() string {
  61. return "Brussels STIB-MIVB"
  62. }
  63. func (BrusselsStibMivb) Flags() FeedFlags {
  64. return FeedFlags{
  65. Headsign: HeadsignTripHeadsing,
  66. StopIdFormat: "{{stop_id}}",
  67. StopName: "{{stop_name}} [{{stop_id}}]",
  68. LineName: "{{route_short_name}}",
  69. }
  70. }
  71. func (BrusselsStibMivb) FeedPrepareZip(path string) error {
  72. // add feed info
  73. feedInfoFile, err := os.OpenFile(filepath.Join(path, "feed_info.txt"), os.O_RDWR|os.O_CREATE, 0644)
  74. if err != nil {
  75. return fmt.Errorf("while opening feedInfo file: %w", err)
  76. }
  77. defer feedInfoFile.Close()
  78. w := csv.NewWriter(feedInfoFile)
  79. err = w.Write([]string{"feed_publisher_name", "feed_publisher_url", "feed_lang", "default_lang"})
  80. if err != nil {
  81. return fmt.Errorf("while writing header: %w", err)
  82. }
  83. err = w.Write([]string{"STIB-MIVB", "https://www.stib-mivb.be", "mul", "fr"})
  84. if err != nil {
  85. return fmt.Errorf("while writing record: %w", err)
  86. }
  87. w.Flush()
  88. // fix number of fields in stops
  89. stopsFile, err := os.Open(filepath.Join(path, "stops.txt"))
  90. if err != nil {
  91. return fmt.Errorf("while opening stops file: %w", err)
  92. }
  93. defer stopsFile.Close()
  94. stops2File, err := os.OpenFile(filepath.Join(path, "stops2.txt"), os.O_RDWR|os.O_CREATE, 0644)
  95. if err != nil {
  96. return fmt.Errorf("while opening stops2 file: %w", err)
  97. }
  98. defer stops2File.Close()
  99. r := csv.NewReader(bufio.NewReader(stopsFile))
  100. r.FieldsPerRecord = -1
  101. w = csv.NewWriter(stops2File)
  102. header, err := r.Read()
  103. if err != nil {
  104. return fmt.Errorf("while reading stops header: %w", err)
  105. }
  106. fields := map[string]int{}
  107. for i, headerField := range header {
  108. fields[headerField] = i
  109. }
  110. err = w.Write(header)
  111. if err != nil {
  112. return fmt.Errorf("while writing stops header: %w", err)
  113. }
  114. for {
  115. record, err := r.Read()
  116. if err == io.EOF {
  117. break
  118. }
  119. if err != nil {
  120. return fmt.Errorf("while reading a stop record: %w", err)
  121. }
  122. for len(record) < len(header) {
  123. record = append(record, "")
  124. }
  125. err = w.Write(record)
  126. if err != nil {
  127. return fmt.Errorf("while writing a stop record: %w", err)
  128. }
  129. }
  130. w.Flush()
  131. err = os.Remove(filepath.Join(path, "stops.txt"))
  132. if err != nil {
  133. return fmt.Errorf("while removing stops: %w", err)
  134. }
  135. err = os.Rename(filepath.Join(path, "stops2.txt"), filepath.Join(path, "stops.txt"))
  136. if err != nil {
  137. return fmt.Errorf("while renaming stops: %w", err)
  138. }
  139. return nil
  140. }
  141. func (BrusselsStibMivb) QRInfo() (string, QRLocation, string) {
  142. return "", QRLocationNone, ""
  143. }