Last active
February 19, 2017 05:43
-
-
Save shidarin/8e431b426f9b25d0969ed2fffbd41e68 to your computer and use it in GitHub Desktop.
Duplicates poster.jpg with {title}-poster.jpg and other movie folder cleanup
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
#!/usr/bin/env python | |
import logging | |
import os | |
import re | |
import shutil | |
MOVIES = '/mnt/media/Videos/Movies' | |
MOVIE_NAME = re.compile(r'(^.*?) \(\d{4}\)$') | |
MOVIE_WIDTH = re.compile(r'.*?<width>(\d{3,4})<\/width>.*?') | |
POSTERS = ('poster.jpg', '{0}-poster.jpg') | |
FANART = ('fanart.jpg', '{0}-fanart.jpg') | |
LOG_LOCATION = '/home/pi/temp/movie_cleanup.log' | |
logger = logging.getLogger(__name__) | |
logging.basicConfig( | |
filename=LOG_LOCATION, | |
level=logging.INFO, | |
format='%(asctime)s %(levelname)s, %(message)s' | |
) | |
console = logging.StreamHandler() | |
console.setLevel(logging.INFO) | |
console.setFormatter(logging.Formatter('%(asctime)s %(levelname)s, %(message)s')) | |
logger.addHandler(console) | |
def remove_period_from_end(title): | |
if title.endswith('.'): | |
title = title[:-1] | |
return remove_period_from_end(title) | |
return title | |
def check_and_copy_content(content_type, path, contents, title): | |
if title.endswith('.'): | |
title = remove_period_from_end(title) # Ember saves with no trailing .s | |
if content_type[0] in contents and not content_type[1].format(title) in contents: | |
logger.info("[{title}] Filling {dest}".format(title=title, dest=content_type[1].format(title))) | |
shutil.copy(os.path.join(path, content_type[0]), os.path.join(path, content_type[1]).format(title)) | |
elif content_type[1].format(title) in contents and not content_type[0] in contents: | |
logger.info("[{title}] Filling {dest}".format(title=title, dest=content_type[0])) | |
shutil.copy(os.path.join(path, content_type[1].format(title)), os.path.join(path, content_type[0])) | |
def check_and_copy_posters(path, contents, title): | |
check_and_copy_content(POSTERS, path, contents, title) | |
def check_and_copy_fanart(path, contents, title): | |
check_and_copy_content(FANART, path, contents, title) | |
def clean(path, contents, title): | |
if title.endswith('.'): | |
title = remove_period_from_end(title) # Ember saves with no trailing .s | |
if '{title}.tbn'.format(title=title) in contents: | |
logger.info("[{title}] Removing {title}.tbn".format(title=title)) | |
os.remove(os.path.join(path, '{title}.tbn'.format(title=title))) | |
if 'movie.tbn' in contents: | |
logger.info("[{title}] Removing movie.tbn".format(title=title)) | |
os.remove(os.path.join(path, 'movie.tbn')) | |
if 'folder.jpg' in contents: | |
logger.info("[{title}] Removing folder.jpg".format(title=title)) | |
os.remove(os.path.join(path, 'folder.jpg')) | |
def fix_videosource(path, contents, title): | |
if title.endswith('.'): | |
title = remove_period_from_end(title) # Ember saves with no trailing .s | |
if not '{title}.nfo'.format(title=title) in contents: | |
logging.warning("[{title}] No .nfo found!".format(title=title)) | |
return | |
with open(os.path.join(path, '{title}.nfo'.format(title=title)), 'r') as f: | |
nfo = f.read() | |
if not 'videosource' in nfo: | |
width = re.search(MOVIE_WIDTH, nfo) | |
if not width: | |
logger.warning("[{title}] Could not determine width for setting videosource".format(title=title)) | |
return | |
width = width.group(1) | |
if int(width) in range(1000, 2000): | |
logger.info('[{title}] Setting nfo videosource to bluray'.format(title=title)) | |
nfo = nfo.replace('</movie>', ' <videosource>bluray</videosource>\n</movie>') | |
elif int(width) in range(600, 800): | |
logger.info('[{title}] Setting nfo videosource to dvd'.format(title=title)) | |
nfo = nfo.replace('</movie>', ' <videosource>dvd</videosource>\n</movie>') | |
else: | |
logger.warning('[{title}] Could not determine width for videosource'.format(title=title)) | |
return | |
with open(os.path.join(path, '{title}.nfo'.format(title=title)), 'w') as f: | |
f.write(nfo) | |
for folder in os.listdir(MOVIES): | |
path = os.path.join(MOVIES, folder) | |
if os.path.isdir(path): | |
title = re.match(MOVIE_NAME, folder) | |
if not title: | |
logger.warning("Could not determine title for movie: {0}".format(folder)) | |
continue | |
title = title.group(1) | |
contents = os.listdir(path) | |
check_and_copy_posters(path, contents, title) | |
check_and_copy_fanart(path, contents, title) | |
fix_videosource(path, contents, title) | |
clean(path, contents, title) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment