Help writing a copy script

Share your scripts or request scripts with specific features.
Forum rules
Please keep the forum clean - one topic per script. Questions not related to a specific script should be posted in Support forum.
Post Reply
Rumik
Posts: 19
Joined: 04 Feb 2014, 19:28

Help writing a copy script

Post by Rumik » 04 Feb 2014, 19:31

Hi there,

I've been using sab for a while, and I had a coder friend write a script for me to copy files that matched certain categories into a given folder, so I essentially ended up with 2 copies of the same file in 2 different places. I'd like to do the same thing with NZBGet but I'm having trouble adapting the script, as I'm no coder. Can someone help?


Here's what I've got so far but the script fails without any useful info:


#!/usr/bin/env python
##############################################################################
### NZBGET POST-PROCESSING SCRIPT ###

# Copies comic and music.

### NZBGET POST-PROCESSING SCRIPT ###
##############################################################################

import sys,os,distutils.dir_util

# Args as found in docs at: http://wiki.sabnzbd.org/user-scripts
# 1 The final directory of the job (full path)
origin = "$NZBPP_DIRECTORY" #sys.argv[1]
# 2 The original name of the NZB file
# 3 Clean version of the job name (no path info and ".nzb" removed)
# 4 Indexer's report number (if supported)
# 5 User-defined category
category = "$NZBPP_CATEGORY" #sys.argv[5]
# 6 Group that the NZB was posted in e.g. alt.binaries.x
# 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+21

destination = ""
if category.lower() == "Comics":
destination = "C:\Users\Ryan\Dropbox\Comics"
elif category.lower() == "Music":
destination = "C:\Users\Ryan\Music\iTunes\iTunes Media\Automatically Add to iTunes"
else:
print("Origin: " + origin)
print("Category: " + category)
sys.exit("Script failed!")

distutils.dir_util.copy_tree(origin, destination)



Thanks!

minimeh
Posts: 33
Joined: 16 Aug 2013, 09:54

Re: Help writing a copy script

Post by minimeh » 05 Feb 2014, 10:24

Rumik wrote:I've been using sab for a while, and I had a coder friend write a script for me to copy files that matched certain categories into a given folder, so I essentially ended up with 2 copies of the same file in 2 different places. I'd like to do the same thing with NZBGet but I'm having trouble adapting the script, as I'm no coder. Can someone help?
Your script has several problems. Just for grins, I cranked out a version that may work. I say "may" because I haven't tested it, but it should get you going in the right direction.

Code: Select all

#!/usr/bin/env python
##############################################################################
### NZBGET POST-PROCESSING SCRIPT                                          ###

# Copies comic and music.

### NZBGET POST-PROCESSING SCRIPT                                          ###
##############################################################################

import sys
import os
import distutils.dir_util
import string

# Exit codes used by NZBGet
POSTPROCESS_SUCCESS=93
POSTPROCESS_NONE=95
POSTPROCESS_ERROR=94

# Check if the script is called from nzbget 11.0 or later
if not 'NZBOP_SCRIPTDIR' in os.environ:
    print('*** NZBGet post-processing script ***')
    print('This script is supposed to be called from nzbget (11.0 or later).')
    sys.exit(POSTPROCESS_ERROR)

# Check par and unpack status for errors
if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ['NZBPP_PARSTATUS'] == '4' or os.environ['NZBPP_UNPACKSTATUS'] == '1':
    print('[WARNING] Download of "%s" has failed, exiting' % (os.environ['NZBPP_NZBNAME']))
    sys.exit(POSTPROCESS_NONE)

# Args as found in docs at: http://wiki.sabnzbd.org/user-scripts
# 1 The final directory of the job (full path)
# origin = "$NZBPP_DIRECTORY" #sys.argv[1]
# Check if we have the final directory
origin = os.environ.get('NZBPP_FINALDIR', '').lower()
if len(origin) == 0:
    origin = os.environ.get('NZBPP_DIRECTORY', '').lower()

# 2 The original name of the NZB file
# 3 Clean version of the job name (no path info and ".nzb" removed)
# 4 Indexer's report number (if supported)
# 5 User-defined category
# category = "$NZBPP_CATEGORY" #sys.argv[5]
category = os.environ.get('NZBPP_CATEGORY', '').lower()
# 6 Group that the NZB was posted in e.g. alt.binaries.x
# 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+21

destination = ""
if category == "comics":
    destination = "C:\\Users\\Ryan\\Dropbox\\Comics"
elif category == "music":
    destination = "C:\\Users\\Ryan\\Music\\iTunes\\iTunes Media\\Automatically Add to iTunes"
else:
    print("[INFO] Origin: " + origin)
    print("[INFO] Category: " + category)
    print("[ERROR] Script failed!")
    sys.exit(POSTPROCESS_ERROR)

try:
    distutils.dir_util.copy_tree(origin, destination)
except Exception as e:
    print('[ERROR] Failed copying tree.')
    print('[ERROR] %s' % e)
    sys.exit(POSTPROCESS_ERROR)

sys.exit(POSTPROCESS_SUCCESS)
Some of the problems that I found were

Code: Select all

origin = "$NZBPP_DIRECTORY" #sys.argv[1]
category = "$NZBPP_CATEGORY" #sys.argv[5]
These statements do not retrieve the environment variables but rather set the variables to the environment variable names with a dollar sign prepended.

Code: Select all

if category.lower() == "Comics":
    destination = "C:\Users\Ryan\Dropbox\Comics"
elif category.lower() == "Music":
    destination = "C:\Users\Ryan\Music\iTunes\iTunes Media\Automatically Add to iTunes"
You're comparing lowered case strings to mixed case strings, so never going to match. The string escape character "\" in the path names need to be escaped themselves or they are misinterpreted by python. Of course the formatting (critical in python) is wrong, but I'm assuming that is only in the post and due to not pasting the code into a code section.

Code: Select all

sys.exit("Script failed!")
NZBget defines specific exit codes POSTPROCESS_SUCCESS=93, POSTPROCESS_NONE=95, and POSTPROCESS_ERROR=94. Calling sys.exit() with a string returns 1, which is a generic code that NZBget doesn't represent well in its logs and messages. Instead, it is better to simply print the message desired, then sys.exit with the appropriate error code. If the printed message begins with "[ERROR]", it will be handled appropriately in the logs and messages.

I also enclosed the call to distutils.dir_util.copy_tree() in a try/except block and process the exception so that you'll get a message indicating the problem encountered.

I made sure that all failures result in an error message, and if all goes well, a successful status is returned to NZBget.

Rumik
Posts: 19
Joined: 04 Feb 2014, 19:28

Re: Help writing a copy script

Post by Rumik » 05 Feb 2014, 14:37

Wow, thanks so much, I really appreciate it :) I'll give it a whirl tonight!

Thanks again :)

Rumik
Posts: 19
Joined: 04 Feb 2014, 19:28

Re: Help writing a copy script

Post by Rumik » 06 Feb 2014, 08:37

Yep, that worked brilliantly. Thanks again!

tomcat
Posts: 1
Joined: 02 May 2015, 10:06

Re: Help writing a copy script

Post by tomcat » 02 May 2015, 10:13

Hi im trying to use this script but for some unknown reason it failes :/
Im using nzbget 14.2 on ubuntu
blow the script with all changes i made.
Hope someone can help me :)

Code: Select all

#!/usr/bin/env python
##############################################################################
### NZBGET POST-PROCESSING SCRIPT                                          ###

# Copies comic and music.

### NZBGET POST-PROCESSING SCRIPT                                          ###
##############################################################################

import sys
import os
import distutils.dir_util
import string

# Exit codes used by NZBGet
POSTPROCESS_SUCCESS=93
POSTPROCESS_NONE=95
POSTPROCESS_ERROR=94

# Check if the script is called from nzbget 11.0 or later
if not 'NZBOP_SCRIPTDIR' in os.environ:
    print('*** NZBGet post-processing script ***')
    print('This script is supposed to be called from nzbget (11.0 or later).')
    sys.exit(POSTPROCESS_ERROR)

