AMC - rescan PLEX library section

Running FileBot from the console, Groovy scripting, shell scripts, etc
saitoh183
Posts: 112
Joined: 02 Oct 2012, 16:59

Re: AMC - rescan PLEX library section

Post by saitoh183 »

is it normal that filebot is taking the path + value of $plexsection instead of just taking the value.

Code: Select all

Locking P:\scripts\Filebot.log
Run script [fn:amc] at [Fri Oct 14 14:10:38 EDT 2016]
Parameter: seriesFormat = P:/videos/TV/{n}/{'Season '+s}/{n} - {sxe} - {t}
Parameter: clean = y
Parameter: ut_label = Series
Parameter: exec = ""C:\Program Files (x86)\Plex\Plex Media Server\Plex Media Scanner.exe" --scan --refresh --section %1 --directory "{folder}""
Argument: P:\Processing\sonarr_tv
Argument: C:\4 <---- $plexsection 
User avatar
rednoah
The Source
Posts: 23953
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: AMC - rescan PLEX library section

Post by rednoah »

No. Passing a folder called "4" is not normal. But it's you doing that, so I can't help you there.

PS: Did you replace "cmd.exe" with PowerShell or something? I have no idea what you're doing, but just running a few fairly simple commands can't be that difficult. You could have learned some basic Python or Groovy by now.
:idea: Please read the FAQ and How to Request Help.
saitoh183
Posts: 112
Joined: 02 Oct 2012, 16:59

Re: AMC - rescan PLEX library section

Post by saitoh183 »

Yes i do use powershell for other windows task so my default console is powershell.

I used groovy in the pass a bit but since my basic script was working in powershell, everything was fine but now it had a new level of complexity so maybe it might be time to change language...
saitoh183
Posts: 112
Joined: 02 Oct 2012, 16:59

Re: AMC - rescan PLEX library section

Post by saitoh183 »

rednoah wrote:No. Passing a folder called "4" is not normal. But it's you doing that, so I can't help you there.
So the "No" means i cant pass the variable? Im guessing this would be possible in python and groovy?
User avatar
rednoah
The Source
Posts: 23953
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: AMC - rescan PLEX library section

Post by rednoah »

FileBot is a simple command-line tool that accepts command-line arguments. It works the same regardless of how you invoke it: bash, CMD, groovy, python, ...

The reason I recommend Groovy is because it makes passing command-line arguments very easy (although more verbose) which is what most beginners (including you here) continuously get wrong:
viewtopic.php?f=4&t=1899


In Groovy you can pass arguments as array, so you don't need to worry about how CMD/PS parses your argument string, and with '''...''' String syntax you don't need to worry about escaping at all:

groovy my-filebot-call.groovy

Code: Select all

def p = ['filebot', '-version'].execute()
p.waitForProcessOutput(System.out, System.err)

return p.exitValue()
@see http://groovy-lang.org/groovy-dev-kit.h ... management
:idea: Please read the FAQ and How to Request Help.
saitoh183
Posts: 112
Joined: 02 Oct 2012, 16:59

Re: AMC - rescan PLEX library section

Post by saitoh183 »

With the help of my sister (that knows more python than me) here is the script and it works great so far:

Code: Select all

import shutil
import subprocess
import os
import math
from os.path import getsize


def get_plexscan(section):
    return '"\"C:\\Program Files (x86)\\Plex\\Plex Media Server\\Plex Media Scanner.exe\" --scan --refresh --section %s --directory \"{folder}\""' % section

def get_child(path):
    return [item for item in os.listdir(path) if item not in ('.stignore', '.stfolder')]
   
def get_command(command, extra):
    cmd = list(command)
    cmd.extend(extra)
    return cmd
 
#convert file size to human redable (http://stackoverflow.com/questions/5194057/better-way-to-convert-file-sizes-in-python)
def convert_size(size):
   if (size == 0):
       return '0B'
   size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
   i = int(math.floor(math.log(size,1024)))
   p = math.pow(1024,i)
   s = round(size/p,2)
   return '%s' % s

#Declare variables   
source = "P:\\Processing"
subfolders = os.listdir(source)
logfile = "P:\\Scripts\\Filebot.log"

#Check if log is more than 2Mb and delete it it if needed
logsize = float(convert_size(getsize(logfile)))

#Delete Logfile if it is equal or greater then 2MB
if logsize >= 2:
   os.remove(logfile)
   


filebot_cmd = [
    "C:\\Program Files\\Filebot\\filebot.exe", #ie 'C:\Program Files\Filebot\filebot.exe'
    
    # I recommend replacing 'fn:amc' with the path to a locally saved copy of
    # amc.groovy. 'fn:amc' downloads the script every time you run filebot.exe
    # from filebot.net.  If filebot.net is down, your file processing won't be
    # able to run. You can download the script from here:
    # https://github.com/filebot/scripts/blob/devel/amc.groovy
    '-script', 'P:/Scripts/AMC/amc.groovy',#'fn:amc',

    # Save log files to same directory where this script file is located
    '--log-file', os.path.join(os.path.dirname(os.path.realpath(__file__)), 'Filebot.log'),
    '--action', 'move',
    '--def', 'clean=y',
    '-no-xattr',
    '--log=Fine'
]

for folder in subfolders:

    folder_path = os.path.join(source, folder)
    child = get_child(folder_path)
    childcount = len(child)

    if not childcount:
        #print folder_path + " empty"
        continue

    if folder == "cps":

        #print "Looking for English Movies"
        new_filebot = get_command(filebot_cmd, [

            '--def', 'movieFormat=D:/Videos/Movies/{plex[1..-1]}',
            '--def', 'ut_label=Movies',
            '--def', 'exec=' + get_plexscan(1)
        ])

    elif folder == "cpsfr":

        #print "Looking for New French Movies and/or English Movies"
        dualmovies = os.path.join(source, "dualmovies")

        for path, subdirs, files in os.walk(folder_path):
            for name in files:

                if any(name.endswith(ext) for ext in ('mkv', 'mp4', 'avi')):
                    current_file = os.path.join(path, name)
                    audio = subprocess.check_output([
                        "P:\\apps\\Mediainfo_x64\\MediaInfo.exe",
                        '--Inform=General;%AudioCount%', current_file
                    ])
                    intaudio = int(audio)
                    if intaudio > 1: # Dual audio
                        if os.path.join(folder_path, name) == current_file:
                            # File exist at the root
                            print "Moving file: %s to dualmovies" % name
                            dst = os.path.join(dualmovies, name)
                            os.rename(current_file, dst)
                        else:
                            # File is in subfolder, move subfolder
                            print "Moving directory: %s to dualmovies" % path
                            shutil.move(path, dualmovies)
        
        if len(get_child(folder_path)): # Verify cpsfr
            new_filebot = get_command(filebot_cmd, [

                '--def', 'movieFormat=D:/Videos/French movies/{plex[1..-1]}',
                '--def', 'ut_label=Movies',
                '--def', 'exec=' + get_plexscan(17)
            ])
        else:
            continue

    elif folder == "dualmovies":
        # In case the movies were added, but never processed by filebot
        #print "Looking for dual audio Movies"
        new_filebot = get_command(filebot_cmd, [

            '--def', 'movieFormat=D:/Videos/Dual Audio Movies/{plex[1..-1]}',
            '--def', 'ut_label=Movies',
            '--def', 'exec=' + get_plexscan(18)
        ])

    elif folder == "sickrage_tv":

        #print "Looking for New French Series"
        new_filebot = get_command(filebot_cmd, [

            '--def', 'seriesFormat=D:/Videos/FrenchTV/{n}/{"Season "+s}/{n} - {sxe} - {t}',
            '--lang', 'fr',
            '--def', 'ut_label=Series',
            '--def', 'exec=' + get_plexscan(6)
        ])

    elif folder == "sonarr_tv":

        #print "Looking for New English Series"
        new_filebot = get_command(filebot_cmd, [

            '--def', 'seriesFormat=D:/Videos/TV/{n}/{"Season "+s}/{n} - {sxe} - {t}',
            '--filter', 'age < 7',
            '--def', 'ut_label=Series',
            '--def', 'exec=' + get_plexscan(2)
        ])
        
    elif folder == "UFC":

        #print "Looking for New UFC PPV/FN"
        new_filebot = get_command(filebot_cmd, [

            '--def', 'movieFormat=D:/Videos/UFC/{plex[1..-1]}',
            '--def', 'ut_label=Movies',
            '--def', 'exec=' + get_plexscan(3)
        ])

    else: # Move to the next folder in subfolders
        continue 
    
    new_filebot.extend(['-non-strict', folder_path])
    subprocess.Popen(new_filebot).wait()
I had one question, would it be possible to change the drive letter in the {folder} before or while i pass it to

'"\"C:\\Program Files (x86)\\Plex\\Plex Media Server\\Plex Media Scanner.exe\" --scan --refresh --section %s --directory \"{folder}\""' % section
saitoh183
Posts: 112
Joined: 02 Oct 2012, 16:59

Re: AMC - rescan PLEX library section

Post by saitoh183 »

I tried a replace like this

Code: Select all

'"\"C:\\Program Files (x86)\\Plex\\Plex Media Server\\Plex Media Scanner.exe\" --scan --refresh --section %s --directory \"{folder.replace("P:", "C:")}\""'
but the result is

Code: Select all

Execute: ""C:\Program Files (x86)\Plex\Plex Media Server\Plex Media Scanner.exe" --scan --refresh --section 3 --directory """
User avatar
rednoah
The Source
Posts: 23953
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: AMC - rescan PLEX library section

Post by rednoah »

:idea: When will you learn to use the Format Editor for prototyping your format expressions? Like seriously? Oh well, you're wasting your own time...

Image

folder is a File object not a String object so you can't call String methods like replace.

Solution:

Code: Select all

{'X:'+folder.path.substring(2)}
:idea: Please read the FAQ and How to Request Help.
saitoh183
Posts: 112
Joined: 02 Oct 2012, 16:59

Re: AMC - rescan PLEX library section

Post by saitoh183 »

I forgot i could use that...i did uses for other pattern testing but didnt think i could use it for figuring out this...sorry
User avatar
rednoah
The Source
Posts: 23953
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: AMC - rescan PLEX library section

Post by rednoah »

saitoh183 wrote:I forgot i could use that...i did uses for other pattern testing but didnt think i could use it for figuring out this...sorry
Of course you can, it's the same thing, just don't rename files with that "format". :lol:
:idea: Please read the FAQ and How to Request Help.
saitoh183
Posts: 112
Joined: 02 Oct 2012, 16:59

Re: AMC - rescan PLEX library section

Post by saitoh183 »

Quick question is there a way to capture Filebot messages? Like i would want to capture Failed to match files to episode data and store it in a variable in python.

The reason is because i use the age filter with tv shows, so i get the failed message when the tv show is a old series. So i would want to capture the errror and use it to rerun filebot but without filter
User avatar
rednoah
The Source
Posts: 23953
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: AMC - rescan PLEX library section

Post by rednoah »

IO redirection. Easy with bash. Surely not too hard with Python. Just checking the exit code 0 (success) or 1 (failure) is probably easier though. ;)
:idea: Please read the FAQ and How to Request Help.
saitoh183
Posts: 112
Joined: 02 Oct 2012, 16:59

Re: AMC - rescan PLEX library section

Post by saitoh183 »

yeah probably getting the exit code will be easier...no bash in windows...
User avatar
rednoah
The Source
Posts: 23953
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: AMC - rescan PLEX library section

Post by rednoah »

FYI: There are many ways to call different commands depending on the input file and metadata.

e.g. depending on xattr metadata:

Code: Select all

--def exec="echo \"Hello! This is a {movie; 'Movie'}{episode; 'Series'}\""

Code: Select all

Execute: echo "Hello! This is a Series"
Hello! This is a Series
Execute: echo "Hello! This is a Movie"
Hello! This is a Movie
:idea: Please read the FAQ and How to Request Help.
saitoh183
Posts: 112
Joined: 02 Oct 2012, 16:59

Re: AMC - rescan PLEX library section

Post by saitoh183 »

Thanks will play around with this
Post Reply