Help with writing some python scripts

Get help with all aspects of SABnzbd
Forum rules
Help us help you:
  • Are you using the latest stable version of SABnzbd? Downloads page.
  • Tell us what system you run SABnzbd on.
  • Adhere to the forum rules.
  • Do you experience problems during downloading?
    Check your connection in Status and Interface settings window.
    Use Test Server in Config > Servers.
    We will probably ask you to do a test using only basic settings.
  • Do you experience problems during repair or unpacking?
    Enable +Debug logging in the Status and Interface settings window and share the relevant parts of the log here using [ code ] sections.
Post Reply
Rumik
Newbie
Newbie
Posts: 13
Joined: January 30th, 2012, 3:14 am

Help with writing some python scripts

Post by Rumik »

Hi guys,

I'm moving my SAB installation over to a new mac mini running Mountain Lion and I could really use a hand writing some replacement scripts, as my old .bat scripts obviously do not work with OSX, however I don't know python. Could some kind soul help me write some new ones?

Here they are:


Audio

@echo off
xcopy %1 "S:\Media\Music\iTunes\iTunes Media\Automatically Add to iTunes\"%3\ /s/e

the new file structure is: /Volumes/Share-1/Media/Music/iTunes/iTunes Media/Automatically Add to iTunes/



Comics

@echo off
xcopy %1 C:\Users\Ryan\Dropbox\Downloads\%3\ /s/e

the new file structure is: Users/Ryan/Dropbox/Comics/


Thanks, I really appreciate the help.

Ryan
User avatar
sander
Release Testers
Release Testers
Posts: 9062
Joined: January 22nd, 2008, 2:22 pm

Re: Help with writing some python scripts

Post by sander »

Python is great, but you don't need it for these oneliners: just a "cp --recursive ..." (or "mv") should do. And replayce %1 with $1.

My first guess:

cp --recursive $1 "/Volumes/Share-1/Media/Music/iTunes/iTunes Media/Automatically Add to iTunes/"$3

As always: first issue the command from the command line. Experiment with it. If it works, put it in a .sh file. Test it. If it works, run the script from SABnzbd.
Rumik
Newbie
Newbie
Posts: 13
Joined: January 30th, 2012, 3:14 am

Re: Help with writing some python scripts

Post by Rumik »

Thanks for the advice, I'm grateful! Unfortunately I didn't really understand what you jus said lol but I will try the script you suggested :)
Rumik
Newbie
Newbie
Posts: 13
Joined: January 30th, 2012, 3:14 am

Re: Help with writing some python scripts

Post by Rumik »

I've come up with the following script, but it's still not working. Could someone tell me what I'm doing wrong? I'm pulling my hair out here!
import sys,os,distutils.dir_util

origin = sys.argv[1]
destination = ""

if sys.argv[2].lower() == "comics":
destination = "/Users/Ryan/Dropbox/Comics"
elif sys.argv[2].lower() == "music":
destination = "/Volumes/Share/Media/Music/iTunes/iTunes Media/Automatically Add to iTunes"
else:
sys.exit("Usage: larry.py sourcepath [comics | music]")

distutils.dir_util.copy_tree(origin, destination)

and the error I'm getting is:
Exit(1) Usage: larry.py sourcepath [comics | music]
So, I moved on to shell, and came up with two separate scripts:
#!/bin/bash
mv $1 /Volumes/Share/Media/Music/iTunes/iTunes Media/Automatically Add to iTunes/
and
#!/bin/bash
cp $1 /Users/Ryan/Comics/
But those both simply return the error:
Cannot run script.
Can anyone help me figure this out?

Thanks!
User avatar
shypike
Administrator
Administrator
Posts: 19774
Joined: January 18th, 2008, 12:49 pm

Re: Help with writing some python scripts

Post by shypike »

Does /bin/bash exist on your system?
Your Python script doesn't have a first "magic line", something like:
#!/usr/bin/python
Also your Python script doesn't use indentation for the "then" and "else" parts of "if" statements.
Rumik
Newbie
Newbie
Posts: 13
Joined: January 30th, 2012, 3:14 am

Re: Help with writing some python scripts

Post by Rumik »

A friend managed to help me figure it out :)

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 = 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 = 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 = "/Users/Ryan/Dropbox/Comics"
elif category.lower() == "music":
destination = "/Volumes/Share/Media/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)
Rumik
Newbie
Newbie
Posts: 13
Joined: January 30th, 2012, 3:14 am

Re: Help with writing some python scripts

Post by Rumik »

Ah, unfortunately while the comic portion of the script is working, the audio one is not. I'm getting the "script failed!" Error... I don't want to keep bothering my friend, can anyone help? I thought it might be the spaces in the patch (iTunes media and automatically add to iTunes) so I changed them to 'iTunes media', etc but it hasn't made any difference.

Anyone have any ideas?
Rumik
Newbie
Newbie
Posts: 13
Joined: January 30th, 2012, 3:14 am

Re: Help with writing some python scripts

Post by Rumik »

No worries, I fixed it :) wrong category!
Post Reply