Custom Plugins - User Script

So you made CouchPotato even more awsome?
craig91
Member
Posts: 57
Joined: 30 Mar 2013 03:49
Has thanked: 2 times
Been thanked: 2 times
Contact:
Status: Offline

Re: Custom Plugins - User Script

Postby craig91 » 22 Dec 2014 23:28

clinton.hall wrote:What user runs CouchPotato? Does that user have permissions to write into logs/processmovie.log?


Also, should that actually be /logs/processmovie.log? Without the "/" it is looking for a logs directory inside the cureny directory!!!


You were right about the log location. I thought the directories would have been relative to the script location and not absolute. Also why it showed failed because the copy failed - due to the location again. :P

Thanks, it's working :)

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 » 22 Dec 2014 23:44

glad you got it working as you want.

secjoe88
Junior Member
Posts: 1
Joined: 13 Jan 2015 07:48
Status: Offline

Re: Custom Plugins - User Script

Postby secjoe88 » 13 Jan 2015 08:03

Please help!! Trying to implement a custom plugin for a postprocessing script! As far as I can tell I followed the instructions, but the plugin doesn't even load!

my __init__.py

Code: Select all

from .main import PostProcess
from couchpotato.core.logger import CPLog

log=CPLog(__name__)
def autoload():
   log.info('PostProcess plugin load attempted.')
   return PostProcess()


and my main.py

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):
      log.info('PostProcessor plugin initiating...')
      addEvent('renamer.after',self.callscript)
      log.info('PostProcessor plugin succesfully loaded!')

   def callscript(self, message=none, group=None):
      log.info('Attempting to call postprocessor callscript...')
      command=['/media/movies/post_movies.sh']
      
      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 error code: %s', str(res))
      except:
         log.error('Failed to call script: %s', (traceback.format_exc()))
      
      return False


there are several instances in which I attempted logging for debugging purposes (in the __init__ and main .py files), but absolutely nothing even shows up in the logs! Couchpotato runs as user 'couchpotato', all files are 755 for the user 'couchpotato', data_dir is set, files are in the correct custom_plugins folder, but still I get nothing.

I'm posting to see if there is anything obviously amiss in my code, if it comes to it i can provide log files as well. Thanks!

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 » 13 Jan 2015 11:15

Do these 2 files exist in their own directory inside the custom plugins folder?
With debug logging enable, does this plugin appear in the logs on restart?.... Should be a whole section of "Loading xyz"

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

Re: Custom Plugins - User Script

Postby weezywizzle » 06 Feb 2015 10:18

Can anyone help me with a script to write the name of a renamed movie to a text file and then call a command script? I don't know anything about writing scripts in python and I've been trying for a while but I've made zero progress. What I'm trying to do is get the name of each movie after it's renamed then pass that to another script so that Plex only scans the newly created movie directory instead of the entire section. Yesterday I did it with Sick-Beard and it works fine and I was able to do it all in command scripts though. Any help would be appreciated.

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

Re: Custom Plugins - User Script

Postby weezywizzle » 19 Feb 2015 07:20

Can someone please tell me how to make this pass the full path of the renamed file to a text file?

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 » 19 Feb 2015 12:29

The first post in is thread covers most of what you need... Then just call a shell script that writes the 3rd argument into a tex file, or edit the "command" section to just open a file and write 'movie' into the file.

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

Re: Custom Plugins - User Script

Postby weezywizzle » 19 Feb 2015 16:08

clinton.hall wrote:The first post in is thread covers most of what you need... Then just call a shell script that writes the 3rd argument into a tex file, or edit the "command" section to just open a file and write 'movie' into the file.


I've read every post but I'm still having trouble. I don't know anything about python and I know what I'm trying to do is simple but I have no understanding of it. So far, the parts that I have working have been from trial and error with the syntax. I know what I need it to do but I just don't know how to make it do it.

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

__init__.py

Postby weezywizzle » 19 Feb 2015 16:29

from .main import PostProcess


def autoload():
return PostProcess()

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

main.py

Postby weezywizzle » 19 Feb 2015 16:31

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 = ['Path-To-My-Script\My-Script.py']
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

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??


Return to “External Tools”