berlin_vbb.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. "fmt"
  10. "io"
  11. "net/http"
  12. "os"
  13. "path/filepath"
  14. "strings"
  15. "time"
  16. "golang.org/x/text/transform"
  17. )
  18. type BerlinVbb struct {
  19. client http.Client
  20. }
  21. func (BerlinVbb) getTimezone() *time.Location {
  22. l, _ := time.LoadLocation("Europe/Berlin")
  23. return l
  24. }
  25. func (BerlinVbb) ConvertVehicles() ([]Vehicle, error) {
  26. return []Vehicle{}, nil
  27. }
  28. func (BerlinVbb) GetVersions(date time.Time, timezone *time.Location) ([]Version, error) {
  29. versions := []Version{}
  30. version, err := MakeVersionTimezone("00010101_99991231", timezone)
  31. if err != nil {
  32. return nil, err
  33. }
  34. version.Link = "https://www.vbb.de/fileadmin/user_upload/VBB/Dokumente/API-Datensaetze/gtfs-mastscharf/GTFS.zip"
  35. versions = append(versions, version)
  36. return versions, nil
  37. }
  38. func (BerlinVbb) String() string {
  39. return "berlin_vbb"
  40. }
  41. func (BerlinVbb) RealtimeFeeds() map[RealtimeFeedType]string {
  42. return map[RealtimeFeedType]string{}
  43. }
  44. func (BerlinVbb) Transformer() transform.Transformer {
  45. return transform.Chain(transformers.TransformerDE, transformers.TransformerPL, transformers.TransformerFR)
  46. }
  47. func (BerlinVbb) Name() string {
  48. return "Berlin/Brandenburg VBB"
  49. }
  50. func (BerlinVbb) Flags() FeedFlags {
  51. return FeedFlags{
  52. Headsign: HeadsignTripHeadsing,
  53. StopIdFormat: "{{stop_id}}_{{platform_code}}",
  54. StopName: "{{stop_name}} | {{platform_code}}", // TODO platform_code might not be present, but currently templating doesn’t support this
  55. LineName: "{{route_short_name}}",
  56. }
  57. }
  58. func (BerlinVbb) FeedPrepareZip(path string) error {
  59. // TODO remove or fix zones
  60. // NOTE add non-printable characrer before umlauts
  61. trips2File, err := os.OpenFile(filepath.Join(path, "trips2.txt"), os.O_RDWR|os.O_CREATE, 0644)
  62. if err != nil {
  63. return fmt.Errorf("while opening trips2 file: %w", err)
  64. }
  65. defer trips2File.Close()
  66. w := csv.NewWriter(trips2File)
  67. err = forEachRowWithHeader(filepath.Join(path, "trips.txt"), func(header []string) error {
  68. err := w.Write(header)
  69. if err != nil {
  70. return fmt.Errorf("while writing header: %w", err)
  71. } else {
  72. return nil
  73. }
  74. }, func(offset int64, fields map[string]int, record []string) error {
  75. record[fields["trip_headsign"]] = strings.ReplaceAll(record[fields["trip_headsign"]], "ü", ZWJ+"ü")
  76. record[fields["trip_headsign"]] = strings.ReplaceAll(record[fields["trip_headsign"]], "Ü", ZWJ+"Ü")
  77. record[fields["trip_headsign"]] = strings.ReplaceAll(record[fields["trip_headsign"]], "ä", ZWJ+"ä")
  78. record[fields["trip_headsign"]] = strings.ReplaceAll(record[fields["trip_headsign"]], "Ä", ZWJ+"Ä")
  79. record[fields["trip_headsign"]] = strings.ReplaceAll(record[fields["trip_headsign"]], "ö", ZWJ+"ö")
  80. record[fields["trip_headsign"]] = strings.ReplaceAll(record[fields["trip_headsign"]], "Ö", ZWJ+"Ö")
  81. err := w.Write(record)
  82. if err != nil {
  83. return fmt.Errorf("while writing record: %w", err)
  84. } else {
  85. return nil
  86. }
  87. })
  88. w.Flush()
  89. err = os.Remove(filepath.Join(path, "trips.txt"))
  90. if err != nil {
  91. return fmt.Errorf("while removing trips: %w", err)
  92. }
  93. err = os.Rename(filepath.Join(path, "trips2.txt"), filepath.Join(path, "trips.txt"))
  94. if err != nil {
  95. return fmt.Errorf("while renaming trips: %w", err)
  96. }
  97. // NOTE add platform codes from stop_id and add nonprintable character before umlauts
  98. stopsFile, err := os.Open(filepath.Join(path, "stops.txt"))
  99. if err != nil {
  100. return fmt.Errorf("while opening stops file: %w", err)
  101. }
  102. defer stopsFile.Close()
  103. stops2File, err := os.OpenFile(filepath.Join(path, "stops2.txt"), os.O_RDWR|os.O_CREATE, 0644)
  104. if err != nil {
  105. return fmt.Errorf("while opening stops2 file: %w", err)
  106. }
  107. defer stops2File.Close()
  108. r := csv.NewReader(bufio.NewReader(stopsFile))
  109. w = csv.NewWriter(stops2File)
  110. header, err := r.Read()
  111. if err != nil {
  112. return fmt.Errorf("while reading stops header: %w", err)
  113. }
  114. fields := map[string]int{}
  115. for i, headerField := range header {
  116. fields[headerField] = i
  117. }
  118. err = w.Write(header)
  119. if err != nil {
  120. return fmt.Errorf("while writing stops header: %w", err)
  121. }
  122. for {
  123. record, err := r.Read()
  124. if err == io.EOF {
  125. break
  126. }
  127. if err != nil {
  128. return fmt.Errorf("while reading a stop record: %w", err)
  129. }
  130. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "ü", ZWJ+"ü")
  131. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Ü", ZWJ+"Ü")
  132. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "ä", ZWJ+"ä")
  133. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Ä", ZWJ+"Ä")
  134. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "ö", ZWJ+"ö")
  135. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Ö", ZWJ+"Ö")
  136. f, ok := fields["location_type"]
  137. if (!ok || record[f] == "" || record[f] == "0") && record[fields["platform_code"]] == "" {
  138. stopID := strings.Split(record[fields["stop_id"]], ":")
  139. if len(stopID) >= 5 {
  140. record[fields["platform_code"]] = stopID[4]
  141. }
  142. }
  143. err = w.Write(record)
  144. if err != nil {
  145. return fmt.Errorf("while writing a stop record: %w", err)
  146. }
  147. }
  148. w.Flush()
  149. err = os.Remove(filepath.Join(path, "stops.txt"))
  150. if err != nil {
  151. return fmt.Errorf("while removing stops: %w", err)
  152. }
  153. err = os.Rename(filepath.Join(path, "stops2.txt"), filepath.Join(path, "stops.txt"))
  154. if err != nil {
  155. return fmt.Errorf("while renaming stops: %w", err)
  156. }
  157. // NOTE fix route types and add nonprintable character before umlauts
  158. routesFile, err := os.Open(filepath.Join(path, "routes.txt"))
  159. if err != nil {
  160. return fmt.Errorf("while opening routes file: %w", err)
  161. }
  162. defer routesFile.Close()
  163. routes2File, err := os.OpenFile(filepath.Join(path, "routes2.txt"), os.O_RDWR|os.O_CREATE, 0644)
  164. if err != nil {
  165. return fmt.Errorf("while opening routes2 file: %w", err)
  166. }
  167. defer routes2File.Close()
  168. r = csv.NewReader(bufio.NewReader(routesFile))
  169. w = csv.NewWriter(routes2File)
  170. header, err = r.Read()
  171. if err != nil {
  172. return fmt.Errorf("while reading routes header: %w", err)
  173. }
  174. fields = map[string]int{}
  175. for i, headerField := range header {
  176. fields[headerField] = i
  177. }
  178. err = w.Write(header)
  179. if err != nil {
  180. return fmt.Errorf("while writing routes header: %w", err)
  181. }
  182. for {
  183. record, err := r.Read()
  184. if err == io.EOF {
  185. break
  186. }
  187. if err != nil {
  188. return fmt.Errorf("while reading a route record: %w", err)
  189. }
  190. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "ü", ZWJ+"ü")
  191. record[fields["route_short_name"]] = strings.ReplaceAll(record[fields["route_short_name"]], "ü", ZWJ+"ü")
  192. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Ü", ZWJ+"Ü")
  193. record[fields["route_short_name"]] = strings.ReplaceAll(record[fields["route_short_name"]], "Ü", ZWJ+"Ü")
  194. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "ä", ZWJ+"ä")
  195. record[fields["route_short_name"]] = strings.ReplaceAll(record[fields["route_short_name"]], "ä", ZWJ+"ä")
  196. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Ä", ZWJ+"Ä")
  197. record[fields["route_short_name"]] = strings.ReplaceAll(record[fields["route_short_name"]], "Ä", ZWJ+"Ä")
  198. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "ö", ZWJ+"ö")
  199. record[fields["route_short_name"]] = strings.ReplaceAll(record[fields["route_short_name"]], "ö", ZWJ+"ö")
  200. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Ö", ZWJ+"Ö")
  201. record[fields["route_short_name"]] = strings.ReplaceAll(record[fields["route_short_name"]], "Ö", ZWJ+"Ö")
  202. if record[fields["route_type"]] == "100" {
  203. record[fields["route_type"]] = "2"
  204. }
  205. if record[fields["route_type"]] == "109" {
  206. record[fields["route_type"]] = "1"
  207. }
  208. if record[fields["route_type"]] == "400" {
  209. record[fields["route_type"]] = "1"
  210. }
  211. if record[fields["route_type"]] == "700" {
  212. record[fields["route_type"]] = "3"
  213. }
  214. if record[fields["route_type"]] == "900" {
  215. record[fields["route_type"]] = "0"
  216. }
  217. if record[fields["route_type"]] == "1000" {
  218. record[fields["route_type"]] = "4"
  219. }
  220. err = w.Write(record)
  221. if err != nil {
  222. return fmt.Errorf("while writing a route record: %w", err)
  223. }
  224. }
  225. w.Flush()
  226. err = os.Remove(filepath.Join(path, "routes.txt"))
  227. if err != nil {
  228. return fmt.Errorf("while removing routes: %w", err)
  229. }
  230. err = os.Rename(filepath.Join(path, "routes2.txt"), filepath.Join(path, "routes.txt"))
  231. if err != nil {
  232. return fmt.Errorf("while renaming routes: %w", err)
  233. }
  234. return nil
  235. }
  236. func (BerlinVbb) QRInfo() (string, QRLocation, string) {
  237. // TODO mapping IDs
  238. return "qr.bvg.de", QRLocationPath, "/(?<SEL>.*)"
  239. }