Post your execute plugin scripts!

Suggest, post, or discuss plugins for Deluge
TPG
New User
New User
Posts: 5
Joined: Fri May 22, 2015 9:23 am

Re: Post your execute plugin scripts!

Post by TPG »

This is basically a very simple bash script to dump info upon completion in an html file instead of a log file. The html can then be viewed from anywhere over my LAN
Here is my code:

Code: Select all

#!/bin/bash
torrentid=$1
torrentname=$2
torrentpath=$3
OUTPUT="/path/to/nas/share/deluge_scripts/info.html"
echo "<div class="date">$(date)</div> <div class="lblDone">$torrentname</div><a href=\"//$torrentpath\">$torrentpath</a><br>" >> $OUTPUT
And inside the info.html file, I have some styling:

Code: Select all

<style>
body {background-color:lightgray; border:2px solid gray; padding:1em;}
img	{float:right;}
.date {color:#6c42c9; display: inline-block;}
.lblAdded {color:red; padding-right:1em;display: inline-block;}
.lblDone 	{color:green; padding-right:1em;display: inline-block;}
</style>
bri

Re: Post your execute plugin scripts!

Post by bri »

Since nobody has posted anything written in Windows Batch yet...

Code: Select all

:addtorrents %torrentid% %torrentname% %torrentpath%
::Copies any MP3 files, any M4A files, and any PDF files (ONLY copies PDFs if MP3 and/or M4A files are found) to the folder D:\autoadd, and adds a line to %userprofile%\Music\deluge-script.log with the date/time of script run
setlocal
set torrentid=%1
set torrentname=%2
set torrentpath=%3
set "log=%userprofile%\Music\deluge-script.log"
set autoadd=D:\autoadd
@echo on
::reset errorlevel
set errorlevel=
cmd /c exit /b 0

echo Download of %torrentname% Completed at %date% %time%: ^<%torrentpath%\%torrentname%^> ^<%torrentid%^>  >> "%log%"
pushd %torrentpath%\%torrentname%
call:mktmp filestoadd
call:findmp3 && call:findpdf
call:findm4a && call:findpdf
type %filestoadd%
for /f "tokens=*" %%a in (%filestoadd%) do (copy /b /d /y "%%a" d:\autoadd)
endlocal & del %filestoadd% & exit /b %errorlevel%

:findmp3
2>nul 1>> %filestoadd% dir /b /s *.mp3 && exit /b 0 || exit /b 1
:findm4a
2>nul 1>> %filestoadd% dir /b /s *.m4a && exit /b 0 || exit /b 1
:findpdf
2>nul 1>> %filestoadd% dir /b /s *.pdf && exit /b 0 || exit /b 1

:mktmp (out)tmpfilename
setlocal
pushd %temp%
set tmpfilename=%1
set filename=addtorrents.%tmpfilename%.%random%.tmp
type nul>%filename%
endlocal & set %tmpfilename%=%temp%\%filename%
D:\autoadd is a directory symbolic link* that points to my "Automatically add to iTunes" folder, and Google Music syncs with iTunes. I like to keep the PDFs that come with albums, too.

I didn't work very hard on making the script particularly clean or commenting it very well, but it will get the job done for me :)

*mklink /d linkname linktarget
TPG
New User
New User
Posts: 5
Joined: Fri May 22, 2015 9:23 am

Re: Post your execute plugin scripts!

Post by TPG »

To those who also have xbmc/kodi running, this little script will show a notification when media is added and when media is completed.

Media added:

Code: Select all

#!/bin/bash
torrentid=$1
torrentname=$2
torrentpath=$3
ssh osmc@osmc_ip_address 'xbmc-send -a "Notification(Deluge: Media added,' $torrentname , 5000 , "/path/to/image" ')"'
Media completed:

Code: Select all

#!/bin/bash
torrentid=$1
torrentname=$2
torrentpath=$3
ssh osmc@osmc_ip_address 'xbmc-send -a "Notification(Deluge: Media ready,' $torrentname , 5000 , "/path/to/image" ')"'
Of course this requires that the machine running deluged have ssh access to the machine running xbmc/kodi without having to enter password. This can be achived by creating key pair using

Code: Select all

ssh-keygen
Note that: 5000 is the time to display the notification in milliseconds. /path/to/image is the icon image to display which should be resolvable by xbmc/kodi (I uploaded mine to the home folder in kodi)
haydent
Member
Member
Posts: 30
Joined: Tue Apr 12, 2016 10:24 am

Re: Post your execute plugin scripts!

Post by haydent »

Here's a little history script that i made to use with my textTab plugin to record recently finished torrents and show them in a tab in deluge.
http://forum.deluge-torrent.org/viewtop ... =9&t=53953

Code: Select all

#!/usr/bin/python
import sys
import os
import time
#sys.argv[1] - id
#sys.argv[2] - name
#sys.argv[3] - path


f = open("/media/sdf1/home/fh8123789/private/deluge/data/History.txt", "a")
try:
    f.write("%s - %s - %s\n" % (os.path.basename(os.path.normpath(sys.argv[3])), sys.argv[2], time.strftime("%d/%m")))
except:
    pass
    
f.close()
x-fuego

Sorting: move downloaded torrent data to other location

Post by x-fuego »

Hi! I'am a newbie to the bash-scripting, but I think that my script is nice. This script I use on complete with Execute plugin. Script will check extentions of downloaded files and move torrent data to other location. So we can sort data by type of. Because deluge-console isn't supported mv command, script do it via two steps: first rm command, then add. After re-adding torrent, deluge will recheck data and start seeding again.

Code: Select all

#!/bin/bash

script_name=$(basename $0)

torrent_id=$1
torrent_name=$2
torrent_path=$3

# you can use any paths, but check write access before
videos_directory_path="$torrent_path/Видео"
audios_directory_path="$torrent_path/Аудио"
programs_directory_path="$torrent_path/Программы"

# current torrent file: we need to back it up it before use "deluge-console rm ..."
torrent_file="${HOME}/.config/deluge/state/${torrent_id}.torrent"
# torrent backup path name
tmp_file="/tmp/$torrent_id.torrent"

# current full path to downloaded data
data_full_path="$torrent_path/$torrent_name"

log()
{
        logger -t "$script_name" "$@"
        #echo "$@"
}

# this func will try to execute command, and, if it failure, write messages to system log
run()
{
        local result_output
        local result_code
        local command_text
        local whitespace="[[:space:]]"

        for i in "$@"
        do
                if [[ $i =~ $whitespace ]]; then
                        i=\"$i\"
                fi
                if [ -n "$command_text" ]; then
                        command_text="$command_text $i"
                else
                        command_text="$i"
                fi
        done

        result_output=$(eval "$command_text" 2>&1)
        result_code=$?
        if [ $result_code -ne 0 ]; then
                log  "$result_output"
        fi
        return $result_code;
}

#if [ -e "$data_full_path" ] ||  {[ "$data_full_path" != "$videos_directory_path" ] && [ "$data_full_path" != "$audios_directory_path" ] && [ "$data_full_path" != "$programs_directory_path" ];}; then
if [ -e "$data_full_path" ]; then
        log "Обработка $data_full_path [ID: $torrent_id]"
fi

handle_as="none"

if [ -f "$data_full_path" ]; then

        run chmod a+rwx "$data_full_path"
        #run chown nobody:nogroup "$data_full_path"

		# need to modify for case insensitive checking
        if [[ $torrent_name == *.mkv || $torrent_name == *.avi || $torrent_name == *.mpg ]]; then
                handle_as="video"
        elif [[ $torrent_name == *.mp3 || $torrent_name == *.flac ]]; then
                handle_as="audio"
        elif [[ $torrent_name == *.exe ]]; then
                handle_as="program"
        fi
fi

if [ -d "$data_full_path" ] && [ "$data_full_path" != "$videos_directory_path" ] && [ "$data_full_path" != "$audios_directory_path" ] && [ "$data_full_path" != "$programs_directory_path" ]; then

        run chmod -R a+rwxX "$data_full_path"
        #run chown -R nobody:nogroup "$data_full_path"

        if [[ -n $(find "$data_full_path" -iname "*.exe") ]]; then
                handle_as="program"
        elif [[ -n $(find "$data_full_path" -iname "*.mkv" -or -iname "VIDEO_TS" -or -iname "*.avi" -or -iname "BDMV" -or -iname "*.mpg") ]]; then
                handle_as="video"
        elif [[ -n $(find "$data_full_path" -iname "*.mp3" -or -iname "*.flac") ]]; then
                handle_as="audio"
        fi
fi

if [ $handle_as != "none" ]; then

        if [ $handle_as == "video" ]; then
                destination_directory_path="$videos_directory_path"
        elif [ $handle_as == "audio" ]; then
                destination_directory_path="$audios_directory_path"
        elif [ $handle_as == "program" ]; then
                destination_directory_path="$programs_directory_path"
        else
                exit
        fi

        if [ ! -d "$destination_directory_path" ]; then
                run mkdir "$destination_directory_path"
        fi

        if [ -d "$destination_directory_path" ] && [ ! -w "$destination_directory_path" ]; then
                run chmod a+rwXt "$destination_directory_path"
        fi

        if [ -f "$torrent_file" ]; then
                run cp "$torrent_file" "/tmp"
                run deluge-console rm $torrent_id
        fi

        run mv "$data_full_path" "$destination_directory_path"
        if [ $? -eq 0 ]; then
                if [ -f "$tmp_file" ] && [ -e "$destination_directory_path" ]; then
                        run deluge-console add -p "$destination_directory_path" "$tmp_file"
                fi
        fi

fi

X_Ch4n
New User
New User
Posts: 3
Joined: Tue Nov 15, 2016 12:39 pm

Re: Post your execute plugin scripts!

Post by X_Ch4n »

Hi guys,
thank you for your great work.

I'm trying to write an execute script. i've a question: what about the spaces inside the torrent name?

this is my simple code:

Code: Select all

#!/bin/bash

torrentid=$1
torrentname=$2
torrentpath=$3
finalpath=$torrentpath/$torrentname

echo $finalpath
If torrent name is such a "This is the torrent Name", the content of variable $torrentname will be 'This' then $torrentpath/$torrentname will point a non-existent dir and i can't perform any operation on the files contained into the dir "This is the torrent name".

My machine is a Debian Jessie (8.6) and Deluge 1.3.10-3 (installed from apt repo.

How can i solve this annoying issue?

Thank you in advance.
mhertz
Moderator
Moderator
Posts: 2182
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: Post your execute plugin scripts!

Post by mhertz »

Use double-quotes on variable-values.

Also, something I recently found out, e.g. "$dir/*.mkv" doesn't work in bash scripts, but needs to be like this: "$dir"/*.mkv for content with spaces. ...Wish everyone would just simply follow the nice unix convention of using underscores instead of spaces and lowercase-only - dammit! :)
X_Ch4n
New User
New User
Posts: 3
Joined: Tue Nov 15, 2016 12:39 pm

Re: Post your execute plugin scripts!

Post by X_Ch4n »

hi mherts,
thank you for your feedback.

Maybe my previous post wasn't so clear.
I'm talking about the case when the torrent name (received as parameter by execute plugin) has space inside:

Example:
When deluge finish to download a torrent named as "Just downloaded torrent", it will activate the execute plugin as follow:

./execute_script.sh 4e1a68ece3c098b16fd1783d106cfade5facaff7 Just downloaded torrent /the/absolute/path/where/to/save/all my torrents/

Code: Select all

torrentid=$1
torrentname=$2
torrentpath=$3
finalpath="$torrentpath"/"$torrentname"

echo $finalpath
will result in:
downloaded/Just

Now..... How can i prevent this?

Thank you in advance for your effort.
mhertz
Moderator
Moderator
Posts: 2182
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: Post your execute plugin scripts!

Post by mhertz »

If testing with e.g.:

echo "$2" > /home/user/test.log
echo "$3" >> /home/user/test.log

Does it still not show full name/path? If it doesn't work, then it's a deluge-bug and needs code-changing.
X_Ch4n
New User
New User
Posts: 3
Joined: Tue Nov 15, 2016 12:39 pm

Re: Post your execute plugin scripts!

Post by X_Ch4n »

yes, of course. No problems when i manually execute the script using the " to the parameters:
e.g.

Code: Select all

./execute_script.sh 4e1a68ece3c098b16fd1783d106cfade5facaff7 "Just downloaded torrent" "/the/absolute/path/where/to/save/all my torrents/"
The matter is Execute plugin calls the bash script without the "..."
Post Reply