Page 1 of 1

How to create custom post-processing script?

Posted: Tue Sep 22, 2020 3:31 pm
by CovertDad
I've written a python script that will extract a comic file, convert the images to webp, and zip it back up. I'm trying to get it working within Mylar, but I can't figure out what I need to do to my script to make Mylar be able to use it? Like, is there a specific function name I have to implement, what parameters are coming in from Mylar, etc.

Below is what I've cobbled together as the meat of my script:

Code: Select all

def processIssue(dirName=newpath, filename=newfilename):
    if str.endswith(dirName + "/" + filename, "cbr"):
        filepath = unrar(dirName + "/" + filename)
    elif str.endswith(dirName + "/" + filename, "cbz"):
        filepath = unzip(dirName + "/" + filename)

    removeFilesByMatchingPattern(filepath, "z*.*")

    convertImagesToWebP(filepath)
    removeFilesByMatchingPattern(filepath, "*.jpg")
    removeFilesByMatchingPattern(filepath, "*.png")

    filePaths = os.listdir(filepath)

    os.remove(filename)

    zip_file = ZipFile(filepath+'.cbz', 'w')
    with zip_file:
        for file in filePaths:
            zip_file.write(filepath + "/" + file)

    shutil.rmtree(filepath)

def main():
    processIssue(os.path.splitext(sys.argv[1])[0], sys.argv[1])
    

if __name__ == "__main__":
    main()
How can I get this to work after Mylar has processed each file?

Re: How to create custom post-processing script?

Posted: Wed Sep 23, 2020 2:26 pm
by evilhero
Processing AFTER each file has already been post-processed by Mylar would be the extra-scripts option - but in the complete path to your python script within the configuration option (Configuration / Post-Processing / Extra Script).

These are the variables that are sent to an external script that uses the Extra Script option (position / value):
0 = isn't used as it's usually the script name position
1 = original nzb name
2 = original download location
3 = final filename (after any renaming)
4 = final folder location
5 = seriesmetadata (this part is actually json, so you need to subset it)
  • name: the name of the series
  • comicyear: the year the series started
  • comicid: comicvine comicid
  • issueid: comicvine issueid
  • issueyear: the year of the issue
  • issue: the issue number
  • publisher: the publisher of the issue


By subsetting I mean you'd have to do something like :

Code: Select all

metadata_in = sys.argv[5]
metadata = metadata_in[0][1]
then make calls to it via metadata['name'], or metadata['publisher'], etc.

So with your script, you'd just have remove the main function and then set your initial call to:

Code: Select all

if __name__ == '__main__':
    processIssue(sys.argv[4], sys.argv[3]
Also,
if str.endswith(dirName + "/" + filename, "cbr"):
filepath = unrar(dirName + "/" + filename)
elif str.endswith(dirName + "/" + filename, "cbz"):
filepath = unzip(dirName + "/" + filename)
should be something like this as you can't use a filepath the way you're trying to later on but attaching it to zip/unrar call as well:
if str.endswith("cbr"):
filepath = os.path.join(dirName, filename)
elif str.endswith("cbz"):
filepath = os.path.join(dirName, filename)
Hope that helps!

Re: How to create custom post-processing script?

Posted: Wed Sep 23, 2020 4:24 pm
by CovertDad
Thanks so much!

Next question: is there an easy way to trigger a post processing, without having to go through marking an issue wanted and letting it download? In order to test my script?

Re: How to create custom post-processing script?

Posted: Wed Sep 23, 2020 4:27 pm
by evilhero
Not as a specific testing aspect, but you can do a manual post-processing on a directory with like one file in it (that you can just keep on moving back into it after each time it post-processes) and you'll be able to see your script run and the output (or if it's working of course).

Re: How to create custom post-processing script?

Posted: Tue Feb 08, 2022 3:02 pm
by JbaĆ¢l
Hi!
If I get it well, the extra-script will launch after each post-processed file.
Is there a way to launch a script after the MASS-ADD from the Folder Monitor ?