Created
February 9, 2013 00:29
-
-
Save jacobhak/4743181 to your computer and use it in GitHub Desktop.
A simple python script which categorizes and copies a file from the first command line argument. Excellent to use with xbmc or Plex for some automation. Written by Walter Nordström, [email protected]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=latin-1 | |
import sys | |
import shutil | |
import os | |
import re | |
# Paths | |
drive = '/Path/To/Drive/' | |
movies = 'MovieDirOnDrive/' | |
tv = 'TVDirOnDrive/' | |
fileExtensions = ['.mkv'] | |
# Functions | |
def findFiles(path): | |
if os.path.isdir(path): | |
dirList = os.listdir(path) | |
for p in dirList: | |
findFiles(path+'/'+p) | |
else: | |
moveFile(path) | |
def moveFile(path): | |
if isTv (path): | |
showName = getShowName(path) | |
showPath = drive + tv + showName + '/' | |
if not os.path.isdir(showPath): | |
os.makedirs(showPath) | |
if 'american dad' in showName.lower(): | |
showPath = showPath + incrementSeason(path) | |
shutil.copy2(path,showPath) | |
elif isMovie(path): | |
dst = drive + movies | |
shutil.copy2(path,dst) | |
def isTv(path): | |
fileName = os.path.basename(path) | |
fileName = fileName.replace(' ', '.') | |
matchObj = re.match( r'(.*)\.([S][0-9][0-9].?[E][0-9][0-9])(.*)', fileName, re.I) | |
if 'sample' in fileName.lower(): | |
return False | |
if matchObj and os.path.splitext(fileName)[1] in fileExtensions: | |
return True | |
else: | |
return False | |
def getShowName(path): | |
fileName = os.path.basename(path) | |
fileName = fileName.replace(' ', '.') | |
matchObj = re.match( r'(.*)\.([S][0-9][0-9].?[E][0-9][0-9])(.*)', fileName, re.I) | |
if matchObj: | |
return matchObj.group(1).replace('.', ' '); | |
else: | |
return '' | |
def incrementSeason(path): | |
fileName = os.path.basename(path) | |
matchObj = re.match(r'.*([S])([0-9][0-9]).*', fileName, re.I) | |
if matchObj: | |
s = matchObj.group(1) | |
season = matchObj.group(2) | |
seasonNumbers = list(season) | |
if seasonNumbers[1] == '9': | |
seasonNumbers[1] = '0' | |
seasonNumbers[0] = str(int(seasonNumbers[0]) + 1) | |
else: | |
seasonNumbers[1] = str(int(seasonNumbers[1]) + 1) | |
newSeason = 'S' + seasonNumbers[0] + seasonNumbers[1] | |
return re.sub(s + season, newSeason, fileName) | |
else: | |
return '' | |
def isMovie(path): | |
fileName = os.path.basename(path) | |
if 'sample' in fileName.lower(): | |
return False | |
if os.path.getsize(path) > 3221225472 and os.path.splitext(fileName)[1] in fileExtensions: | |
return True | |
else: | |
return False | |
## Main program | |
if len(sys.argv) >= 2: | |
path = sys.argv[1] | |
findFiles(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment