Code:
import os
import sys
import re
import shutil
from urllib import quote_plus
import datetime
print "Start Media Manager..."
def _mkdir(newdir):
"""works the way a good mkdir should :)
- already exists, silently complete
- regular file in the way, raise an exception
- parent directory(ies) does not exist, make them as well
"""
if os.path.isdir(newdir):
pass
elif os.path.isfile(newdir):
raise OSError("a file with the same name as the desired " \
"dir, '%s', already exists." % newdir)
else:
head, tail = os.path.split(newdir)
if head and not os.path.isdir(head):
_mkdir(head)
if tail:
os.mkdir(newdir)
src_dir = 'F:\\My BR Rips'
for root, dirs, files in os.walk(src_dir):
for name in files:
p=re.compile('(.+?).\[(.+?)-(.+?)-H.264\]-NewArtRiot.mp4$')
match=p.findall(name)
for moviename, year, format in match:
if format == "HDDVDRip":
new_format = "HDDVD"
elif format == "BDRip":
new_format = "bluray"
else:
new_format = format
old_file = os.path.join(src_dir, moviename+' ['+year+'-'+format+'-H.264]-NewArtRiot', moviename+' ['+year+'-'+format+'-H.264]-NewArtRiot.mp4')
new_file = os.path.join(src_dir, moviename+' ['+year+'-'+format+'-H.264]-NewArtRiot', moviename+' ('+year+') ('+new_format+').mp4')
os.rename (old_file, new_file)
old_dir = os.path.join(src_dir, moviename+' ['+year+'-'+format+'-H.264]-NewArtRiot')
new_dir = os.path.join(src_dir, moviename+' ('+year+') (' +new_format+')')
os.rename(old_dir, new_dir)
src_dir = 'F:\\TEMP'
new_dir = 'F:\\My TV Shows'
for root, dirs, files in os.walk(src_dir):
for name in files:
p=re.compile('(.+?)\.S([0-9]+)E([0-9]+).+?\.mkv$')
match=p.findall(name)
for filename, season, episode in match:
filename = filename.replace(".", " ")
newdir = os.path.join(new_dir,filename)
_mkdir(newdir)
shutil.move(os.path.join(src_dir,name), os.path.join(newdir,name))
p=re.compile('(.+?)\.S([0-9]+)E([0-9]+).+?\.avi$')
match=p.findall(name)
for filename, season, episode in match:
filename = filename.replace(".", " ")
newdir = os.path.join(new_dir,filename)
_mkdir(newdir)
shutil.move(os.path.join(src_dir,name), os.path.join(newdir,name))
p=re.compile('(.+?)\.([0-9]+)x([0-9]+).+?\.avi$')
match=p.findall(name)
for filename, season, episode in match:
filename = filename.replace(".", " ")
newdir = os.path.join(new_dir,filename)
_mkdir(newdir)
shutil.move(os.path.join(src_dir,name), os.path.join(newdir,name))
#delete old tv shows
d = datetime.datetime.now() + datetime.timedelta(days=-60)
minusdays = d.strftime('%Y-%m-%d')
new_sql = "SELECT path.strPath AS path, files.strFileName AS filename FROM episode JOIN files ON files.idFile=episode.idFile JOIN path ON files.idPath=path.idPath WHERE episode.c05 <= '"+minusdays+"';"
records = xbmc.executehttpapi( "QueryVideoDatabase(%s)" % ( quote_plus( new_sql ), ) )
p=re.compile('<field>(.+?)</field><field>(.+?)</field>')
match=p.findall(records)
for path, filename in match:
xbmc.executehttpapi("FileDelete(%s)" % (path + filename))
#let's delete duplicate shows that aren't in HD
new_sql = "select episode.c00, tvshowlinkepisode.idShow, count(*) as total from episode JOIN tvshowlinkepisode ON episode.idEpisode = tvshowlinkepisode.idEpisode group by tvshowlinkepisode.idShow, episode.c00 having count(*) > 1 order by total desc;"
records = xbmc.executehttpapi( "QueryVideoDatabase(%s)" % ( quote_plus( new_sql ), ) )
p=re.compile('<field>(.+?)</field><field>(.+?)</field><field>(.+?)</field>')
match=p.findall(records)
for c00, idShow, total in match:
new_sql = "SELECT path.strPath AS path, files.strFileName AS filename FROM episode JOIN files ON files.idFile=episode.idFile JOIN tvshowlinkepisode ON episode.idEpisode = tvshowlinkepisode.idEpisode JOIN path ON files.idPath=path.idPath WHERE tvshowlinkepisode.idShow = '" + idShow + "' AND episode.c00 = \"" + c00 + "\" AND files.strFileName NOT LIKE '%%720P%%' LIMIT 1;"
records = xbmc.executehttpapi( "QueryVideoDatabase(%s)" % ( quote_plus( new_sql ), ) )
p=re.compile('<field>(.+?)</field><field>(.+?)</field>')
match=p.findall(records)
for path, filename in match:
xbmc.executehttpapi("FileDelete(%s)" % (path + filename))
print "Deleted " + path + filename
xbmc.executebuiltin('XBMC.updatelibrary(video)')
print "Media Manager is finished..."