[Python] Simple movie renamer
Posted: October 12th, 2015, 8:17 am
I recently migrated to a qnap nas server, and my old renaming script only worked on windows. So I learned a little python to create the movie renaming script I wanted. I don't like to use couch potato because I like to pick my own releases. In my case, all of the movie nzbs have a format of Movie.Name.2015.HD.x264.Group. If you have an nzb downloading that doesn't match this pattern, you can rename the title yourself while it's in the queue. So, what it does is find the largest file and any file extensions in the whitelist (subtitles). Then it renames them by finding the 4 digit year, cuts off everything to the right, replaces the periods, and ends up with Movie Name (2014). Then it moves it to a folder depending on the category name. Then removes the remaining files and folders. Also, it will trigger a plex library scan. I'm sure it's not perfect, and I'm not an expert with python, but it works for me. My movie library doesn't have separate folders like most people do, but this should be easily modified for different needs. Hopefully this helps someone looking for simple movie renaming.
Code: Select all
#!/usr/bin/python
import sys, os, re, urllib2, shutil
full_path = sys.argv[1:2][0]
job_name = sys.argv[3:4][0]
category = sys.argv[5:6][0]
#full_path = "/share/Download/sabnzbd/complete/Movie.Name.2015.BDRip.x264-Group"
#job_name = "Movie.Name.2015.BDRip.x264-Group"
#category = "movie"
ext_whitelist = ["sub","idx","srt"]
plex_host = "localhost:32400"
destination_folder = "/share/Multimedia/Videos/Movies"
plex_library = 1
if category == "kids movie":
destination_folder = "/share/Multimedia/Videos/Kids"
plex_library = 3
# Begin Find Largest File
largest_file = ("", -1, "")
subtitles = []
def search(dir):
global largest_file
for item in os.listdir(dir):
item = dir + "/" + item
if os.path.isdir(item):
search(item)
else:
itemsize = os.path.getsize(item)
itemext = os.path.splitext(item)[1][1:]
if itemext in ext_whitelist:
subtitles.append(item)
if itemsize > largest_file[1]:
largest_file = (item, itemsize, itemext)
search(full_path)
if largest_file[1] == -1:
sys.exit()
# End Find Largest File
# Determine new file name
find_year = re.search("\d\d\d\d", job_name).start()
movie_name = job_name[0:find_year-1].replace("."," ")
movie_year = job_name[find_year:find_year+4]
new_name = movie_name + " (" + movie_year + ")"
new_file_name = new_name + "." + largest_file[2]
# Rename/move largest file
os.rename(largest_file[0], destination_folder + "/" + new_file_name)
# Rename/move subtitle files
for s in subtitles:
os.rename(s, destination_folder + "/" + new_name + "." + os.path.splitext(s)[1][1:])
# Delete remaining files
shutil.rmtree(full_path)
# Plex Library Scan
urllib2.urlopen("http://" + plex_host + "/library/sections/" + `plex_library` + "/refresh").read()