3

Is there a way to retrieve the ID generated by transmission on a newly added torrent when invoking the command:

$> transmission-remote -a file.torrent

The return value of this command $? returns 0 or 1 depending on whether the torrent was a valid file but I would like to find a way to get a unique identifier to later perform operations on the torrent (stop, start, remove, etc).

2
  • By ID, you do you mean the torrent number (like you'd pass to -t)?
    – derobert
    May 5, 2017 at 16:08
  • @derobert , Yes I do ! May 5, 2017 at 16:10

1 Answer 1

3

I think there are only two ways to do it:

  1. Use --list before and after, and see what's new. Doable in a script, but sounds painful. And of course prone to races.

  2. Use -t TORRENT -i and look at the Id: field. At first glance this looks circular, but it turns out TORRENT doesn't have to be an Id. It can be a hash.

So, with method 2:

hash="$( transmission-show FILE.TORRENT  | perl -n -E 'say $1 if /^\s*Hash: (.+)$/' )"
id="$(transmission-remote -t "$hash" -i | perl -n -E 'say $1 if /^\s*Id: ([0-9]+)$/' )"

You can combine those all in one line, of course. And you'll have to add your server/authentication options to that tramission-remote line. (Personally, I have a t-r script which does that and also adds even-odd row highlighting to -l output). It looks like this:

#!/bin/bash

if ! [ -r ~/.transmission-netrc ]; then
    echo Expected to find a ~/.transmission-netrc file with the username
    echo and password.
    exit 1
fi

if [ "xterm" == "$TERM" ]; then
    export TERM=xterm-256color
fi

transmission-remote «HOSTNAME» -N ~/.transmission-netrc "$@" | (
    if [ "-l" == "$1" ]; then
        sed -e "1~2 s|^|`tput setab 149``tput el`|"  -e "2~2 s|^|`tput setab 221``tput el`|"
        tput setab 7
        tput el
    else
        cat
    fi
)

Note the «HOSTNAME» you'd need to fill in.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .