Page 1 of 1

Help with writing some python scripts

Posted: December 24th, 2012, 11:19 am
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

Re: Help with writing some python scripts

Posted: December 24th, 2012, 3:16 pm
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.

Re: Help with writing some python scripts

Posted: December 24th, 2012, 3:29 pm
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 :)

Re: Help with writing some python scripts

Posted: January 13th, 2013, 1:39 pm
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!

Re: Help with writing some python scripts

Posted: January 13th, 2013, 2:28 pm
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.

Re: Help with writing some python scripts

Posted: January 13th, 2013, 3:16 pm
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)

Re: Help with writing some python scripts

Posted: January 13th, 2013, 5:30 pm
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?

Re: Help with writing some python scripts

Posted: January 13th, 2013, 5:42 pm
by Rumik
No worries, I fixed it :) wrong category!