Last active
January 8, 2019 13:13
-
-
Save bagage/c1b6ecec0640f58ff374 to your computer and use it in GitHub Desktop.
Torrent and magnet manager for torrentcloud.eu (V2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# modification of an initial version from uwe-schwarz: https://github.com/uwe-schwarz/dot/blob/master/bin/torrentcloud.sh | |
# license: WTFPL | |
host="www.torrentcloud.eu" | |
uri="/cpv2/" | |
u="$(grep ^username= ~/.tcrc | cut -d= -f 2)" | |
p="$(grep ^password= ~/.tcrc | cut -d= -f 2)" | |
target="$(grep ^target= ~/.tcrc | cut -d= -f 2)" | |
targettmp="$(grep ^targettmp= ~/.tcrc | cut -d= -f 2)" | |
url="https://$u:$p@$host$uri" | |
dl="axel -a -n 8" | |
mp4=~/bin/2mp4 | |
on_exit_content="" | |
on_exit() { | |
# if stdout is NOT a terminal, do a notification | |
if [ ! -t 1 ] && [ ! -z "$on_exit_content" ]; then | |
notify-send -t 500 -h int:transient:1 \ | |
-i /usr/share/rhythmbox/icons/hicolor/16x16/status/rhythmbox-set-star.png \ | |
"torrent cloud upload" "$on_exit_content" | |
fi | |
} | |
trap on_exit EXIT | |
notify() { | |
output=$(printf "$@") | |
echo "$output" | |
on_exit_content="$on_exit_content\n$output" | |
} | |
die() { | |
notify "$@" | |
exit 1 | |
} | |
if [ -z "$u" ] || [ -z "$p" ]; then | |
die "please create ~/.tcrc with: | |
username=XX | |
password=YY" | |
elif [ ! -d "$target" ] || [ ! -d "$targettmp" ]; then | |
die "please set target and tempoary directories in ~/.tcrc with: | |
target=<one_dir> | |
targettmp=<one_or_another_dir>" | |
fi | |
# get files | |
_t_status() { | |
tmp="$(mktemp)" | |
curl -s "$url" > "$tmp" | |
slots_avail="$(egrep -o '<b>[0-9]+</b> available' "$tmp" | egrep -o '[0-9]+')" | |
} | |
t_add() { | |
_t_status | |
m="$1" | |
if [ -z "$m" ]; then | |
m="$(xclip -o)" | |
fi | |
if [ $slots_avail -gt 0 ]; then | |
slots=1 | |
else | |
slots=0 | |
fi | |
# we are trying a magnet | |
if grep -q '^magnet:' <<< "$m"; then | |
n="$(echo "$m" | egrep -o "dn=[^&]*" | cut -d= -f2- | tr "+" " ")" | |
if [ -z "$n" ]; then | |
read -p "title? " n | |
fi | |
n="${n/ \[PublicHD\]}" | |
notify "adding %s …\n" "$n" | |
options=(--form "torrentmagnet=$m" --form "name=$n") | |
# we are trying to add a torrent | |
else | |
tmpdir=$(mktemp -d) | |
if [ ! -f "$m" ]; then | |
notify "uploading torrent from url $m.. but downloading it first...\n" | |
wget --directory-prefix $tmpdir/ $m | |
else | |
cp $m $tmpdir | |
fi | |
# rename file to have a proper name so that its correctly displayed on webpage | |
for torrent in $tmpdir/*; do | |
which transmission-show || die "please install transmission-cli" | |
file=$(transmission-show "$torrent" | head -n1 | cut -d: -f2- | tr -c -d 'A-Za-z0-9\.').torrent | |
mv "$torrent" "$file" | |
notify "uploading torrent from file $file\n" | |
options=(--form "file=@$file") | |
done | |
fi | |
errfile=$(mktemp) | |
curl -o $errfile ${options[@]} --form "cmdSend=Run" --form "slots=$slots" "$url" || die "oops, curl failed" | |
error=$(grep error $errfile | cut -d '>' -f 2- | cut -d '<' -f1) | |
[ -z "$error" ] && notify "added, check with $0 list if it's running.\n" || die "failed to upload: $error" | |
} | |
# list_torrent_numbers / names | |
t_list() { | |
_t_status | |
notify "id\t%%\tslot\ttitle\n" | |
grep -o "TORRENT=[0-9]*" "$tmp" | cut -d= -f2 | sort -u | while read t; do | |
title="$(grep "titl.*$t" "$tmp" | sed 's/^.*<b [^>]*>//;s/<.*$//;s/.magnet//')" | |
left="$(egrep -o "PROC_D$t.*prg_done.*width:\s*[0-9]*" $tmp | awk -F':' '{print $NF}')" | |
progress=$((100 - $left)) | |
slot="$(egrep -o "TORRENT=$t&SLOT.*>\s*[0-9]+\s*<" "$tmp" | head -n 1 | awk '{print $(NF-1)}')" | |
notify "%d\t%d\t%d\t%s\n" "$t" "$progress" "$slot" "$title" | |
done | |
} | |
t_slot() { | |
_t_status | |
up="${1:-1}" | |
t="$2" | |
curl -so /dev/null "$url?TORRENT=$t&SLOTS=$up" | |
slot=$(t_list | grep "$1" | awk '{ print $3 }') | |
if [ "$slot" != "$up" ]; then | |
die "Could not allocate %d slot to %s (none available?)" "$up" "$t" | |
else | |
notify "%s got %d slot.\n" "$t" "$up" | |
fi | |
} | |
t_delete() { | |
_t_status | |
t="$1" | |
curl -so /dev/null "$url?TORRENT=$t&DELETE=yes" | |
notify "%s deleted.\n" "$t" | |
} | |
# download torrent $1 | |
_t_down() { | |
_t_status | |
conv="${1:-n}" | |
t="$2" | |
title="$(grep "titl.*$t" "$tmp" | sed 's/^.*<b [^>]*>//;s/<.*$//;s/.magnet//')" | |
down="$(curl -s "$url?_LIST_FILES&TORRENT=$t" | egrep '(HTTPS)' | grep -o "https[^\"]*" | head -n 1)" | |
mkdir /tmp/$$ | |
cd /tmp/$$ | |
if [ -n "$down" ]; then | |
notify "downloading from %s to %s\n" "$down" "/tmp/$$" | |
n=($(curl -Lks "$down" | grep -o "href=\"[^\"]*\"" | cut -d\" -f2 | grep -v "\.\./")) | |
while [ ${#n[@]} -gt 0 ]; do | |
echo $n | |
last=$[${#n[@]}-1] | |
if [ ${n[$last]:$[${#n[$last]}-1]:1} == "/" ]; then | |
notify "traversing dir: %s\n" "${n[$last]}" | |
# directory, add them to the list | |
n_tmp="${n[$last]}" | |
n_tmp2=($(curl -Lks "$down/$n_tmp" | grep -o "href=\"[^\"]*\"" | cut -d\" -f2 | grep -v "\.\./")) | |
for ((i=0;i<${#n_tmp2[@]};i++)); do | |
n[$last]="${n_tmp}${n_tmp2[$i]}" | |
(( last++ )) | |
done | |
else | |
notify "downloading file: %s\n" "${n[$last]}" | |
$dl "$down/${n[$last]}" | |
if [ "$conv" == "y" ]; then | |
fn="$(echo -e "$(echo "${n[$last]/*\/}" | sed 's/%\([0-9][0-9]*\)/\\x\1/g')")" | |
ext="${n[$last]:$[${#n[$last]}-3]:3}" | |
if [ "$ext" == "mkv" -o "$ext" == "avi" -o "$ext" == "mpg" ]; then | |
notify "converting to mp4: %s\n" "/tmp/$$/$fn" | |
$mp4 "/tmp/$$/$fn" | |
fi | |
fi | |
fi | |
unset n[$last] | |
done | |
notify "moving %s to %s\n" "/tmp/$$" "$target/$title" | |
mv "/tmp/$$" "$targettmp/" | |
mv "$targettmp/$$" "$target/$title" | |
else | |
printf "Could not find URL for downloading, torrent may not exist or not ready yet?\n" | |
return 1 | |
fi | |
[ -d /tmp/$$ ] && rm -r /tmp/$$ | |
} | |
t_download() { | |
while ! _t_down "$1" "$2"; do | |
notify "$2 looks not ready yet! Retrying in 60sec...\n" | |
$0 list | |
read -p "stop with any key" -t 60 -n 1 | |
if [ $? -eq 0 ]; then break; fi | |
echo | |
done | |
} | |
t_target() { | |
tree -L 2 $target | |
} | |
t_whole() { | |
slot=$(t_list | grep "$1" | awk '{ print $3 }') | |
if [ "$slot" = 0 ]; then | |
notify "$1 has no slot yet.. trying to assign one" | |
t_slot 1 "$1" | |
fi | |
t_download n "$1" | |
if [ -e "$target/$1" ]; then | |
t_delete "$1" | |
fi | |
} | |
t_help() { | |
printf "$0 must be used with one of the following actions:\n" | |
printf "\tl/list display list of all torrents\n" | |
printf "\ta/add <magnets/torrents> add magnet(s) or/and torrent(s) and start it/them if a slot is available\n" | |
printf "\tr/remove <ids> remove torrent(s)\n" | |
printf "\td/download <conv> <ids> download torrent(s) to $target when ready and optionally convert it/them to mp4\n" | |
printf "\ts/slot <slots> <ids> assign a free slot to torrent(s) if any\n" | |
printf "\tt/target show content of $target\n" | |
printf "\tw/whole <ids> start, download and delete torrent(s)\n" | |
} | |
# main | |
method= | |
case "${1:-help}" in | |
"a"|"add") method="t_add" ;; | |
"l"|"list") t_list ;; | |
"d"|"download") method="t_download $2"; shift ;; | |
"rm"|"remove") method="t_delete" ;; | |
"s"|"slot") method="t_slot $2"; shift ;; | |
"t"|"target") t_target ;; | |
"w"|"whole") method="t_whole";; | |
*) t_help ;; | |
esac | |
if [ ! -z "$method" ]; then | |
shift | |
for id in $@; do | |
$method $id | |
done | |
fi | |
rm -f "$tmp" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment