db-delay-notify.sh 2.7 KB

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