Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
- # Uncomment this line to print the original filename and new filename for testing.
- #print 'Original filename: ' + filename + ' - New filename: ' + new_filename
- # Last, but not least, rename the file. Comment this out during testing
- os.rename(ugly_folder + '\\' + filename, ugly_folder + '\\' + new_filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement