Opened 4 years ago

Last modified 20 months ago

#5586 reopened Enhancement

Running a script when adding a torrent

Reported by: quinq Owned by: jordan
Priority: Normal Milestone: None Set
Component: libtransmission Version: 2.82
Severity: Normal Keywords: script torrent add
Cc:

Description

I'd like to have transmission run a script when adding a torrent, the same way one can be executed when a torrent is finished. Here is a quick patch (against version 2.82) that enables it.

Attachments (4)

libtransmission-scriptadded.patch (11.8 KB) - added by quinq 4 years ago.
patch for enabling executing a script when a torrent is added
cli-scriptadded.patch (1.2 KB) - added by quinq 4 years ago.
Patch enabling running a script when starting a torrent with cli
libtransmission-scriptadded-added-only.patch (11.8 KB) - added by scotia 4 years ago.
patch for enabling executing a script when a torrent is added (but not when the daemon is started)
libtransmission-scriptadded-torrent-and-magnet.patch (12.3 KB) - added by cfpp2p 3 years ago.

Download all attachments as: .zip

Change History (21)

Changed 4 years ago by quinq

patch for enabling executing a script when a torrent is added

Changed 4 years ago by quinq

Patch enabling running a script when starting a torrent with cli

comment:1 Changed 4 years ago by livings124

What would be the use-case for this? Seems more suited for the app downloading the torrent file.

comment:2 Changed 4 years ago by quinq

Maybe you're right about that. I use it to run a script which fetches a list of tracker for the added torrent on a metasearch website, and adds them to the torrent.

comment:3 Changed 4 years ago by livings124

  • Resolution set to wontfix
  • Status changed from new to closed

I appreciate the patch, but this isn't mainstream enough to belong in the client.

comment:4 Changed 4 years ago by clooney

I would find this extremely useful as this allows me to: 1) keep torrents stopped if they don't match a certain criteria 2) could detect and set final download location before a single byte is downloaded

Given it allows for more automation, I'd love to see this getting into transmission.

Changed 4 years ago by scotia

patch for enabling executing a script when a torrent is added (but not when the daemon is started)

comment:5 Changed 4 years ago by scotia

I've added a new patch set which runs a script only when a new torrent is added. The existing patch runs a script also when transmission is started (or restarted). The only change applies to libtransmission/torrent.c. You also need the cli script as added by quinq.

comment:6 Changed 4 years ago by scotia

Great patch, works well. Exactly what I needed.

Just one question - when the daemon starts it assumes all existing torrents are new and so the script-added command runs against all of the torrents. Can the patch be modified to not run upon daemon start but only when new torrents are actually added?

Thanks Scott

comment:7 Changed 4 years ago by quinq

I indeed called torrentCallScript() in the wrong place, thank you for correcting it. I didn't notice until now, as I rarely (re)start transmission-daemon. Thanks, good work !

comment:8 follow-up: Changed 4 years ago by ticsone

How to install this patch

comment:9 in reply to: ↑ 8 Changed 4 years ago by scotia

Replying to ticsone:

How to install this patch

I assume you are asking "How do I install these patches?" I assume also that you're running on an *nix/*BSD platform. I've no idea how to build this on Windows.

1) Download and untar the Transmission source 2) Download the two patches (the cli and lib patches: use the lib patch with '-added-only' if you want to run a script when a torrent is added rather than when Transmission is started) 3) Change directory into the top of the untar'd transmission directory tree 4a) On FreeBSD, run "patch < libtransmission-scriptadded-added-only.patch", then "patch < cli-scriptadded.patch". (Do not include the quotes, and specify the path to the patch files if necessary). On Linux, refer to the patch man page 4b) If you can't work out 4a), just inspect the two patch files and backup and edit the source files by hand. The +++ tells you the file to edit, the @@ tells you the line numbers and the + tells you what lines to add. 4a) is a *lot* easier. 5) Continue building as normal (configure, make, install)

Hopefully this helps.

comment:10 Changed 4 years ago by cfpp2p

I think the libtransmission-scriptadded-added-only.patch could be better if we waited until after the "Magnet Verify" https://trac.transmissionbt.com/changeset/14234 before running the torrentAddedScript.

comment:11 follow-up: Changed 3 years ago by quinq

I think that what you linked there is for stopping a torrent. When running the script, the torrent should be ready whichever source it comes from.

comment:12 in reply to: ↑ 11 Changed 3 years ago by cfpp2p

Replying to quinq:

I think that what you linked there is for stopping a torrent. When running the script, the torrent should be ready whichever source it comes from.

I've implemented the libtransmission-scriptadded-added-only.patch and it works perfectly for whichever source when

tr_torrentVerify (tor, NULL, NULL)

then

torrentCallScript (tor, tr_sessionGetTorrentAddedScript (tor->session))

are encapsulated within an

if (tr_torrentHasMetadata (tor)) statement.

And immediately after verifying the magnet we must then call

torrentCallScript (tor, tr_sessionGetTorrentAddedScript (tor->session))

Then we're consistent whether the source is a .torrent or magnet. Otherwise with a .torrent file we run the Torrent Added Script after verify, and with a magnet before the verification.

Last edited 3 years ago by cfpp2p (previous) (diff)

comment:13 follow-up: Changed 3 years ago by quinq

Great, could you attach a patch ?
Thanks !

comment:14 in reply to: ↑ 13 Changed 3 years ago by cfpp2p

Replying to quinq:

Great, could you attach a patch ?
Thanks !

Only difference from libtransmission-scriptadded-added-only.patch is in torrent.c

patch from current trunk

@@ -962,10 +1026,13 @@
 
   tor->tiers = tr_announcerAddTorrent (tor, onTrackerResponse, NULL);
 
-  if (isNewTorrent)
+  if ((isNewTorrent) && (tr_torrentHasMetadata (tor)))
     {
       tor->startAfterVerify = doStart;
       tr_torrentVerify (tor, NULL, NULL);
+
+      if (tr_sessionIsTorrentAddedScriptEnabled (tor->session))
+        torrentCallScript (tor, tr_sessionGetTorrentAddedScript (tor->session));
     }
   else if (doStart)
     {
@@ -1878,6 +1945,9 @@
       tr_logAddTorInfo (tor, "%s", "Magnet Verify");
       refreshCurrentDir (tor);
       tr_torrentVerify (tor, NULL, NULL);
+
+      if (tr_sessionIsTorrentAddedScriptEnabled (tor->session))
+        torrentCallScript (tor, tr_sessionGetTorrentAddedScript (tor->session));
     }
 }
 
Last edited 3 years ago by cfpp2p (previous) (diff)

comment:15 Changed 3 years ago by cfpp2p

Also we might want to use x190's check before verify method to prevent queuing of empty downloads (ticket:4034#comment:7) at both:

.torrent file

if ((isNewTorrent) && (tr_torrentHasMetadata (tor)))
  {
   tor->startAfterVerify = doStart;
   if (tr_torrentGetCurrentSizeOnDisk (tor)) tr_torrentVerify (tor, NULL, NULL);

magnet

if (tr_torrentGetCurrentSizeOnDisk (tor))
        {
          tr_logAddTorInfo (tor, "%s", "Magnet Verify");
          tr_torrentVerify (tor, NULL, NULL);
        }

edit->oops...

Last edited 3 years ago by cfpp2p (previous) (diff)

comment:16 Changed 20 months ago by atommixz

why this patch does not take into mainline? You have the option to run scripts after downloading, but you do not add the option to run scripts before downloading. What reasons?

comment:17 Changed 20 months ago by atommixz

  • Resolution wontfix deleted
  • Status changed from closed to reopened
Note: See TracTickets for help on using tickets.