Help with a script function

Come up with a useful post-processing script? Share it here!
FatalOne
Newbie
Newbie
Posts: 14
Joined: June 30th, 2013, 2:15 pm

Help with a script function

Post by FatalOne »

Hi all,

Attempting to write a python script that will rename and move movies. There are several of these around the forum, though they all lack one tiny function. Hoping somebody can point me in the right direction

My drive structure is:
E:\Videos\Movies\M\Movie name, The (year)\ The Movie Name.avi

Where the folder "M" is a folder named after the first letter of the movie title.

I have 27 sub directories in the Movies Directory, these are alphabetical directories, except for the folder labled " # " which is where numerical or symbol movie titles are stored.

So, for the sake of rambling, I need to be able to grab the first letter of the movie title, ignoring "the" and move the renamed movie folder to the correct alphabetical subdirectory.

Is this possible?

Thanks in advance!
FatalOne
Newbie
Newbie
Posts: 14
Joined: June 30th, 2013, 2:15 pm

Re: Help with a script function

Post by FatalOne »

So I've created a portion of the script that I need, haven't tested this, but what I'm looking to do is move a directory and its contents to another directory. Although, this still lacks the ability to grab first letter of movie name and make the destination directory be a folder of that letter. My library structure Is alphabetical.

Anybody able to point me in the right direction?

ty again

Code: Select all

#!/usr/bin/python
import shutil
 srcDir="D:\ServerFolders\SABnzb Complete\movie"
 dstDir="E:\ServerFolders\Videos\Movies"
  def move(srcDir, dstDir): 
    shutil.move(srcDir, dstDir)
User avatar
sander
Release Testers
Release Testers
Posts: 9264
Joined: January 22nd, 2008, 2:22 pm

Re: Help with a script function

Post by sander »

Can you write down in pseudo-code, or the steps in just plain English, what you want to achieve?

My first steps:

start with srcDir
split the srcDir-name on "\", find latest string, remove "the" (and "The") and space from the beginning
then get the first letter, turn to capital. if letter is not a letter, then turn into "#".
...
FatalOne
Newbie
Newbie
Posts: 14
Joined: June 30th, 2013, 2:15 pm

Re: Help with a script function

Post by FatalOne »

Sanders,

First off, thank you in advance for your help.

So I have catagories setup in SABnzbd, SAB will be handling the downloading, unpacking, directory clean up, as well as renaming, once this is complete I will have a folder in my "movies" catagory directory with a name in the format of "The Movie (date)"

At this point, all i want to do is move that folder to my archives, but the catch is my archives have an alphabetical directory structure. So somehow, i need a script to identify the first letter of the Movie name, ignoring "the" or "The", then move that movie folder to the appropriate alphabetical directory in my movie archives.

I've never written a python script before. I believe i can stumble my way through moving a directory, though dealing with my alphabetical directory structure is beyond me.

again,

Thanks for your time
User avatar
sander
Release Testers
Release Testers
Posts: 9264
Joined: January 22nd, 2008, 2:22 pm

Re: Help with a script function

Post by sander »

FatalOne wrote: At this point, all i want to do is move that folder to my archives, but the catch is my archives have an alphabetical directory structure. So somehow, i need a script to identify the first letter of the Movie name, ignoring "the" or "The", then move that movie folder to the appropriate alphabetical directory in my movie archives.
Exactly. So my question to you was: can you de-compose those steps in mini-steps (written in techno-English, not a programming language), of which I gave you the first ones.
FatalOne wrote: I've never written a python script before.
Because you want to write in python, I want to help you. :-)
FatalOne
Newbie
Newbie
Posts: 14
Joined: June 30th, 2013, 2:15 pm

Re: Help with a script function

Post by FatalOne »

Sanders

start with srcDir
split the srcDir-name on "\", find latest string, remove "the" (and "The") and space from the beginning
then get the first letter, turn to capital. if letter is not a letter, then turn into "#".
set dstVar = to first letter
set dstDir = E:\ServerFolders\Videos\Movies\(dstVar)
move contents of current directory to dstDir

This seems to be the gist of the script I need, I'm sure there are intermediate steps that I skipped over. Though i have so many questions...lol

This split function, how does that work? How do you specify where to split, and what happens with the info to the left of the split?

Shutil module, is this the correct choice for moving the directory?

Is moving the directory to the alphabetical structure the way to handle this, or would it be better to create a new folder named the first letter, containing the movie directory and over write the existing alphabetical folder in the destination. This would cause the dstDir to be static vs being a variable.

thank you
User avatar
sander
Release Testers
Release Testers
Posts: 9264
Joined: January 22nd, 2008, 2:22 pm

Re: Help with a script function

Post by sander »

Is the output below correct? Is that what you want?

Code: Select all

D:\ServerFolders\SABnzb Complete\The Greatest Movie (2032)
The Greatest Movie (2032)
THE GREATEST MOVIE (2032)
GREATEST MOVIE (2032)
G
Yes
First character is ... G

D:\ServerFolders\SABnzb Complete\2012 The Greatest Movie (2032)
2012 The Greatest Movie (2032)
2012 THE GREATEST MOVIE (2032)
2012 GREATEST MOVIE (2032)
2
First character is ... #

D:\ServerFolders\SABnzb Complete\The Greatest Movie (2032)\
The Greatest Movie (2032)
THE GREATEST MOVIE (2032)
GREATEST MOVIE (2032)
G
Yes
First character is ... G
FatalOne
Newbie
Newbie
Posts: 14
Joined: June 30th, 2013, 2:15 pm

Re: Help with a script function

Post by FatalOne »

Sanders,

Yes, the output looks correct, though, i'm not sure about using the file name rather then the folder name. Think I would prefer folder name, unless there is a reason for using the file name that I'm missing.

Just to clarify, I'm not looking to actually do any renaming to the movies folder or file name, just looking to grab the first letter so i can move the folder to the correct alphabetical directory.

Thank you for your time
User avatar
sander
Release Testers
Release Testers
Posts: 9264
Joined: January 22nd, 2008, 2:22 pm

Re: Help with a script function

Post by sander »

The above is all about finding the first letter and thus the correct alphabetical directory.

Python code is below. NB: the code is very redundant. You can probably reduce it to five lines or so

Are you able to use this in your script?

Code: Select all

def GiveMeFirstChatacter(srcDir):
	splitchar = '\\'

	print "\n",srcDir
	last = srcDir.split(splitchar)[-1]
	if last == '':
		last = srcDir.split(splitchar)[-2]

	print last

	last = last.upper()
	print last

	last = last.replace("THE ","")
	print last

	firstchar = last[0]
	print firstchar

	if firstchar >= 'A' and firstchar <= 'Z' :
		print "Yes"
	else:
		firstchar = '#'

	rc = "First character is ... " + firstchar
	return rc


print GiveMeFirstChatacter('D:\\ServerFolders\\SABnzb Complete\\The Greatest Movie (2032)')
print GiveMeFirstChatacter('D:\\ServerFolders\\SABnzb Complete\\2012 The Greatest Movie (2032)')
print GiveMeFirstChatacter('D:\\ServerFolders\\SABnzb Complete\\The Greatest Movie (2032)\\')
FatalOne
Newbie
Newbie
Posts: 14
Joined: June 30th, 2013, 2:15 pm

Re: Help with a script function

Post by FatalOne »

Sanders,

Ok, so after messing with the code for a couple days, I have yet to get it to work properly, it could very well be something I'm doing incorrectly.

So I have gotten the bit of code that actually moves the directories to function correctly, though I was having trouble getting it to move the contents in the "movies" directory, it was wanting to move the "movies" directory itself, and from what I've read shutil.move doesn't take wildcards. So I had to get a lil creative to make it work.

Code: Select all

#!/usr/bin/python
import distutils.dir_util
import shutil
import os

srcDir="D:\ServerFolders\SABnzb Complete\movies"
dstDir="E:\ServerFolders\Videos\Movies"

distutils.dir_util.copy_tree(srcDir, dstDir)

shutil.rmtree(srcDir)
os.mkdir(srcDir)
This bit of code works, been tested, and moves contents of "D:\ServerFolders\SABnzb Complete\movies" to "E:\ServerFolders\Videos\Movies"

