其他 / 群晖 · 2023年6月29日 0

群晖 Download Station BT 种子下载 自动添加trackerlist

群晖的DownloadStation(简称DS)不支持订阅trackerlist的功能,trackerlist可以让ds去连接更多的种子服务器,让下载速度加快很多;

虽然ds本身不支持订阅trackerlist,但是可以手动向每个下载的任务添加tracker列表,如下图

trackerlist可以从github上公开的仓库获取,也可以找其他的tracker列表,比较有名的trackerlist是这个仓库 https://ngosang.github.io/trackerslist/, 该仓库会定时更新最新的trackerlist。

如果之前看过我的另一篇文章的话,就知道我找到了个万能api可以操作群晖上的任意功能

所以基于这个万能api,我写了个脚本来用于自动向ds的下载任务添加trackerlist,

脚本的配置方法和上面的文章一样,配置一个1分钟的定时任务自动执行,

该脚本就会自动检测ds的下载任务,并且添加trackerlist,这样去下载东西就不用手动添加了

#!/bin/bash

DISABLE_COLOR=false

BLACK="\e[30m"
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
MAGENTA="\e[35m"
CYAN="\e[36m"
LIGHT_GRAY="\e[37m"
DARK_GRAY="\e[90m"
LIGHT_RED="\e[91m"
LIGHT_GREEN="\e[92m"
LIGHT_YELLOW="\e[93m"
LIGHT_BLUE="\e[94m"
LIGHT_MAGENTA="\e[95m"
LIGHT_CYAN="\e[96m"
WHITE="\e[97m"
END="\e[0m"

if [ $DISABLE_COLOR == true ]; then
  BLACK=""
  RED=""
  GREEN=""
  YELLOW=""
  BLUE=""
  MAGENTA=""
  CYAN=""
  LIGHT_GRAY=""
  DARK_GRAY=""
  LIGHT_RED=""
  LIGHT_GREEN=""
  LIGHT_YELLOW=""
  LIGHT_BLUE=""
  LIGHT_MAGENTA=""
  LIGHT_CYAN=""
  WHITE=""
  END=""
fi


# 群晖DS 任务状态定义
TASK_STATUS_WAITING=1
TASK_STATUS_DOWNLOADING=2
TASK_STATUS_PAUSED=3
TASK_STATUS_FINISHING=4
TASK_STATUS_FINISHED=5
TASK_STATUS_HASH_CHECKING=6
TASK_STATUS_SEEDING=7
TASK_STATUS_FILEHOSTING_WAITING=8
TASK_STATUS_EXTRACTING=9
TASK_STATUS_ERROR=10
TASK_STATUS_BROKEN_LINK=11
TASK_STATUS_DESTINATION_NOT_EXIST=12
TASK_STATUS_DESTINATION_DENIED=13
TASK_STATUS_DISK_FULL=14
TASK_STATUS_QUOTA_REACHED=15
TASK_STATUS_TIMEOUT=16
TASK_STATUS_EXCEED_MAX_FILE_SYSTEM_SIZE=17
TASK_STATUS_EXCEED_MAX_DESTINATION_SIZE=18
TASK_STATUS_EXCEED_MAX_TEMP_SIZE=19
TASK_STATUS_ENCRYPTED_NAME_TOO_LONG=20
TASK_STATUS_NAME_TOO_LONG=21
TASK_STATUS_TORRENT_DUPLICATE=22
TASK_STATUS_FILE_NOT_EXIST=23
TASK_STATUS_REQUIRED_PREMIUM_ACCOUNT=24
TASK_STATUS_NOT_SUPPORTED_TYPE=25
TASK_STATUS_TRY_IT_LATER=26
TASK_STATUS_TASK_ENCRYPTION=27
TASK_STATUS_MISSING_PYTHON=28
TASK_STATUS_PRIVATE_VIDEO=29
TASK_STATUS_FTP_ENCRYPTION_NOT_SUPPORTED_TYPE=30
TASK_STATUS_EXTRACT_FAILED=31
TASK_STATUS_EXTRACT_FAILED_WRONG_PASSWORD=32
TASK_STATUS_EXTRACT_FAILED_INVALID_ARCHIVE=33
TASK_STATUS_EXTRACT_FAILED_QUOTA_REACHED=34
TASK_STATUS_EXTRACT_FAILED_DISK_FULL=35
TASK_STATUS_UNKNOWN=36

