nl_ovapi.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package traffic
  5. import (
  6. "strings"
  7. "apiote.xyz/p/szczanieckiej/transformers"
  8. "bufio"
  9. "encoding/csv"
  10. "fmt"
  11. "io"
  12. "net/http"
  13. "os"
  14. "path/filepath"
  15. "time"
  16. "golang.org/x/text/transform"
  17. )
  18. type NlOvapi struct {
  19. client http.Client
  20. }
  21. func (NlOvapi) getTimezone() *time.Location {
  22. l, _ := time.LoadLocation("Europe/Amsterdam")
  23. return l
  24. }
  25. func (NlOvapi) ConvertVehicles() ([]Vehicle, error) {
  26. return []Vehicle{}, nil
  27. }
  28. func (NlOvapi) GetVersions(_ 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://gtfs.ovapi.nl/nl/gtfs-nl.zip"
  35. versions = append(versions, version)
  36. return versions, nil
  37. }
  38. func (NlOvapi) String() string {
  39. return "nl_ovapi"
  40. }
  41. func (NlOvapi) RealtimeFeeds() map[RealtimeFeedType]string {
  42. return map[RealtimeFeedType]string{
  43. TRIP_UPDATES: "https://gtfs.ovapi.nl/nl/tripUpdates.pb https://gtfs.ovapi.nl/nl/trainUpdates.pb",
  44. VEHICLE_POSITIONS: "https://gtfs.ovapi.nl/nl/vehiclePositions.pb",
  45. ALERTS: "https://gtfs.ovapi.nl/nl/alerts.pb",
  46. }
  47. }
  48. func (NlOvapi) Transformer() transform.Transformer {
  49. return transform.Chain(transformers.TransformerFY, transformers.TransformerNL, transformers.TransformerDE, transformers.TransformerFR, transformers.TransformerDA)
  50. }
  51. func (NlOvapi) Name() string {
  52. return "The Netherlands"
  53. }
  54. func (NlOvapi) Flags() FeedFlags {
  55. return FeedFlags{
  56. Headsign: HeadsignTripHeadsing,
  57. StopIdFormat: "{{stop_id}}",
  58. StopName: "{{stop_name}} [{{stop_code}}]",
  59. LineName: "{{route_short_name}}",
  60. }
  61. }
  62. func (NlOvapi) FeedPrepareZip(path string) error {
  63. // NOTE fix wrong encoded characters and add ZWJ for German diaeresises
  64. trips2File, err := os.OpenFile(filepath.Join(path, "trips2.txt"), os.O_RDWR|os.O_CREATE, 0644)
  65. if err != nil {
  66. return fmt.Errorf("while opening trips2 file: %w", err)
  67. }
  68. defer trips2File.Close()
  69. w := csv.NewWriter(trips2File)
  70. err = forEachRowWithHeader(filepath.Join(path, "trips.txt"), func(header []string) error {
  71. err := w.Write(header)
  72. if err != nil {
  73. return fmt.Errorf("while writing header: %w", err)
  74. } else {
  75. return nil
  76. }
  77. }, func(offset int64, fields map[string]int, record []string) error {
  78. record[fields["trip_headsign"]] = strings.ReplaceAll(record[fields["trip_headsign"]], "Mönchengladbach", "M"+ZWJ+"önchengladbach")
  79. record[fields["trip_headsign"]] = strings.ReplaceAll(record[fields["trip_headsign"]], "Lagerlöflaan", "Lagerl"+ZWJ+"öflaan") // FIXME Selma Lagerlöf was actually swedish, not German
  80. err := w.Write(record)
  81. if err != nil {
  82. return fmt.Errorf("while writing record: %w", err)
  83. } else {
  84. return nil
  85. }
  86. })
  87. w.Flush()
  88. err = os.Remove(filepath.Join(path, "trips.txt"))
  89. if err != nil {
  90. return fmt.Errorf("while removing trips: %w", err)
  91. }
  92. err = os.Rename(filepath.Join(path, "trips2.txt"), filepath.Join(path, "trips.txt"))
  93. if err != nil {
  94. return fmt.Errorf("while renaming trips: %w", err)
  95. }
  96. stopsFile, err := os.Open(filepath.Join(path, "stops.txt"))
  97. if err != nil {
  98. return fmt.Errorf("while opening stops file: %w", err)
  99. }
  100. defer stopsFile.Close()
  101. stops2File, err := os.OpenFile(filepath.Join(path, "stops2.txt"), os.O_RDWR|os.O_CREATE, 0644)
  102. if err != nil {
  103. return fmt.Errorf("while opening stops2 file: %w", err)
  104. }
  105. defer stops2File.Close()
  106. r := csv.NewReader(bufio.NewReader(stopsFile))
  107. w = csv.NewWriter(stops2File)
  108. header, err := r.Read()
  109. if err != nil {
  110. return fmt.Errorf("while reading stops header: %w", err)
  111. }
  112. fields := map[string]int{}
  113. for i, headerField := range header {
  114. fields[headerField] = i
  115. }
  116. err = w.Write(header)
  117. if err != nil {
  118. return fmt.Errorf("while writing stops header: %w", err)
  119. }
  120. for {
  121. record, err := r.Read()
  122. if err == io.EOF {
  123. break
  124. }
  125. if err != nil {
  126. return fmt.Errorf("while reading a stop record: %w", err)
  127. }
  128. switch record[fields["stop_code"]] {
  129. case "HA2700":
  130. fallthrough
  131. case "HA2957":
  132. record[fields["stop_name"]] = "Rotterdam, Port Saïdstraat"
  133. case "HA2102":
  134. fallthrough
  135. case "HA2103":
  136. record[fields["stop_name"]] = "Rotterdam, Selma Lagerlöfweg"
  137. case "HA2401":
  138. record[fields["stop_name"]] = "Rotterdam, Caïrostraat"
  139. case "HA2473":
  140. fallthrough
  141. case "HA2496":
  142. record[fields["stop_name"]] = "Vlaardingen, Verploegh Chasséplein"
  143. }
  144. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "›", "ø")
  145. // FIXME Atatürk was actually Turkish
  146. record[fields["stop_code"]] = strings.ReplaceAll(record[fields["stop_code"]], "Bürgermeisteramt", "B"+ZWJ+"ürgermeisteramt")
  147. record[fields["stop_code"]] = strings.ReplaceAll(record[fields["stop_code"]], "Händelstraat", "H"+ZWJ+"ändelstraat")
  148. record[fields["stop_code"]] = strings.ReplaceAll(record[fields["stop_code"]], "Grünerweg", "Gr"+ZWJ+"ünerweg")
  149. record[fields["stop_code"]] = strings.ReplaceAll(record[fields["stop_code"]], "Händellaan", "H"+ZWJ+"ändellaan")
  150. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Mönchengladbach", "M"+ZWJ+"önchengladbach")
  151. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Lagerlöflaan", "Lagerl"+ZWJ+"öflaan") // FIXME Selma Lagerlöf was actually swedish, not German
  152. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Händelstraat", "H"+ZWJ+"ändelstraat")
  153. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Händellaan", "H"+ZWJ+"ändellaan")
  154. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Bürgermeisteramt", "B"+ZWJ+"ürgermeisteramt")
  155. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Tütthees", "T"+ZWJ+"ütthees")
  156. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Bahnübergang", "Bahn"+ZWJ+"übergang")
  157. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Nütterden", "N"+ZWJ+"ütterden")
  158. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Schützenhaus", "Sch"+ZWJ+"ützenhaus")
  159. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Mühle", "M"+ZWJ+"ühle")
  160. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Grünerweg", "Gr"+ZWJ+"ünerweg")
  161. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Brückenkopf", "Br"+ZWJ+"ückenkopf")
  162. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Löwe", "L"+ZWJ+"öwe")
  163. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Nütterden", "N"+ZWJ+"ütterden")
  164. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Donsbrüggen", "Donsbr"+ZWJ+"üggen")
  165. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Lückerheide", "L"+ZWJ+"ückerheide")
  166. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Gässchen", "G"+ZWJ+"ässchen")
  167. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Tüddern", "T"+ZWJ+"üddern")
  168. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Süsterseel", "S"+ZWJ+"üsterseel")
  169. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Höngen", "H"+ZWJ+"öngen")
  170. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Tüddern", "T"+ZWJ+"üddern")
  171. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Höfgen", "H"+ZWJ+"öfgen")
  172. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Düsseldorf", "D"+ZWJ+"üsseldorf")
  173. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Osnabrück", "Osnabr"+ZWJ+"ück")
  174. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Schönefeld", "Sch"+ZWJ+"önefeld")
  175. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Südkreuz", "S"+ZWJ+"üdkreuz")
  176. record[fields["stop_name"]] = strings.ReplaceAll(record[fields["stop_name"]], "Dülken", "D"+ZWJ+"ülken")
  177. err = w.Write(record)
  178. if err != nil {
  179. return fmt.Errorf("while writing a stop record: %w", err)
  180. }
  181. }
  182. w.Flush()
  183. err = os.Remove(filepath.Join(path, "stops.txt"))
  184. if err != nil {
  185. return fmt.Errorf("while removing stops: %w", err)
  186. }
  187. err = os.Rename(filepath.Join(path, "stops2.txt"), filepath.Join(path, "stops.txt"))
  188. if err != nil {
  189. return fmt.Errorf("while renaming stops: %w", err)
  190. }
  191. routesFile, err := os.Open(filepath.Join(path, "routes.txt"))
  192. if err != nil {
  193. return fmt.Errorf("while opening routes file: %w", err)
  194. }
  195. defer routesFile.Close()
  196. routes2File, err := os.OpenFile(filepath.Join(path, "routes2.txt"), os.O_RDWR|os.O_CREATE, 0644)
  197. if err != nil {
  198. return fmt.Errorf("while opening routes2 file: %w", err)
  199. }
  200. defer routes2File.Close()
  201. r = csv.NewReader(bufio.NewReader(routesFile))
  202. w = csv.NewWriter(routes2File)
  203. header, err = r.Read()
  204. if err != nil {
  205. return fmt.Errorf("while reading routes header: %w", err)
  206. }
  207. fields = map[string]int{}
  208. for i, headerField := range header {
  209. fields[headerField] = i
  210. }
  211. err = w.Write(header)
  212. if err != nil {
  213. return fmt.Errorf("while writing routes header: %w", err)
  214. }
  215. for {
  216. record, err := r.Read()
  217. if err == io.EOF {
  218. break
  219. }
  220. if err != nil {
  221. return fmt.Errorf("while reading a route record: %w", err)
  222. }
  223. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Mönchengladbach", "M"+ZWJ+"önchengladbach")
  224. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Lagerlöflaan", "Lagerl"+ZWJ+"öflaan") // FIXME Selma Lagerlöf was actually swedish, not German
  225. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Händelstraat", "H"+ZWJ+"ändelstraat")
  226. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Händellaan", "H"+ZWJ+"ändellaan")
  227. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Bürgermeisteramt", "B"+ZWJ+"ürgermeisteramt")
  228. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Tütthees", "T"+ZWJ+"ütthees")
  229. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Bahnübergang", "Bahn"+ZWJ+"übergang")
  230. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Nütterden", "N"+ZWJ+"ütterden")
  231. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Schützenhaus", "Sch"+ZWJ+"ützenhaus")
  232. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Mühle", "M"+ZWJ+"ühle")
  233. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Grünerweg", "Gr"+ZWJ+"ünerweg")
  234. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Brückenkopf", "Br"+ZWJ+"ückenkopf")
  235. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Löwe", "L"+ZWJ+"öwe")
  236. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Nütterden", "N"+ZWJ+"ütterden")
  237. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Donsbrüggen", "Donsbr"+ZWJ+"üggen")
  238. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Lückerheide", "L"+ZWJ+"ückerheide")
  239. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Gässchen", "G"+ZWJ+"ässchen")
  240. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Tüddern", "T"+ZWJ+"üddern")
  241. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Süsterseel", "S"+ZWJ+"üsterseel")
  242. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Höngen", "H"+ZWJ+"öngen")
  243. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Tüddern", "T"+ZWJ+"üddern")
  244. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Höfgen", "H"+ZWJ+"öfgen")
  245. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Düsseldorf", "D"+ZWJ+"üsseldorf")
  246. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Osnabrück", "Osnabr"+ZWJ+"ück")
  247. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Schönefeld", "Sch"+ZWJ+"önefeld")
  248. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Südkreuz", "S"+ZWJ+"üdkreuz")
  249. record[fields["route_long_name"]] = strings.ReplaceAll(record[fields["route_long_name"]], "Dülken", "D"+ZWJ+"ülken")
  250. err = w.Write(record)
  251. if err != nil {
  252. return fmt.Errorf("while writing a route record: %w", err)
  253. }
  254. }
  255. w.Flush()
  256. err = os.Remove(filepath.Join(path, "routes.txt"))
  257. if err != nil {
  258. return fmt.Errorf("while removing routes: %w", err)
  259. }
  260. err = os.Rename(filepath.Join(path, "routes2.txt"), filepath.Join(path, "routes.txt"))
  261. if err != nil {
  262. return fmt.Errorf("while renaming routes: %w", err)
  263. }
  264. return nil
  265. }
  266. func (NlOvapi) QRInfo() (string, QRLocation, string) {
  267. return "", QRLocationNone, ""
  268. }