But this is ignoring the alphabetical structure.

I tried incorporating your code with mine and came up with:

Code: Select all


#!/usr/bin/python
import shutil
import distutils.dir_util
import os

srcDir="D:\ServerFolders\SABnzb Complete\movies"



def GiveMeFirstChatacter(srcDir):
     splitchar = '\\'

     print "\n",srcDir
     last = srcDir.split(splitchar)[-1]
     if last == '':
       last = srcDir.split(splitchar)[-2]

     print last

     last = last.upper()
     print last

     last = last.replace("THE ","")
     print last

     firstchar = last[0]
     print firstchar

     if firstchar >= 'A' and firstchar <= 'Z' :
       print "Yes"
     else:
       firstchar = '#'

     rc = "First character is ... " + firstchar
     return rc


   print GiveMeFirstChatacter('D:\\ServerFolders\\SABnzb Complete\\The Greatest Movie      (2032)')
   print GiveMeFirstChatacter('D:\\ServerFolders\\SABnzb Complete\\2012 The Greatest Movie (2032)')
   print GiveMeFirstChatacter('D:\\ServerFolders\\SABnzb Complete\\The Greatest Movie (2032)\\')

dstDir="E:\ServerFolders\Videos\Movies\"+firstchar

distutils.dir_util.copy_tree(srcDir, dstDir)

shutil.rmtree(srcDir)
os.mkdir(srcDir)
and I get nothing.

I'm not even 100% that I merged these together correctly.

I'm concerned that the manipulation you are doing with the srcDir may be doing it on the "movies" directory rather then the directory within "movies"

This was the issue I was running into while trying to move things. Then again, I'm totally new to this, so I may be way off the mark.

again, thank you for your help!
User avatar
sander
Release Testers
Release Testers
Posts: 9264
Joined: January 22nd, 2008, 2:22 pm

Re: Help with a script function

Post by sander »

When/how do you want to run the script? As a SABnzbd post processing script, or as a a batch script to clean up your "D:\ServerFolders\SABnzb Complete\movie\" directory?
FatalOne
Newbie
Newbie
Posts: 14
Joined: June 30th, 2013, 2:15 pm

Re: Help with a script function

Post by FatalOne »

As a SABnzbd pp script... After each movie category download
User avatar
sander
Release Testers
Release Testers
Posts: 9264
Joined: January 22nd, 2008, 2:22 pm

Re: Help with a script function

Post by sander »

FatalOne wrote:As a SABnzbd pp script... After each movie category download
OK. That means you have to use the parameters that SABnzbd passes to the script. With python, you can catch those parameters with sys.argv[1] etc:

Code: Select all

import sys

print sys.argv[1]
print sys.argv[2]
Reading http://wiki.sabnzbd.org/user-scripts , I think only you only need the first parameter.


Question: have you been able to run any (existing) PP script at all? That would be the first step.
FatalOne
Newbie
Newbie
Posts: 14
Joined: June 30th, 2013, 2:15 pm

Re: Help with a script function

Post by FatalOne »

Ah yes, I read about the 5 or so parameters that are passed from SABnzbd, im guessing the nzb clean name or path is what we want?

Yes I have ran python pp script I run a custom fork of sickbeard from source, so python is Installed with environment variables path set to point to python install...
User avatar
sander
Release Testers
Release Testers
Posts: 9264
Joined: January 22nd, 2008, 2:22 pm

Re: Help with a script function

Post by sander »

Shall we go to the very basics? Create a python script that moves the download to a destination directory?

The pp script below moves the contents of download directory to another location with directory name 'jobname'. Do you understand the code? Does it work for you (after changing the destinationdirectory to something that works on your machine)?

Code: Select all

import sys, shutil

sourcedirectory = sys.argv[1]	### The final directory of the job (full path)
jobname = sys.argv[3]
print sourcedirectory
print jobname

destinationdirectory = '/home/sander/MyDownloads/' + jobname 
print destinationdirectory

shutil.move(sourcedirectory, destinationdirectory)

exit(0)
If the above works, I can give you the script that moves the the structure you want (first letter, etc).
Post Reply