Advertisement
Guest User

Justin

a guest
Jan 9th, 2010
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. # Python 2.6.4 | http://www.python.org/download/releases/2.6.4/
  2. import socket,sys,urllib2,os
  3. from urllib2 import Request, URLError, urlopen
  4.  
  5.  
  6. # Rename the file to match the folder (job) name
  7. # Get the Newzbin category
  8. cat = sys.argv[5]
  9. # Get the folder name again
  10. ugly_folder = sys.argv[1]
  11. # Get the job name
  12. ugly_jobname = sys.argv[3]
  13. # Set movie (and related) file extensions. These are filetypes we care about. Add more if you want those renamed, too
  14. movie_extensions = ['avi', 'mkv', 'wmv', 'avi', 'ts', 'img', 'iso', 'sub', 'idx', 'srt']
  15.  
  16.  
  17. def ext_count(file_list):
  18.     # Create the extensions dictionary
  19.     extensions = {}
  20.     # Loop through the file list to make a list of extensions and how many there are of each
  21.     for filename in file_list:
  22.         # Split filename by period
  23.         ext = filename.split('.')
  24.         # Get the file extension
  25.         ext = ext[-1]
  26.         # Check if the extension exists in the array list yet
  27.         if extensions.has_key(ext):
  28.             extensions[ext] = extensions[ext] + 1
  29.             # If so, add one to the existing entry
  30.         else:
  31.             # Otherwise, create the list entry
  32.             extensions[ext] = 1
  33.     return extensions
  34.  
  35.  
  36. # Apply this to movies only
  37. if cat == "movies":
  38.     # Make an empty dictionary for counting how many we've renamed of each extension
  39.     ext_tally = {}
  40.     # Make a list (downloaded_files) containing all of the downloaded files
  41.     downloaded_files = sorted(os.listdir(ugly_folder))
  42.     # Create a dictionary of extensions (the key) and the number of each (the value)
  43.     extensions = ext_count(downloaded_files)
  44.     # Loop through the list of downloaded files
  45.     for filename in downloaded_files:
  46.         # We don't know if this file is relevant to our interests
  47.         is_video = 0
  48.         # See if the ext matches one we care about. Loop through movie_extensions
  49.         for mov_ext in movie_extensions:
  50.             # See if the filename ends with a relevant extension
  51.             if filename.endswith('.' + mov_ext):
  52.                 # Flag the file as relevant
  53.                 is_video = 1
  54.                 # Stop checking (theoretically, it shouldn't have more than one extension)
  55.                 break
  56.         # If we determined that the file was relevant...
  57.         if is_video == 1:
  58.             # Start building the new filename
  59.             new_filename = ugly_jobname
  60.             # Get the extension of the file
  61.             file_extension = filename.split('.')[-1]
  62.             # Check to see if there was more than one of that extension
  63.             if extensions[file_extension] > 1:
  64.                 # If so, add " - CD" to the end
  65.                 new_filename = new_filename + ' - CD'
  66.                 # Check to see if we've already renamed one file with this extension
  67.                 if ext_tally.has_key(file_extension):
  68.                     # If so, add one to the count
  69.                     ext_tally[file_extension] = ext_tally[file_extension] + 1
  70.                 else:
  71.                     # If not, create a counter and set it to 1
  72.                     ext_tally[file_extension] = 1
  73.                 # Then append that number to the end of the filename
  74.                 new_filename = new_filename + str(ext_tally[file_extension])
  75.             # Finally, add the extension
  76.             new_filename = new_filename + '.' + file_extension
  77.             # Uncomment this line to print the original filename and new filename for testing.
  78.             #print 'Original filename: ' + filename + '    -    New filename: ' + new_filename
  79.             # Last, but not least, rename the file. Comment this out during testing
  80.             os.rename(ugly_folder + '\\' + filename, ugly_folder + '\\' + new_filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement