brussels_stib_mivb_alerts.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. -- SPDX-FileCopyrightText: Adam Evyčędo
  2. --
  3. -- SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---@diagnostic disable-next-line: lowercase-global, unused-local
  5. function getAlerts(stopID, stopCode, tripID, lineID)
  6. local whereClause = ""
  7. if lineID ~= '' then
  8. whereClause = "where=lines%20like%20'" .. lineID .. "'"
  9. elseif stopID ~= '' then
  10. whereClause = "where=points%20like%20'" .. stopID .. "'"
  11. end
  12. local http = require("http")
  13. local json = require("json")
  14. local error_struct = {
  15. httpResponseCode=0,
  16. message="",
  17. willNextRequestFail=false
  18. }
  19. local response, error_message = http.get("https://stibmivb.opendatasoft.com/api/explore/v2.1/catalog/datasets/travellers-information-rt-production/records?" .. whereClause, {
  20. timeout="30s",
  21. })
  22. if response == nil then
  23. error_struct.message = "while getting alerts: " .. error_message
  24. local error_json, _ = json.encode(error_struct)
  25. return "", error_json
  26. end
  27. if response.status_code ~= 200 then
  28. error_struct.message = "api returned code " .. response.status_code .. "; " .. response.body
  29. error_struct.httpResponseCode = response.status_code
  30. error_struct.willNextRequestFail = true
  31. local error_json, _ = json.encode(error_struct)
  32. return "", error_json
  33. end
  34. ---@diagnostic disable-next-line: redefined-local
  35. local struct, error_message = json.decode(response.body)
  36. if struct == nil then
  37. error_struct.message = "while decoding alerts: " .. error_message
  38. local error_json, _ = json.encode(error_struct)
  39. return "", error_json
  40. end
  41. local alerts = {}
  42. local i = 1
  43. for _, entry in ipairs(struct.results) do
  44. ---@diagnostic disable-next-line: redefined-local
  45. local content, error_message = json.decode(entry.content)
  46. if content == nil then
  47. error_struct.message = "while decoding alert text: " .. error_message
  48. end
  49. ---@diagnostic disable-next-line: param-type-mismatch
  50. for _, alertContent in ipairs(content) do
  51. alerts[i] = {
  52. timeRanges={
  53. {
  54. startDate='0001-01-01T00:00:00.000Z',
  55. endDate='9999-12-31T23:59:59.999Z'
  56. }
  57. },
  58. headers={},
  59. descriptions={},
  60. urls={},
  61. cause=0,
  62. effect=0
  63. }
  64. for lang, text in pairs(alertContent.text[1]) do
  65. alerts[i].headers[lang] = text
  66. alerts[i].descriptions[lang] = text
  67. end
  68. i = i+1
  69. end
  70. end
  71. ---@diagnostic disable-next-line: redefined-local
  72. local result, error_message = json.encode(alerts)
  73. if result == nil then
  74. error_struct.message = "while encoding result: " .. error_message
  75. local error_json, _ = json.encode(error_struct)
  76. return "", error_json
  77. end
  78. return result, ""
  79. end