smirnov

tg-notify.bash

Jan 13th, 2021
689
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env -S bash
  2.  
  3. TOKEN=""
  4. CHAT_ID=""
  5.  
  6. declare -A PIDS
  7.  
  8. function notify() {
  9.     local URL="$1"
  10.     local PID="$2"
  11.     local MESSAGE="$3"
  12.     wget -qO->>/dev/null -U "GayRobot (like TelegramBot)" \
  13.         --post-data "chat_id=${CHAT_ID}&text=<b><a href=\"${URL}\">${PID}</a> ${MESSAGE}</b>&parse_mode=HTML&disable_web_page_preview=True" \
  14.         "https://api.telegram.org/bot${TOKEN}/sendMessage"
  15. }
  16.  
  17. function check() {
  18.     local PID=$1
  19.     [[ -d "/proc/${PID}" ]] && return 0 || return 1
  20. }
  21.  
  22. function download() {
  23.     local URL="$1"
  24.     local NAME="$2"
  25.     PIDS["$URL"]+=$(wget --no-check-certificate -U "Mozilla/5.0" -qO "$NAME" -bc "$URL" | grep -Ewo "[0-9]+")
  26. }
  27.  
  28. function main() {
  29.     while IFS=' ' read -r link name; do download "$link" "$name"; done < links.txt # format: https://example.com/deb.iso deb.iso
  30.  
  31.     while [[ "${#PIDS[@]}" != 0 ]]; do
  32.         for k in "${!PIDS[@]}"; do
  33.             if [[ $(check ${PIDS["$k"]}; echo $?) != 0 ]]; then
  34.                 notify "$k" "${PIDS["$k"]}" "download is complete."
  35.                 unset "PIDS["$k"]"
  36.                 sleep 3s
  37.             fi
  38.         done
  39.         sleep 60s
  40.     done
  41.  
  42.     exit 0
  43. }
  44.  
  45. main & disown
  46. echo "main PID: $!"
  47. exit 0
RAW Paste Data