transmission-clean.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/bash
  2. #
  3. # Script to clean completed torrent after seed ratio limit
  4. #
  5. # Copyright (C) 2018 Distopico <distopico@riseup.net>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. # Configuration
  21. HOST=localhost
  22. POST=9091
  23. USER=test
  24. PASS=test
  25. REMOVE_PATH=/Torrents/movies
  26. SERVER="$HOST:$9091 -n $USER:$PASS"
  27. # use transmission-remote to get done torrent list
  28. TORRENTLIST=`transmission-remote $SERVER --list | sed -e '1d;s/^ *//' | grep 'Done' | cut --only-delimited --delimiter=' ' --fields=1`
  29. # for each torrent in the list
  30. for TORRENTID in $TORRENTLIST
  31. do
  32. echo Processing: $TORRENTID
  33. # check if torrent download is completed
  34. DONE=`transmission-remote $SERVER --torrent $TORRENTID --info | grep 'Percent Done: 100%'`
  35. # check torrents ratio
  36. RATIO_LIMIT=`transmission-remote $SERVER --session-info | grep 'Default seed ratio limit:' | sed -e 's/[A-Z|a-z]\:*//g' | bc -l`
  37. RATIO=`transmission-remote $SERVER --torrent $TORRENTID --info | grep 'Ratio:' | sed -e 's/[A-Z|a-z]\:*//g' | bc -l`
  38. LOCATION=`transmission-remote $SERVER --torrent $TORRENTID --info | grep 'Location:' | sed -e 's/Location\: //g'`
  39. NAME=`transmission-remote $SERVER --torrent $TORRENTID --info | grep 'Name:' | sed -e 's/ Name\: //g'`
  40. echo $DONE
  41. echo Ratio: "${RATIO%.*}"
  42. # if the torrent is complete and has ratio limit
  43. if [ "$DONE" ] && [ "${RATIO%.*}" -ge "${RATIO_LIMIT%.*}" ]; then
  44. # remove the torrent from Transmission
  45. if [[ "$LOCATION" =~ "$REMOVE_PATH" ]]; then
  46. echo "Torrent #$TORRENTID ($NAME) is completed, Removing torrent and data from list"
  47. transmission-remote $SERVER --torrent $TORRENTID --remove-and-delete
  48. else
  49. echo "Torrent #$TORRENTID ($NAME) is completed, Removing torrent from list"
  50. transmission-remote $SERVER --torrent $TORRENTID --remove
  51. fi
  52. else
  53. echo "Torrent #$TORRENTID is not completed. Ignoring."
  54. fi
  55. printf "\n"
  56. done