# Check par and unpack status for errors
if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ['NZBPP_PARSTATUS'] == '4' or os.environ['NZBPP_UNPACKSTATUS'] == '1':
    print('[WARNING] Download of "%s" has failed, exiting' % (os.environ['NZBPP_NZBNAME']))
    sys.exit(POSTPROCESS_NONE)

# Args as found in docs at: http://wiki.sabnzbd.org/user-scripts
# 1 The final directory of the job (full path)
# origin = "$NZBPP_DIRECTORY" #sys.argv[1]
# Check if we have the final directory
origin = os.environ.get('NZBPP_FINALDIR', '').lower()
if len(origin) == 0:
    origin = os.environ.get('NZBPP_DIRECTORY', '').lower()

# 2 The original name of the NZB file
# 3 Clean version of the job name (no path info and ".nzb#!/usr/bin/env python
##############################################################################
### NZBGET POST-PROCESSING SCRIPT                                          ###

# Copies comic and music.

### NZBGET POST-PROCESSING SCRIPT                                          ###
##############################################################################

import sys
import os
import distutils.dir_util
import string

# Exit codes used by NZBGet
POSTPROCESS_SUCCESS=93
POSTPROCESS_NONE=95
POSTPROCESS_ERROR=94

# Check if the script is called from nzbget 11.0 or later
if not 'NZBOP_SCRIPTDIR' in os.environ:
    print('*** NZBGet post-processing script ***')
    print('This script is supposed to be called from nzbget (11.0 or later).')
    sys.exit(POSTPROCESS_ERROR)

# Check par and unpack status for errors
if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ['NZBPP_PARSTATUS'] == '4' or os.environ['NZBPP_UNPACKSTATUS'] == '1':
    print('[WARNING] Download of "%s" has failed, exiting' % (os.environ['NZBPP_NZBNAME']))
    sys.exit(POSTPROCESS_NONE)

# Args as found in docs at: http://wiki.sabnzbd.org/user-scripts
# 1 The final directory of the job (full path)
# origin = "$NZBPP_DIRECTORY" #sys.argv[1]
# Check if we have the final directory
origin = os.environ.get('NZBPP_FINALDIR', '').lower()
if len(origin) == 0:
    origin = os.environ.get('NZBPP_DIRECTORY', '').lower()

# 2 The original name of the NZB file
# 3 Clean version of the job name (no path info and ".nzb" removed)
# 4 Indexer's report number (if supported)
# 5 User-defined category
# category = "$NZBPP_CATEGORY" #sys.argv[5]
category = os.environ.get('NZBPP_CATEGORY', '').lower()
# 6 Group that the NZB was posted in e.g. alt.binaries.x
# 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+21

destination = ""
if category == "Filme-DTS-HD":
    destination = "/media/nas/fileserver/Filme"
elif category == "Musik":
    destination = "/media/nas/fileserver/Musik"
else:
    print("[INFO] Origin: " + origin)
    print("[INFO] Category: " + category)
    print("[ERROR] Script failed!")
    sys.exit(POSTPROCESS_ERROR)

try:
    distutils.dir_util.copy_tree(origin, destination)
except Exception as e:
    print('[ERROR] Failed copying tree.')
    print('[ERROR] %s' % e)
    sys.exit(POSTPROCESS_ERROR)

sys.exit(POSTPROCESS_SUCCESS)" removed)
# 4 Indexer's report number (if supported)
# 5 User-defined category
# category = "$NZBPP_CATEGORY" #sys.argv[5]
category = os.environ.get('NZBPP_CATEGORY', '').lower()
# 6 Group that the NZB was posted in e.g. alt.binaries.x
# 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+21

destination = ""
if category == "Filme-DTS-HD":
    destination = "/media/nas/fileserver/Filme"
elif category == "Musik":
    destination = "/media/nas/fileserver/Musik"
else:
    print("[INFO] Origin: " + origin)
    print("[INFO] Category: " + category)
    print("[ERROR] Script failed!")
    sys.exit(POSTPROCESS_ERROR)

try:
    distutils.dir_util.copy_tree(origin, destination)
except Exception as e:
    print('[ERROR] Failed copying tree.')
    print('[ERROR] %s' % e)
    sys.exit(POSTPROCESS_ERROR)

sys.exit(POSTPROCESS_SUCCESS)

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests