Custom Plugins - User Script

So you made CouchPotato even more awsome?
weezywizzle
Junior Member
Posts: 9
Joined: 06 Feb 2015 10:09
Status: Offline

My-Script.py

Postby weezywizzle » 19 Feb 2015 16:32

f = open("My-Text-File.txt","w")
f.write(MovieDirectory)
f.close()

from subprocess import Popen
p = Popen("My-Script.cmd", cwd=r"Path-To-My-Script")
stdout, stderr = p.communicate()

weezywizzle
Junior Member
Posts: 9
Joined: 06 Feb 2015 10:09
Status: Offline

Re: Custom Plugins - User Script

Postby weezywizzle » 19 Feb 2015 16:35

In My-Script.py, I need to assign the renamed file name to the variable 'MovieDirectory'. I've tried different things and I'm stuck.

FelipeSQ
Junior Member
Posts: 3
Joined: 20 Feb 2015 01:43
Status: Offline

Re: Custom Plugins - User Script

Postby FelipeSQ » 20 Feb 2015 01:48

Hi Clinton,

First of all, great post and I have to say I was kinda sad I couldn't see a way to add a post script to my Couch Potato, but this thing is pretty neat!
I added everything you mentioned, just put my own name for my Post Process script and folder and I added the link to a shell script that I have and used to fix my wrongly created MKV files.

I got it when you said that the way to test it, would be to add logs everywhere and then run the script through CouchPotato and see the log results. So my main question to is, is there any way for me to trigger the script from Couch Potato? Do I have to go somewhere and do a force post process (if I want to try with files that have already been processed). If so, how and where can I do that?

In advance, I thank you for your attention and patience.

Best,
Felipe.

User avatar
clinton.hall
Moderator
Posts: 9224
Joined: 28 Jun 2012 12:55
Has thanked: 94 times
Been thanked: 465 times
Contact:
Status: Offline

Re: Custom Plugins - User Script

Postby clinton.hall » 20 Feb 2015 12:24

@ FelipeSQ
Yes, to trigger this, you need to rename a file. So either download a movie, or take an existing move and do a "manual rename" by going to wanted, clicking the "..." button then selecting the option to force scan a folder.

User avatar
clinton.hall
Moderator
Posts: 9224
Joined: 28 Jun 2012 12:55
Has thanked: 94 times
Been thanked: 465 times
Contact:
Status: Offline

Re: Custom Plugins - User Script

Postby clinton.hall » 20 Feb 2015 12:29

@ weezywizzle

try this

Code: Select all

from couchpotato.core.event import addEvent
from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin
from subprocess import Popen, PIPE
import traceback

log = CPLog(__name__)


class PostProcess(Plugin):

    def __init__(self):
        addEvent('renamer.after', self.callscript)

   
    def callscript(self, message = None, group = None):
        imdbid = group['library']['identifier']
        moviefile = group['renamed_files']
        original = group['files']['movie'][0]
       
        f = open("My-Text-File.txt","w")
        f.close()
        for x in moviefile:
            f.write(x)
        f.close()
        return True

User avatar
redglory
Junior Member
Posts: 24
Joined: 22 Aug 2012 00:16
Been thanked: 1 time
Contact:
Status: Offline

Re: Custom Plugins - User Script

Postby redglory » 20 Feb 2015 15:36

You have to use API to trigger events like rename.after when CP runs renamer

sent from my Nexus4
Plasma: Panasonic TX-55ST60E
HTPC: LC-Power 1320mi :: Sapphire PURE WHITE Fusion E350 :: 4GB DDR3 :: SSD Corsair Nova 2 [SO] :: 4TB [Storage] || Android PC: Minix X5
powered by Win7 :: XBMC Gotham :: SABnzbd :: CouchPotato :: SickBeard

weezywizzle
Junior Member
Posts: 9
Joined: 06 Feb 2015 10:09
Status: Offline

Re: Custom Plugins - User Script

Postby weezywizzle » 20 Feb 2015 18:26

I've made some progress. I've tested and confirmed that my script works when it gets the file name passed to it. The problem I'm having is with main.py

from couchpotato.core.event import addEvent
from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin
from subprocess import Popen, PIPE
import traceback

log = CPLog(__name__)


class PostProcess(Plugin):

def __init__(self):
addEvent('renamer.after', self.callscript)


def callscript(self, message = None, group = None):
imdbid = group['library']['identifier']
moviefile = group['renamed_files']
original = group['files']['movie'][0]

