How to create custom post-processing script?

Information and Tutorials on features in Mylar and how to use it
Post Reply
CovertDad
Posts: 2
Joined: Mon Aug 17, 2020 1:34 pm

How to create custom post-processing script?

Post 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?
User avatar
evilhero
Site Admin
Posts: 2883
Joined: Sat Apr 20, 2013 3:43 pm
Contact:

Re: How to create custom post-processing script?

Post 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!
CovertDad
Posts: 2
Joined: Mon Aug 17, 2020 1:34 pm

Re: How to create custom post-processing script?

Post 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?
User avatar
evilhero
Site Admin
Posts: 2883
Joined: Sat Apr 20, 2013 3:43 pm
Contact:

Re: How to create custom post-processing script?

Post 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).
Jbaâl
Posts: 5
Joined: Mon Sep 13, 2021 10:56 pm

Re: How to create custom post-processing script?

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