Execute plugin: Script not working when action is Torrent removed

General support for problems installing or using Deluge
Post Reply
TrC
New User
New User
Posts: 4
Joined: Thu Mar 30, 2023 3:53 pm

Execute plugin: Script not working when action is Torrent removed

Post by TrC »

Hello,

I've successfully created scipts that sends me a Discord notification when a torrent is completed. I'd like to get a similar notification when a torrent is removed, but I can't seem to get it to work.

The script below works fine if the event that triggers it is "Torrent added", but if I select "Torrent removed", nothing happens. Any help would be appreciated. Thanks

Code: Select all

#!/bin/bash

DISCORD_WEBHOOK_URL="hidden"
torrentid="$1"
torrentname="$2"
torrentpath="$3"

# Send Discord notification
message_content=">>> :x: **Torrent Deleted:** $torrentname \n\n**Path:** $torrentpath"
curl -H "Content-Type: application/json" -X POST -d "{\"content\":\"$message_content\"}" "$DISCORD_WEBHOOK_URL"
mhertz
Moderator
Moderator
Posts: 2195
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: Execute plugin: Script not working when action is Torrent removed

Post by mhertz »

I tried reproduce, and did, though nothing gotten in log. However after restarting deluge(d), then worked again. I just added a shell-script touch'ing a file into /tmp, and checked the timestamp on it to verify.

If still persists after restart, then try enable debug-logging and see if you can catch an error on your end, despite I didn't see one - it logs error of script not found and non-zero exit-code btw, neither of interest here, since you know script found/works from other events scheduled, but nothing else to suggest, and doesn't hurt atleast.

Edit: It never runs the extra added execute event, as log doesn't have corresponding "Running command for (added|completed|removed)" line for new event. In the code it does save the new event to config-file right away, but doesn't update event-handler which then only happens at initialization(plugin start). Instead of refactoring out the few relevant lines to own function and call also from when event added and not just from starting plugin as now, then I made it run self.enable() again when adding new event as doesn't include any real other cruft there anyways, but seriously there's no need to use this, as can just simply restart after new preferences changes, so just ignore this honestly:

Execute-1.4.egg (I upped version number, and changed entry-points, to make it take precedence over original)
TrC
New User
New User
Posts: 4
Joined: Thu Mar 30, 2023 3:53 pm

Re: Execute plugin: Script not working when action is Torrent removed

Post by TrC »

Hi mhertz,

Thank you very much for your help and prompt reply. A restart of the container + the new version of the plugin fixed it! (probably just needed the restart, but I did both at the same time). I'm mad at myself that I didn't try it before posting, but I created a couple of other scripts, tried a few different versions of each one and they worked without needing to restart the container... I didn't think that could be the issue.

Just one more quick question, please: It would be great if my discord notification would include the size of the torrent. I can't seem find a way to do it... Probably not possible?

Thank you :)
mhertz
Moderator
Moderator
Posts: 2195
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: Execute plugin: Script not working when action is Torrent removed

Post by mhertz »

You're welcome :)

Something like adding to $message_content after name or path:

Code: Select all

 ($(du -sh "$3/$2" | cut -f1))
To show a parenthesis with the size in it, afterwards. Or add it in new line with extra '\n''s. If you need more help let me know with it. Btw, you can instead use 'stat -c %s' and omit extra piped 'cut', but still needs processing as shows in bytes, in contrary to du -sh'.

Edit:

Just in case, here's '$message_content' line with added paranthesis with size after torrent name:

Code: Select all

message_content=">>> :x: **Torrent Deleted:** $torrentname ($(du -sh "$3/$2" | cut -f1))\n\n**Path:** $torrentpath"
Alternative version, which adds full path to torrent, so omits second line always listing path to your download-folder:

Code: Select all

message_content=">>> :x: **Torrent Deleted:** $3/$torrentname ($(du -sh "$3/$2" | cut -f1))"
TrC
New User
New User
Posts: 4
Joined: Thu Mar 30, 2023 3:53 pm

Re: Execute plugin: Script not working when action is Torrent removed

Post by TrC »

Hi mhertz,

Thank you very much for your help =)

I tried your first suggestion. I did get the parenthesis after the torrent name, but no info on the torrent size inside them. The final format is torrent_name (). Here's the full code below. Did I miss something? Thank you!

Code: Select all

#!/bin/bash

DISCORD_WEBHOOK_URL="..."
torrentid="$1"
torrentname="$2"
torrentpath="$3"

# Send Discord notification
message_content=">>> :envelope_with_arrow: **Torrent Added:** $torrentname ($(du -sh "$3/$2" | cut -f1))\n\n**Path:** $torrentpath"
curl -H "Content-Type: application/json" -X POST -d "{\"content\":\"$message_content\"}" "$DISCORD_WEBHOOK_URL"
Last edited by TrC on Fri Mar 31, 2023 9:32 pm, edited 1 time in total.
mhertz
Moderator
Moderator
Posts: 2195
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: Execute plugin: Script not working when action is Torrent removed

Post by mhertz »

I'm sorry mate, I have no clue about what that is, i'm affraid :( Hopefully others have any input for you regarding such, as i'm out of ideas, and did a quick googling, and nothing really starred me in my face, or how put. I don't see why NTFS should be different, and just tested an usb stick with ntfs formating, and 'du' worked fine for showing size there.

Last, Also i'm a dumba**, for my original proposal to you, as obviously you cannot calculate the size of torrent data just deleted from disc, don't know why I didn't think about that before, well of-course if you remove but not delete data, but regardless.

Sorry I couldn't help you better.
TrC
New User
New User
Posts: 4
Joined: Thu Mar 30, 2023 3:53 pm

Re: Execute plugin: Script not working when action is Torrent removed

Post by TrC »

After some further testing, I don't think it has anything to do with the drive filesystem. I thought the command you suggested would retrieve the information from the torrent itself (I'm a total noob =P), but I see now that it calculates the folder/file size of the torrent. So I guess the same way it does not work on "torrent delete", it probably won't work on "torrent added" as its size is 0 just after the torrent is added. I guess it would make sense it works on the event "torrent complete" (?) that's the only one I haven't tested yet. The other two types of events, after some tests, don't always give me (), sometimes they return a value, but it's almost always off... There was one case where I deleted a 14.39GB torrent and the message said (15G) though... Kinda close :D Anyway, many thanks for your help, really appreciated.
mhertz
Moderator
Moderator
Posts: 2195
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: Execute plugin: Script not working when action is Torrent removed

Post by mhertz »

Thanks bro, you're a good sport beeing still cordial and appreciative despite my stupid mistakes :)

Indeed you're of-course right, and I didn't even think of that either I must admit, damn i'm clearly on a roll today bursting out with cleverness :lol: (Well new day today, but whatever...)

Yeah as plugin didn't support retrieving size from torrent(libtorrent) itself, then I tried working around it with simple shell commands instead, displaying issues however as you found.

Luckily, libtorrent supports 'total_size' status field, and plugin already has a hook on PreTorrentRemoved event, to catch name and such before deleted, so was easy enough to simply add in that extra status field, now as '$4' and I ran it through deluge.common's fsize() to get nice number formating and e.g. GiB appendings etc, instead of bytes, and no reason use shell commands for that, when we already mod the plugin I gathered.

Link is same as before, and so to append size in parenthesis, then use '($4)', let me know if need help with it of-course.
Post Reply