command = ['C:\\AppData\\CouchPotatoServer\\custom_plugins\\PostProcess\\CouchPotatoServer-Update-Plex-Library.py']
#command.append(imdbid)
#command.append(original)
for x in moviefile:
command.append(x)

try:
p = Popen(command, stdout=PIPE)
res = p.wait()
if res == 0:
log.info('PostProcess Script was called successfully')
return True
else:
log.info('PostProcess Script returned an error code: %s', str(res))

except:
log.error('Failed to call script: %s', (traceback.format_exc()))


return False

It is not running my script so I don't know if it's passing the moviefile variable or not. This is the error that I'm getting in CouchPotato:

02-20 11:13:17 ERROR [ couchpotato.core.event] Error in event "renamer.after", that wasn't caught: Traceback (most recent call last):
File "C:\Program Files (x86)\CouchPotatoServer\couchpotato\core\event.py", line 15, in runHandler
return handler(*args, **kwargs)
File "C:\AppData\CouchPotatoServer\custom_plugins\PostProcess\main.py", line 17, in callscript
imdbid = group['library']['identifier']
KeyError: 'library'
encoding=cp1252 debug=False args=['--data_dir', 'C:\\AppData\\CouchPotatoServer'] app_dir=C:\Program Files (x86)\CouchPotatoServer data_dir=C:\AppData\CouchPotatoServer desktop=None options=Namespace(config_file='C:\\AppData\\CouchPotatoServer\\settings.conf', console_log=False, daemon=False,

data_dir='C:\\AppData\\CouchPotatoServer', debug=False, pid_file='C:\\AppData\\CouchPotatoServer\\couchpotato.pid', quiet=False)

weezywizzle
Junior Member
Posts: 9
Joined: 06 Feb 2015 10:09
Status: Offline

Re: Custom Plugins - User Script

Postby weezywizzle » 20 Feb 2015 19:35

This is the error I'm getting in CouchPotato now:

02-20 12:29:12 ERROR [ PostProcess] Failed to call script: Traceback (most recent call last):
File "C:\AppData\CouchPotatoServer\custom_plugins\PostProcess\main.py", line 28, in callscript
p = Popen(command, stdout=PIPE)
File "C:\Python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 193] %1 is not a valid Win32 application

User avatar
clinton.hall
Moderator
Posts: 9224
Joined: 28 Jun 2012 12:55
Has thanked: 94 times
Been thanked: 465 times
Contact:
Status: Offline

Re: Custom Plugins - User Script

Postby clinton.hall » 20 Feb 2015 22:30

try

Code: Select all

from couchpotato.core.event import addEvent
from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin
from subprocess import Popen, PIPE
import traceback

log = CPLog(__name__)


class PostProcess(Plugin):

    def __init__(self):
        addEvent('renamer.after', self.callscript)

   
    def callscript(self, message = None, group = None):       
        f = open("My-Text-File.txt","w")
        f.close()
        for x in group['renamed_files']:
            f.write(x)
        f.close()
        return True


this gets rid of the un-needed imdb lookup.

bcre3306
Junior Member
Posts: 1
Joined: 27 Feb 2015 14:04
Status: Offline

Re: Custom Plugins - User Script

Postby bcre3306 » 27 Feb 2015 14:08

ok so I am trying to run a super simpler post processing script and I just can't figure it out. If someone could help me I would greatly appreciate it. All I want it to do is run /mnt/array/share/repairmkv.sh against the downloaded file. What should my main.py look like??

User avatar
clinton.hall
Moderator
Posts: 9224
Joined: 28 Jun 2012 12:55
Has thanked: 94 times
Been thanked: 465 times
Contact:
Status: Offline

Re: Custom Plugins - User Script

Postby clinton.hall » 27 Feb 2015 22:24

Take a look at the first post on this thread... Just add the path to your script and comment out/delete the lines pertaining to the arguments you don't need.

User avatar
saitoh183
Junior Member
Posts: 19
Joined: 02 Jul 2012 17:27
Contact:
Status: Offline

Re: Custom Plugins - User Script

Postby saitoh183 » 09 Mar 2016 03:33

im trying to use this function to launch a script and im getting the following error:

Code: Select all

ERROR
[  couchpotato.core.loader] Can't import PostProcess: Traceback (most recent call last):
  File "/opt/couchpotato_fr/couchpotato/core/loader.py", line 58, in run
    m = self.loadModule(module_name)
  File "/opt/couchpotato_fr/couchpotato/core/loader.py", line 164, in loadModule
    return import_module(name)
  File "/opt/couchpotato_fr/libs/importhelper/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/svc_saitoh/.couchpotato_fr/custom_plugins/PostProcess/__init__.py", line 1, in <module>
    from .main import PostProcess
  File "/home/svc_saitoh/.couchpotato_fr/custom_plugins/PostProcess/main.py", line 6, in <module>
    import movieprobe
  File "/home/svc_saitoh/.couchpotato_fr/custom_plugins/PostProcess/movieprobe.py", line 43
    except Exception as e:
         ^
SyntaxError: invalid syntax


here is a link to the script im trying to use

https://drive.google.com/open?id=0B-nxn ... hKaG14aUk0

User avatar
clinton.hall
Moderator
Posts: 9224
Joined: 28 Jun 2012 12:55
Has thanked: 94 times
Been thanked: 465 times
Contact:
Status: Offline

Re: Custom Plugins - User Script

Postby clinton.hall » 31 Mar 2016 14:16

python is very format specific. Probably bad indenting or wrong syntaxt.
Can't tell for sure as the link above is somewhat restricted.

tjalfe
Junior Member
Posts: 29
Joined: 14 Dec 2012 13:22
Contact:
Status: Offline

Re: Custom Plugins - User Script

Postby tjalfe » 15 Jul 2016 09:24

Can i create a custom script/plugin which sends a wake on lan when the downloader report that a download has completed?

User avatar
clinton.hall
Moderator
Posts: 9224
Joined: 28 Jun 2012 12:55
Has thanked: 94 times
Been thanked: 465 times
Contact:
Status: Offline

Re: Custom Plugins - User Script

Postby clinton.hall » 03 Oct 2016 01:00

yes, this is possible. My nzbToMedia script actually has an option to perform WOL before processing.

chris247
Junior Member
Posts: 5
Joined: 01 Nov 2016 09:48
Status: Offline

Re: Custom Plugins - User Script

Postby chris247 » 02 Nov 2016 09:40

I created everything as the 1st post says and cp doesn't do anything after it moves and renames a movie. I simply want it to run a .sh script that i have in my /home/plex/scripts/uploadMovies.sh Looking at the logs I don't think it even knows the PostProcess folder and files are there. Everything is owned by plex user which what I use to run couchpotato

User avatar
clinton.hall
Moderator
Posts: 9224
Joined: 28 Jun 2012 12:55
Has thanked: 94 times
Been thanked: 465 times
Contact:
Status: Offline

Re: Custom Plugins - User Script

Postby clinton.hall » 02 Nov 2016 12:41

Can you show any logs?
Did you resatrt CouchPotato after adding the files?

chris247
Junior Member
Posts: 5
Joined: 01 Nov 2016 09:48
Status: Offline

Re: Custom Plugins - User Script

Postby chris247 » 02 Nov 2016 13:44

Yes i restarted cp. There's nothing on the logs just the typical found nzb, completed, moving etc

chris247
Junior Member
Posts: 5
Joined: 01 Nov 2016 09:48
Status: Offline

Re: Custom Plugins - User Script

Postby chris247 » 28 Dec 2016 11:18

decided to give this another try and now at least I get an error on the logs. I get this:
ERROR
[ couchpotato.core.event] Error in event "renamer.after", that wasn't caught: Traceback (most recent call last): File "/opt/couchpotato/couchpotato/core/event.py", line 15, in runHandler return handler(*args, **kwargs) File "/home/plex/.couchpotato/custom_plugins/PostProcess/main.py", line 17, in callscript imdbid = group['library']['identifier'] KeyError: 'library' encoding=UTF-8 debug=False args=['--daemon'] app_dir=/opt/couchpotato data_dir=/home/plex/.couchpotato desktop=None options=Namespace(config_file='/home/plex/.couchpotato/settings.conf', console_log=False, daemon=True, data_dir=None, debug=False, pid_file='/home/plex/.couchpotato/couchpotato.pid', quiet=False)

All I did was add my script path to the original main.py posted on the 1st post.

EDIT: I walked to away from my computer for a few minutes after seeing this error and was surprised to see that the script worked but only once. I restarted cp and it worked again but only once again. Don't know whats going on.

User avatar
clinton.hall
Moderator
Posts: 9224
Joined: 28 Jun 2012 12:55
Has thanked: 94 times
Been thanked: 465 times
Contact:
Status: Offline

Re: Custom Plugins - User Script

Postby clinton.hall » 28 Dec 2016 21:18

imdbid = group['library']['identifier']

KeyError: 'library'

This error suggests that "group" does not contain a "library" key for this movie.
you could add debug printing to find out what keys group does contain, and find out where the imdb id is contained.


Return to “External Tools”