Here's another script for renaming movie files to match the folder (well, job) name. This time, in Python.
I made this one partially because I wanted to learn some python, partially because I wanted to integrate this script into the one that updates xbmc and sends a prowl notification, and partially because I wanted something more readable/configurable than a batch file.
I've never actually used python before, so it's probably not the prettiest script you'll see, code-wise, and there may be much simpler ways of doing things, but in the handful of tests I ran, it works great.
It works perfectly (thus far) on Python 2.6.4
I *think* it should work on any platform you can run python on, but I have no way of testing this. The only thing I can think of that might cause problems is the direction of the slashes in the last line. They're windows-style, since that's what I use, but I don't know how much other OS'es care. Everything else should be OS-independent.
As I haven't done extensive testing, use at your own risk. I can't imagine it would cause any damage to anything, but I'm always nervous about stuff like that.
The whole thing is heavily commented (pretty much every line, lol). This is mostly for my sake, so I know what the heck is going on if I ever have to look at the code again, but if you look through it and find something I could have done better, please let me know, and feel free to post corrections/changes (an explanation of why would be great for those of us that aren't great with python yet).
Either way, feedback is definitely appreciated.
Get it here:
http://pastebin.com/f1346653b
Or just copy/paste:
Code: Select all
# Python 2.6.4 | http://www.python.org/download/releases/2.6.4/
import socket,sys,urllib2,os
from urllib2 import Request, URLError, urlopen
# Rename the file to match the folder (job) name
# Get the Newzbin category
cat = sys.argv[5]
# Get the folder name again
ugly_folder = sys.argv[1]
# Get the job name
ugly_jobname = sys.argv[3]
# Set movie (and related) file extensions. These are filetypes we care about. Add more if you want those renamed, too
movie_extensions = ['avi', 'mkv', 'wmv', 'avi', 'ts', 'img', 'iso', 'sub', 'idx', 'srt']
def ext_count(file_list):
# Create the extensions dictionary
extensions = {}
# Loop through the file list to make a list of extensions and how many there are of each
for filename in file_list:
# Split filename by period
ext = filename.split('.')
# Get the file extension
ext = ext[-1]
# Check if the extension exists in the array list yet
if extensions.has_key(ext):
extensions[ext] = extensions[ext] + 1
# If so, add one to the existing entry
else:
# Otherwise, create the list entry
extensions[ext] = 1
return extensions
# Apply this to movies only
if cat == "movies":
# Make an empty dictionary for counting how many we've renamed of each extension
ext_tally = {}
# Make a list (downloaded_files) containing all of the downloaded files
downloaded_files = sorted(os.listdir(ugly_folder))
# Create a dictionary of extensions (the key) and the number of each (the value)
extensions = ext_count(downloaded_files)
# Loop through the list of downloaded files
for filename in downloaded_files:
# We don't know if this file is relevant to our interests
is_video = 0
# See if the ext matches one we care about. Loop through movie_extensions
for mov_ext in movie_extensions:
# See if the filename ends with a relevant extension
if filename.endswith('.' + mov_ext):
# Flag the file as relevant
is_video = 1
# Stop checking (theoretically, it shouldn't have more than one extension)
break
# If we determined that the file was relevant...
if is_video == 1:
# Start building the new filename
new_filename = ugly_jobname
# Get the extension of the file
file_extension = filename.split('.')[-1]
# Check to see if there was more than one of that extension
if extensions[file_extension] > 1:
# If so, add " - CD" to the end
new_filename = new_filename + ' - CD'
# Check to see if we've already renamed one file with this extension
if ext_tally.has_key(file_extension):
# If so, add one to the count
ext_tally[file_extension] = ext_tally[file_extension] + 1
else:
# If not, create a counter and set it to 1
ext_tally[file_extension] = 1
# Then append that number to the end of the filename
new_filename = new_filename + str(ext_tally[file_extension])
# Finally, add the extension
new_filename = new_filename + '.' + file_extension
# See if the new filename and the old filename match
if filename == new_filename:
# If so, end this iteration without renaming, and say so:
print "Filenames are the same. Not renaming"
continue
# Uncomment this line to print the original filename and new filename
print 'Renaming ' + filename + ' to ' + new_filename
# Last, but not least, rename the file
os.rename(ugly_folder + '\\' + filename, ugly_folder + '\\' + new_filename)
/edit: Added a couple of lines to keep it from trying to rename a file if the current filename and new filename are the same. Haven't tested this yet, but the code is pretty simple.