dots

git clone git://mattcarlson.org/repos/dots.git
Log | Files | Refs

torr.sh (2334B)


      1 #!/bin/sh
      2 #
      3 # torr
      4 
      5 ICON=''
      6 TORRS="${XDG_CACHE_HOME:-${HOME}/.cache}/dots/bar/torrs"
      7 
      8 add() {
      9     err && return 1
     10 
     11     # add with transmission-remote, don't care about rpc output
     12     # actual magnet link or file is ${2}; ${1} is -a flag
     13     if transmission-remote -a "${2}" >/dev/null; then
     14         # get added torrent name in nice format
     15         # most recent torrent will be at bottom of list
     16         torr="$(transmission-remote -l 2>/dev/null | tail -2 | head -1 | tr -s ' ')"
     17         torr="${torr%% }"
     18         torr="${torr## }"
     19         torr="$(printf '%s\n' "${torr}" | cut -d ' ' -f9- | tr '+' ' ')"
     20 
     21         herbe "Added ${torr}" &
     22     else
     23         herbe 'Error adding torrent' &
     24         return 1
     25     fi
     26 }
     27 
     28 bar() {
     29     daemon_not_running && [ -f "${TORRS}" ] && printf '%s\n' "${ICON} $(cat "${TORRS}")" && return
     30 
     31     [ -n "${TR_TORRENT_NAME}" ] && herbe "${TR_TORRENT_NAME} downloaded"
     32 
     33     torrs="$(transmission-remote -l 2>/dev/null | head -n -1 | tail -n +2)"
     34 
     35     unfinished="$(printf '%s\n' "${torrs}" | awk '{print $2}' | grep -v '100%' | grep -c '.')"
     36 
     37     printf '%s\n' "${unfinished}" > "${TORRS}"
     38 
     39     printf '%s\n' "${ICON} ${unfinished}"
     40 }
     41 
     42 err() {
     43     if daemon_not_running; then
     44         herbe 'Torrent daemon is not active' &
     45         return 0
     46     else
     47         return 1
     48     fi
     49 }
     50 
     51 open() {
     52     err && return 1
     53     "${TERMINAL}" -c "${TORRENT_CLIENT}" -e "${TORRENT_CLIENT}"
     54 }
     55 
     56 rmdone() {
     57     daemon_not_running && printf '%s\n' 'Torrent daemon is not active' && return 1
     58     transmission-remote -l 2>/dev/null | awk '$2 == "100%" { system("transmission-remote -t "$1" -r >/dev/null") }'
     59 }
     60 
     61 # torrent daemon must be running for program to work
     62 daemon_not_running() {
     63     tid="$(ps -ef | grep -i '\<transmission-daemon\>' | grep -iv '\<grep\>' | awk '{ printf "%s", $2 }')"
     64 
     65     if [ -n "${tid}" ]; then
     66         return 1
     67     else
     68         return 0
     69     fi
     70 }
     71 
     72 main() {
     73     [ ${#} -eq 0 ] && bar
     74 
     75     case ${BLOCK_BUTTON} in 1) open ;; esac
     76 
     77     while getopts 'aor' opt; do
     78         case "${opt}" in
     79             # add torrent
     80             # argument should be torrent file or magnet file
     81             a) add "${@}" ;;
     82             # open torrent client
     83             o) open       ;;
     84             # remove all completed torrents
     85             r) rmdone     ;;
     86             *) return     ;;
     87         esac
     88     done
     89 }
     90 
     91 main "${@}"