trackerlistUrl="https://cdn.jsdelivr.net/gh/ngosang/trackerslist@master/trackers_all.txt"

trackerList=$(curl -f $trackerlistUrl 2>/dev/null)
if [[ $? != 0 ]]; then
  echo "Tracker list url unavailable please check url: $trackerlistUrl"
  exit 1
fi
trackerList=$(echo '"'$trackerList'"' | jq -c 'split(" ")')
trackerListLength=$(jq 'length' <<<$trackerList)

if [[ $trackerListLength == 0 ]]; then
  echo "No found tracker please check tracker list: $trackerlistUrl"
  exit 1
fi

pakcageList=$(synowebapi --exec api=SYNO.Core.Package method=list version=2 additional='["status"]' 2>/dev/null | jq -c '.data.packages | map(select(.id == "DownloadStation"))')

isRunning=$(jq -r 'map(select(.id == "DownloadStation") | .additional.status) | .[]' <<<$pakcageList)

if [ $isRunning != "running" ]; then
  echo -e "Download Station$RED not running$END exit"
  exit
fi

echo "Download Station is running go next step"

downloadList=$(synowebapi --exec api=SYNO.DownloadStation2.Task method=list version=2 limit=-1 "status=[$TASK_STATUS_DOWNLOADING]" 2>/dev/null | jq -c '.data.task | map(select(.type == "bt"))')
downloadListCount=$(jq 'length' <<<$downloadList)

if [[ $downloadListCount -eq 0 ]]; then
  echo "Not found bt download task"
  exit
fi

echo "Download tasks count: $downloadListCount"

for idx in $(seq 0 $((downloadListCount - 1))); do
  echo "----------------Task [$idx]------------------"
  task=$(jq -c ".[$idx]" <<<$downloadList)
  taskId=$(jq -r .id <<<$task)
  taskName=$(jq -r .title <<<$task)
  taskTrackerList=$(synowebapi --exec api=SYNO.DownloadStation2.Task.BT.Tracker method=list version=2 task_id="$taskId" limit=-1 2>/dev/null | jq -c '.')
  taskTrackerListMap=$(jq -c '.data.items | reduce .[] as $i ({}; .[$i.url] = 1)' 2>/dev/null <<<$taskTrackerList)
  if [[ $? != 0 ]]; then
    echo "Task: 【$taskName】 is busy skip task; $taskTrackerList"
    continue
  fi
  taskTrackerListCount=$(jq '.data.total' <<<$taskTrackerList)
  echo "Task:【$taskName】 has tracker count: $taskTrackerListCount"
  needAddedTrackerList=$(jq -c --argjson hashMap $taskTrackerListMap 'map(select(. | in($hashMap) == false))' <<<$trackerList)
  needAddedTrackerListLength=$(jq 'length' <<<$needAddedTrackerList)

  if [[ $needAddedTrackerListLength == 0 ]]; then
    echo "The task does not need to add a new tracker"
    continue
  fi

  echo "A list of trackers that need to be added to the task: $needAddedTrackerList , count: $needAddedTrackerListLength"

  result=$(synowebapi --exec api=SYNO.DownloadStation2.Task.BT.Tracker method=add version=2 task_id=$taskId tracker=$needAddedTrackerList 2>/dev/null)

  if [[ $(jq '.success' <<<$result) == true ]]; then
    echo "Task: [$taskName] tracker list add success"
  else
    echo "Task: [$taskName] tracker list add fail"
  fi
done

echo "Automatically add tracker script to complete"