aria2搭配rclone实现下载完成后,自动归档并发送telegram消息提醒。需要安装rclone, curl和aria2软件。

#!/usr/bin/env bash

rcloneDrive='/root/Download/' # 目标文件夹路径
downloadPath='/mnt/data/ariadownload' # aria2下载储存的文件夹

TOKEN=1234567890:ASDFGHJKL-QWERTYUIOP # telegram机器人的token
CHATID=9876543210 # telegram机器人的chat id

urlencode() {
  local length="${#1}"
  for (( i = 0; i < length; i++ )); do
    local c="${1:i:1}"
    case $c in
      [a-zA-Z0-9.~_-]) printf "$c" ;;
      *) printf '%%%02X' "'$c" ;;
    esac
  done
}

if [ $2 -eq 0 ]; then
  exit 0
elif [ $2 -eq 1 ]; then
  basenameStr=`basename "$3"`
  su - -c "rclone move \"$3\" $rcloneDrive"
  TEXT=$(echo $3 | sed 's|.*/||')
  ENCODED_TEXT=$(urlencode "$TEXT Uploaded")
  curl -s "https://api.telegram.org/bot$TOKEN/sendMessage?chat_id=$CHATID&text=$ENCODED_TEXT"
  exit 0
else
  filePath=$3
  while true; do
    dirnameStr=`dirname "$filePath"`
    if [ "$dirnameStr" = "$downloadPath" ]; then
      basenameStr=`basename "$filePath"`
      su - -c "rclone move \"$filePath\" $rcloneDrive\"$basenameStr\""
      TEXT2=$basenameStr
      ENCODED_TEXT2=$(urlencode "$TEXT2 Uploaded")
      curl -s "https://api.telegram.org/bot$TOKEN/sendMessage?chat_id=$CHATID&text=$ENCODED_TEXT2"
      rm -r -f "$filePath"
      exit 0
    elif [ "$dirnameStr" = "/" ]; then
      exit 0
    else
      filePath=$dirnameStr
    fi
  done
fi

标签: linux, shell

添加新评论