From 21b54b708de8639acfdaa6134b6b513b5f40abb7 Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Wed, 17 Feb 2021 13:02:16 -0800 Subject: [PATCH 1/4] make it work with multiple URLs in ${list_url} That was presumably the intention of naming the variable `list_url`, but surrounding it with `""` prevents this from working properly. --- manual-tracker-add.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manual-tracker-add.sh b/manual-tracker-add.sh index 922a663..9baa153 100755 --- a/manual-tracker-add.sh +++ b/manual-tracker-add.sh @@ -10,7 +10,7 @@ list_url=${TRACKER_URL:-https://raw.githubusercontent.com/ngosang/trackerslist/m add_trackers () { torrent_hash=$1 - for base_url in "${list_url}" ; do + for base_url in ${list_url}; do echo -e "\e[1m\e[5m" echo "URL for ${base_url}" echo -e "Adding trackers for \e[91m$torrent_name..." From 06d5cc4724fda789d1400aba260dee7d00014f6b Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Wed, 17 Feb 2021 13:05:00 -0800 Subject: [PATCH 2/4] Only download list(s) of trackers ONCE per run Uses a temporary file to store the list of trackers. --- manual-tracker-add.sh | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/manual-tracker-add.sh b/manual-tracker-add.sh index 9baa153..0cac933 100755 --- a/manual-tracker-add.sh +++ b/manual-tracker-add.sh @@ -8,26 +8,40 @@ fi host=${TRANSMISSION_HOST:-localhost} list_url=${TRACKER_URL:-https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt} +# Download list(s) of trackers to temporary file +list_file=$(mktemp --tmpdir trackers.XXXXX.txt) +echo -e "\e[1m\e[5m" +echo "Downloading trackers into ${list_file}" +for base_url in ${list_url}; do + echo -e "\e[1m\e[5m" + echo "URL for ${base_url}" + echo -en "\e[0m" + if curl --location -# "${base_url}" >> "${list_file}"; then + echo -e '\e[92m done.' + echo -en "\e[0m" + else + echo -e '\e[91m failed.' + echo -en "\e[0m" + fi +done + add_trackers () { torrent_hash=$1 - for base_url in ${list_url}; do echo -e "\e[1m\e[5m" - echo "URL for ${base_url}" echo -e "Adding trackers for \e[91m$torrent_name..." echo -en "\e[0m" echo -e "\e[2m\e[92m" -for tracker in $(curl --location -# "${base_url}") ; do - echo -en "\e[0m" - echo -ne "\e[93m*\e[0m ${tracker}..." -if transmission-remote "$host" ${auth:+--auth="$auth"} --torrent "${torrent_hash}" -td "${tracker}" | grep -q 'success'; then - echo -e '\e[92m done.' - echo -en "\e[0m" -else - echo -e '\e[93m already added.' - echo -en "\e[0m" -fi - done -done + for tracker in $(cat "${list_file}"); do + echo -en "\e[0m" + echo -ne "\e[93m*\e[0m ${tracker}..." + if transmission-remote "$host" ${auth:+--auth="$auth"} --torrent "${torrent_hash}" -td "${tracker}" | grep -q 'success'; then + echo -e '\e[91m failed.' + echo -en "\e[0m" + else + echo -e '\e[92m done.' + echo -en "\e[0m" + fi + done } # Get list of active torrents From ad4aedc91b80566d48d51dd18f4d7119e6f04272 Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Wed, 17 Feb 2021 13:12:13 -0800 Subject: [PATCH 3/4] fix indentation, and move subroutine out of while loop, in tracker-add-auto.sh and tracker-add-auto-router.sh --- tracker-add-auto-router.sh | 75 ++++++++++++++++++------------------- tracker-add-auto.sh | 76 +++++++++++++++++++------------------- 2 files changed, 77 insertions(+), 74 deletions(-) diff --git a/tracker-add-auto-router.sh b/tracker-add-auto-router.sh index 2c1875d..0f9bf25 100644 --- a/tracker-add-auto-router.sh +++ b/tracker-add-auto-router.sh @@ -3,50 +3,51 @@ auth=user:password host=localhost -while true ; do -sleep 25 add_trackers () { torrent_hash=$1 id=$2 trackerslist=/tmp/trackers.txt -for base_url in https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt ; do -if [ ! -f $trackerslist ]; then -curl -o "$trackerslist" "${base_url}" -fi -Local=$(wc -c < $trackerslist) -Remote=$(curl -sI "${base_url}" | awk '/Content-Length/ {sub("\r",""); print $2}') -if [ "$Local" != "$Remote" ]; then -curl -o "$trackerslist" "${base_url}" -fi - echo "URL for ${base_url}" - echo "Adding trackers for $torrent_name..." -for tracker in $(cat $trackerslist) ; do - echo "${tracker}..." -if transmission-remote "$host" --auth="$auth" --torrent "${torrent_hash}" -td "${tracker}" | grep -q 'success'; then - echo ' failed.' -else - echo ' done.' -fi -done -done + for base_url in https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt ; do + if [ ! -f $trackerslist ]; then + curl -o "$trackerslist" "${base_url}" + fi + Local=$(wc -c < $trackerslist) + Remote=$(curl -sI "${base_url}" | awk '/Content-Length/ {sub("\r",""); print $2}') + if [ "$Local" != "$Remote" ]; then + curl -o "$trackerslist" "${base_url}" + fi + echo "URL for ${base_url}" + echo "Adding trackers for $torrent_name..." + for tracker in $(cat $trackerslist) ; do + echo "${tracker}..." + if transmission-remote "$host" --auth="$auth" --torrent "${torrent_hash}" -td "${tracker}" | grep -q 'success'; then + echo ' failed.' + else + echo ' done.' + fi + done + done sleep 3m rm -f "/tmp/TTAA.$id.lock" } -# Get list of active torrents + +while true ; do + sleep 25 + # Get list of active torrents ids="$(transmission-remote "$host" --auth="$auth" --list | grep -vE 'Seeding|Stopped|Finished|[[:space:]]100%[[:space:]]' | grep '^ ' | awk '{ print $1 }')" -for id in $ids ; do - add_date="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info| grep '^ Date added: ' |cut -c 21-)" - add_date_t="$(date -d "$add_date" "+%Y-%m-%d %H:%M")" - dater="$(date "+%Y-%m-%d %H:%M")" - dateo="$(date -D '%s' -d "$(( `date +%s`+1*60 ))" "+%Y-%m-%d %H:%M")" + for id in $ids ; do + add_date="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info| grep '^ Date added: ' |cut -c 21-)" + add_date_t="$(date -d "$add_date" "+%Y-%m-%d %H:%M")" + dater="$(date "+%Y-%m-%d %H:%M")" + dateo="$(date -D '%s' -d "$(( `date +%s`+1*60 ))" "+%Y-%m-%d %H:%M")" -if [ ! -f "/tmp/TTAA.$id.lock" ]; then -if [[ "( "$(add_date_t)" == "$(dater)" || "$(add_date_t)" == "$(dateo)" )" ]]; then - hash="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info | grep '^ Hash: ' | awk '{ print $2 }')" - torrent_name="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info | grep '^ Name: ' |cut -c 9-)" - add_trackers "$hash" "$id" & - touch "/tmp/TTAA.$id.lock" -fi -fi -done + if [ ! -f "/tmp/TTAA.$id.lock" ]; then + if [[ "( "$(add_date_t)" == "$(dater)" || "$(add_date_t)" == "$(dateo)" )" ]]; then + hash="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info | grep '^ Hash: ' | awk '{ print $2 }')" + torrent_name="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info | grep '^ Name: ' |cut -c 9-)" + add_trackers "$hash" "$id" & + touch "/tmp/TTAA.$id.lock" + fi + fi + done done diff --git a/tracker-add-auto.sh b/tracker-add-auto.sh index 683a354..7318637 100644 --- a/tracker-add-auto.sh +++ b/tracker-add-auto.sh @@ -3,50 +3,52 @@ auth=user:password host=localhost -while true ; do -sleep 25 add_trackers () { torrent_hash=$1 id=$2 trackerslist=/tmp/trackers.txt -for base_url in https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt ; do -if [ ! -f $trackerslist ]; then -curl -o "$trackerslist" "${base_url}" -fi -Local=$(wc -c < $trackerslist) -Remote=$(curl -sI "${base_url}" | awk '/Content-Length/ {sub("\r",""); print $2}') -if [ "$Local" != "$Remote" ]; then -curl -o "$trackerslist" "${base_url}" -fi - echo "URL for ${base_url}" - echo "Adding trackers for $torrent_name..." -for tracker in $(cat $trackerslist) ; do - echo -n "${tracker}..." -if transmission-remote "$host" --auth="$auth" --torrent "${torrent_hash}" -td "${tracker}" | grep -q 'success'; then - echo ' done.' -else - echo ' already added.' -fi -done -done + for base_url in https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt ; do + if [ ! -f $trackerslist ]; then + curl -o "$trackerslist" "${base_url}" + fi + Local=$(wc -c < $trackerslist) + Remote=$(curl -sI "${base_url}" | awk '/Content-Length/ {sub("\r",""); print $2}') + if [ "$Local" != "$Remote" ]; then + curl -o "$trackerslist" "${base_url}" + fi + echo "URL for ${base_url}" + echo "Adding trackers for $torrent_name..." + for tracker in $(cat $trackerslist) ; do + echo -n "${tracker}..." + if transmission-remote "$host" --auth="$auth" --torrent "${torrent_hash}" -td "${tracker}" | grep -q 'success'; then + echo ' done.' + else + echo ' already added.' + fi + done + done sleep 3m rm -f "/tmp/TTAA.$id.lock" } -# Get list of active torrents + +while true ; do + sleep 25 + + # Get list of active torrents ids="$(transmission-remote "$host" --auth="$auth" --list | grep -vE 'Seeding|Stopped|Finished|[[:space:]]100%[[:space:]]' | grep '^ ' | awk '{ print $1 }')" -for id in $ids ; do - add_date="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info| grep '^ Date added: ' |cut -c 21-)" - add_date_t="$(date -d "$add_date" "+%Y-%m-%d %H:%M")" - dater="$(date "+%Y-%m-%d %H:%M")" - dateo="$(date -d "1 minutes ago" "+%Y-%m-%d %H:%M")" + for id in $ids ; do + add_date="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info| grep '^ Date added: ' |cut -c 21-)" + add_date_t="$(date -d "$add_date" "+%Y-%m-%d %H:%M")" + dater="$(date "+%Y-%m-%d %H:%M")" + dateo="$(date -d "1 minutes ago" "+%Y-%m-%d %H:%M")" -if [ ! -f "/tmp/TTAA.$id.lock" ]; then -if [[ "( "$add_date_t" == "$dater" || "$add_date_t" == "$dateo" )" ]]; then - hash="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info | grep '^ Hash: ' | awk '{ print $2 }')" - torrent_name="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info | grep '^ Name: ' |cut -c 9-)" - add_trackers "$hash" "$id" & - touch "/tmp/TTAA.$id.lock" -fi -fi -done + if [ ! -f "/tmp/TTAA.$id.lock" ]; then + if [[ "( "$add_date_t" == "$dater" || "$add_date_t" == "$dateo" )" ]]; then + hash="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info | grep '^ Hash: ' | awk '{ print $2 }')" + torrent_name="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info | grep '^ Name: ' |cut -c 9-)" + add_trackers "$hash" "$id" & + touch "/tmp/TTAA.$id.lock" + fi + fi + done done From 5562fab5fe948252be58d636f84eedbec57ac1a5 Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Wed, 17 Feb 2021 13:15:33 -0800 Subject: [PATCH 4/4] use `list_url` variable in all scripts --- tracker-add-auto-router.sh | 4 +++- tracker-add-auto.sh | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tracker-add-auto-router.sh b/tracker-add-auto-router.sh index 0f9bf25..f389671 100644 --- a/tracker-add-auto-router.sh +++ b/tracker-add-auto-router.sh @@ -3,11 +3,13 @@ auth=user:password host=localhost +list_url="https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt" + add_trackers () { torrent_hash=$1 id=$2 trackerslist=/tmp/trackers.txt - for base_url in https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt ; do + for base_url in ${list_url} ; do if [ ! -f $trackerslist ]; then curl -o "$trackerslist" "${base_url}" fi diff --git a/tracker-add-auto.sh b/tracker-add-auto.sh index 7318637..eb9dabb 100644 --- a/tracker-add-auto.sh +++ b/tracker-add-auto.sh @@ -3,11 +3,13 @@ auth=user:password host=localhost +list_url="https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt" + add_trackers () { torrent_hash=$1 id=$2 trackerslist=/tmp/trackers.txt - for base_url in https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt ; do + for base_url in ${list_url} ; do if [ ! -f $trackerslist ]; then curl -o "$trackerslist" "${base_url}" fi