db-delay-notify.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/bash
  2. set -euo pipefail
  3. # Klimbim Software collection, A bag full of things
  4. # Copyright (C) 2011-2023 Johannes 'Banana' Keßler
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. # 2023 http://www.bananas-playground.net
  19. # More information and resources can be found here
  20. # https://www.bananas-playground.net/2023/05/db-delay-notify/
  21. # station eva#
  22. STATIONID=""
  23. # destination eva# Show only for destination
  24. # train changes are not accounted for. It only shows train which
  25. # start at STATIONID and end at DESTINATIONID
  26. DESITNATIONID=""
  27. # can be dep (departure) or arr (arrival)
  28. BOARDTYPE="dep"
  29. # show only delays
  30. # the delay limit is 5min.
  31. DELAY="false"
  32. # which type of transport will be shown
  33. FILTER="11111"
  34. # print some debug info
  35. DEBUG="false"
  36. USAGE="Usage: db-delay.sh STATIONID BOARDTYPE[dep|arr] DELAY[true|false] [DESITNATIONID]"
  37. if [[ $# -ge 1 ]] && [[ -n "$1" ]]; then
  38. STATIONID=$1;
  39. else
  40. echo "Missing argument STATIONID"
  41. echo "${USAGE}"
  42. exit 1
  43. fi
  44. if [[ $# -ge 2 ]] && [[ -n "$2" ]]; then
  45. BOARDTYPE=$2;
  46. else
  47. echo "Missing argument BOARDTYPE"
  48. echo "${USAGE}"
  49. exit 1
  50. fi
  51. if [[ $# -ge 3 ]] && [[ -n "$3" ]]; then
  52. DELAY=$3;
  53. else
  54. echo "Missing argument DELAY"
  55. echo "${USAGE}"
  56. exit 1
  57. fi
  58. if [[ $# -ge 4 ]] && [[ -n "$4" ]]; then
  59. DESITNATIONID=$4;
  60. fi
  61. # the endpoint
  62. DBURL="https://reiseauskunft.bahn.de/bin/bhftafel.exe/dn?L=vs_java&start=yes&boardType=${BOARDTYPE}&input=${STATIONID}&productsFilter=${FILTER}&time=actual"
  63. if [[ "$DELAY" == "true" ]] ; then
  64. DBURL+="&delayedJourney=on";
  65. fi
  66. if [[ ! -z "$DESITNATIONID" ]] ; then
  67. DBURL+="&dirInput=${DESITNATIONID}";
  68. fi
  69. # Return value not really clear yet.
  70. # If nothing is found, just the station text is returned.
  71. # If there is no delay (if delay=true) an error is returned. Why?
  72. # A succesful minimal result is 4 lines
  73. # Nothing is one line
  74. # No delays two lines with error in the second
  75. CALLRESULT=`curl -s "${DBURL}"`
  76. LINES=`wc -l <<< "${CALLRESULT}"`
  77. NOTIFYTEXT="Something wrong"
  78. if [[ "$DELAY" = true ]] && [[ $LINES > 3 ]]; then
  79. NOTIFYTEXT="Delays\n${CALLRESULT}"
  80. elif [[ "$DELAY" = true ]] && [[ $CALLRESULT == *"error"* ]]; then
  81. NOTIFYTEXT=`echo "${CALLRESULT}" | head -1`
  82. NOTIFYTEXT+="\nNo delays yet"
  83. else
  84. NOTIFYTEXT=${CALLRESULT}
  85. fi
  86. if [[ "${DEBUG}" == "true" ]] ; then
  87. echo "${DBURL}"
  88. echo "${CALLRESULT}"
  89. echo "${LINES}"
  90. echo -e "${NOTIFYTEXT}"
  91. fi
  92. BOTTOKEN="xxxxxxxxxxxxxxxx"
  93. CHAT_ID="xxxxxxxxxxx"
  94. TELURL="https://api.telegram.org/bot$BOTTOKEN/sendMessage"
  95. curl -s -X POST -H 'Content-Type: application/json' -d '{"chat_id": "'${CHAT_ID}'", "text": "'"${NOTIFYTEXT}"'", "disable_notification": false}' ${TELURL} > /dev/null 2>&1