def movie_convert(source_file): """Convert the MKV file to valid M4V file""" import os import disklibrary print 'Checking file %s' % source_file file_check = disklibrary.file_check(source_file, 'mkv') if file_check is None: print 'Not a valid MKV file %s' % source_file return 1 os.nice(10) source_link = disklibrary.file_split(source_file) print 'Processing file %s' % source_link.file_name final_link = disklibrary.file_path(source_link.file_path, source_link.file_title + '.m4v') if disklibrary.file_check(final_link, 'm4v') is not None: disklibrary.file_delete(final_link) final_link = disklibrary.file_split(final_link) print 'Creating new file %s' % final_link.file_name error = create_output_file(source_link.full_path, final_link.full_path) if error is not None: print error return 2 print 'Removing source file' disklibrary.file_delete(source_link.full_path) return 0
def read_file_data(file_path): """Retrieve file data""" import os import sys import disklibrary from lxml import etree read_file = disklibrary.file_first(file_path.file_path, 'nfo') if read_file is None: return read_file = open(read_file) read_content = read_file.readlines() read_file.close() read_content = read_content[:-1] read_content = ''.join(read_content) read_file = etree.fromstring(read_content) movie_title = read_file.find('title').text movie_title = disklibrary.path_sane_name(movie_title) movie_year = read_file.find('year').text sort_name = read_file.find('sorttitle').text sort_name = disklibrary.path_sane_name(sort_name) sort_name = '%s (%s)' % (sort_name, movie_year) movie_file = '%s (%s)' % (movie_title, movie_year) movie_file = '%s/%s%s' % (file_path.file_path.replace( file_path.file_title, sort_name), movie_file, file_path.file_extension) movie_file = disklibrary.file_split(movie_file) return movie_file
def main(): """Main script interface for Movie Preparation Script""" import os import sys import argparse import disklibrary print 'Preparing movie' parser = argparse.ArgumentParser( description='Prepares movie for internal library addition') parser.add_argument('folder', metavar='folder', type=str, help='Folder to be checked') args = parser.parse_args() source_folder = args.folder print 'Checking folder %s' % source_folder if not os.path.exists(source_folder): print 'Folder not found' sys.exit(0) source_file = disklibrary.file_first(source_folder, 'mkv') source_file = disklibrary.file_split(source_file) if source_file is None: print 'No valid file found for processing' sys.exit(0) if source_file.file_extension == '.mkv': source_file = process_mkv(source_file) print 'Completed processing %s' % source_file.file_title sys.exit(0)
def rename_movie(source_path): """Rename movie folder and content""" import os import disklibrary file_path = disklibrary.file_first(source_path, 'mkv') if file_path is None: print 'File not found or incorrect type' return 2 file_path = disklibrary.file_split(file_path) rename_file = read_file_data(file_path) if not rename_file: print 'Required files missing' return 2 file_list = os.listdir(file_path.file_path) for file_name in file_list: if file_name == '.DS_Store': continue file_name = disklibrary.file_path(file_path.file_path, file_name) file_name = disklibrary.file_split(file_name) file_rename = file_name.file_title.replace(file_path.file_title, rename_file.file_title) file_rename += file_name.file_extension file_rename = disklibrary.file_path(file_name.file_path, file_rename) file_rename = disklibrary.file_split(file_rename) if file_name.file_name != file_rename.file_name: print 'Renaming file %s' % file_name.file_name os.rename(file_name.full_path, file_rename.full_path) if os.path.isdir(rename_file.file_path): return 0 print 'Renaming folder %s to %s' % (file_path.file_path, rename_file.file_path) if os.path.isdir( file_path.file_path) and not os.path.isdir(rename_file.file_path): os.rename(file_path.file_path, rename_file.file_path) return 0
def validate_file(source_file): """Validate MKV file""" import os import disklibrary print 'Checking file %s' % source_file file_check = disklibrary.file_check(source_file, 'mkv') if file_check is None: print 'Not a valid MKV file %s' % source_file return 1 os.nice(10) print 'Loading MKV file %s' % source_file source_file = disklibrary.file_split(source_file) print 'Processing file %s' % source_file.file_name source_mkv_file = read_mkv_file(source_file.full_path) if source_mkv_file.error is not None: print source_mkv_file.error return 2 source_mkv_file = source_mkv_file.mkv_file print 'Video Tracks: ' + str(len(source_mkv_file.video_tracks)) print 'Audio Tracks: ' + str(len(source_mkv_file.audio_tracks)) print 'Subtitle Tracks: ' + str(len(source_mkv_file.subtitle_tracks)) print 'Checking video tracks' use_video_track = find_valid_video_track(source_mkv_file) print 'Checking audio tracks' use_audio_track = find_valid_audio_track(source_mkv_file) if use_video_track is None: print 'Selected MKV does not contain a valid video track' return 2 if use_audio_track is None: if use_audio_track is None: print 'Selected MKV does not contain a valid audio track' return 2 convert_file = len(source_mkv_file.video_tracks) != 1 convert_file = convert_file or len(source_mkv_file.audio_tracks) != 1 convert_file = convert_file or len(source_mkv_file.subtitle_tracks) > 0 if not convert_file: print 'File already in correct format' return 0 final_link = source_file source_link = disklibrary.file_path(source_file.file_path, source_file.file_title + '.original') source_link = disklibrary.file_split(source_link) source_moved = disklibrary.file_rename(final_link.full_path, source_link.full_path) if not source_moved: print 'Could not create backup of %s' % final_link.file_name return 2 check_file = disklibrary.file_check(final_link.full_path, 'mkv') if check_file is not None: disklibrary.file_delete(final_link) print 'Creating new file %s' % final_link.file_name error = create_output_file(source_link.full_path, final_link.full_path, final_link.file_title, use_video_track, use_audio_track) if error is not None: print error return 2 disklibrary.file_delete(source_link.full_path) print 'Created new file %s' % final_link.file_name return 0
def movie_update(source_file): """Update movie with tag information""" import disklibrary import movielibrary import movielistslibrary file_path = disklibrary.file_check(source_file, 'm4v') if file_path is None: return 2 file_parts = disklibrary.file_split(file_path) if file_parts is None: print 'Unable to parse file path' return 2 movie_title = file_parts.file_title movie_year = movielibrary.movie_parse_year(file_parts.file_title) movie_title = movie_title.replace('(' + str(movie_year) + ')', '').strip() print 'Loading movie %s from %s' % (movie_title, movie_year) movie_data = movielibrary.movie_load_data( MOVIE_DB_API, movie_title, movie_year) if movie_data.error is not None: print movie_data.error return 2 if movie_data.data is None: print 'Movie %s from %s not found' % (movie_title, movie_year) return 2 cover_link = None if 'poster_path' in movie_data.data and movie_data.data['poster_path'] is not None: poster_path = movielibrary.movie_poster( file_parts.file_path, movie_data.data['poster_path']) cover_link = disklibrary.file_path( file_parts.file_path, file_parts.file_title + '-cover.jpg') print 'Downloading movie cover' if not disklibrary.file_rename(poster_path, cover_link): print 'Error creating movie cover' print 'Retrieving additional information for %s' % movie_title sort_name = movielibrary.movie_sort_name_builder( MOVIE_DB_API, movie_data.data) genre = movielibrary.movie_imdb_genre(movie_data.data['imdb_id']) if genre is None: genre = movie_data.data['genres'][0]['name'] studio = movielibrary.movie_imdb_studio(movie_data.data['imdb_id']) if studio is None: company_count = len(movie_data.data['production_companies']) if 'production_companies' in movie_data.data and company_count > 0: studio = movie_data.data['production_companies'][0]['name'] if sort_name.error is not None: print sort_name.error rating = movielibrary.movie_rating( movie_data.data['releases']['countries'], True) print 'Generating cast and crew info for %s' % movie_title credit_xml = movielistslibrary.build_plist_data(movie_data.data, studio) print 'Updating movie tags for %s' % movie_title result = update_file_tag(file_parts.full_path, movie_data.data, sort_name.sort_name, genre, rating, credit_xml, cover_link) if result is not None: print result return 1 return 0
def main(): """Missing movie info finder""" import os import sys import argparse import disklibrary print 'Starting Movie Info Missing Finder' parser = argparse.ArgumentParser( description='Checks for missing movie information') parser.add_argument('folder', metavar='folder', type=str, help='Folder to be checked') args = parser.parse_args() source_folder = args.folder if source_folder is None or not os.path.isdir(source_folder): print 'Folder %s not found or valid' % source_folder sys.exit(2) content_list = os.listdir(source_folder) content_count = len(content_list) if content_list is None or content_count == 0: print 'No files or folders found' sys.exit(2) os.nice(10) item_count = 0 for item in content_list: item_folder = os.path.join(source_folder, item) if not os.path.isdir(item_folder): continue try: m4v_file = disklibrary.file_first(item_folder, 'm4v') if m4v_file is None: print '%s Failed' % item_folder continue m4v_file = disklibrary.file_split(m4v_file) nfo_found = False nfo_file = disklibrary.file_first(item_folder, 'nfo') if nfo_file: nfo_file = disklibrary.file_split(nfo_file) nfo_found = m4v_file.file_title == nfo_file.file_title cover_found = False cover_file = '%s/%s-cover.jpg' % (m4v_file.file_path, m4v_file.file_title) cover_file = disklibrary.file_check(cover_file, 'jpg') if cover_file: cover_found = True poster_found = False poster_file = '%s/%s-fanart.jpg' % ( m4v_file.file_path, m4v_file.file_title) poster_file = disklibrary.file_check(poster_file, 'jpg') if poster_file: poster_found = True if not nfo_found or not cover_file or not poster_file: if len(m4v_file.file_title) > 70: print '{0:70}'.format(m4v_file.file_title[:70]), else: print '{0:70}'.format(m4v_file.file_title), print('NFO:'), if nfo_found: print '{0:15}'.format('Found'), else: print '{0:15}'.format('Not found'), print('\tCover:'), if cover_found: print '{0:15}'.format('Found'), else: print '{0:15}'.format('Not found'), print('\tPoster:'), if poster_found: print '{0:15}'.format('Found') else: print '{0:15}'.format('Not found') item_count += 1 except Exception as exception: print '%s error: %s' % (item_folder, exception.message) break print 'Missing NFO files: %d' % item_count sys.exit(0)
def generate_nfo(source_file, title, year): """Generate NFO content and data""" import disklibrary import movielibrary file_path = disklibrary.file_check(source_file, 'm4v') if file_path is None: file_path = disklibrary.file_check(source_file, 'mkv') if file_path is None: print 'File %s not found or incorrect type' % source_file return 2 file_parts = disklibrary.file_split(file_path) if file_parts is None: print 'Unable to parse file path' return 2 movie_title = file_parts.file_title movie_year = movielibrary.movie_parse_year(file_parts.file_title) if title is not None: movie_title = title if year is not None: movie_year = year movie_title = movie_title.replace('(' + str(movie_year) + ')', '').strip() print 'Loading movie %s from %s' % (movie_title, movie_year) movie_data = movielibrary.movie_load_data( MOVIE_DB_API, movie_title, movie_year) if movie_data.error is not None: print movie_data.error return 2 if movie_data.data is None: print 'Movie %s from %s not found' % (movie_title, movie_year) return 2 movie_sort_name = movielibrary.movie_sort_name_builder( MOVIE_DB_API, movie_data.data) if movie_sort_name.error is not None: print movie_sort_name.error return 2 print 'Loading additional data for %s' % movie_title movie_sort_name = movie_sort_name.sort_name movie_rating = movielibrary.movie_rating( movie_data.data['releases']['countries']) movie_genre = movielibrary.movie_imdb_genre(movie_data.data['imdb_id']) if movie_genre is None: movie_genre = movie_data.data['genres'][0]['name'] movie_studio = movielibrary.movie_imdb_studio(movie_data.data['imdb_id']) if movie_studio is None: company_count = len(movie_data.data['production_companies']) if 'production_companies' in movie_data.data and company_count > 0: movie_studio = movie_data.data['production_companies'][0]['name'] movie_info = collections.namedtuple( 'movie_info', 'data genre studio sort_name rating') movie_data = movie_info(movie_data.data, movie_genre, movie_studio, movie_sort_name, movie_rating) nfo_xml = build_nfo_xml(movie_data) if nfo_xml.error is not None: print nfo_xml.error return 2 nfo_file = disklibrary.file_path( file_parts.file_path, file_parts.file_title + '.nfo') print 'Writing NFO file' disklibrary.file_delete(nfo_file) disklibrary.file_write(nfo_file, nfo_xml.xml) imdb_url = "http://www.imdb.com/title/" + nfo_xml.imdb + '/' disklibrary.file_write(nfo_file, imdb_url) if nfo_xml.poster is not None: poster_path = movielibrary.movie_poster( file_parts.file_path, nfo_xml.poster) save_poster = disklibrary.file_path( file_parts.file_path, file_parts.file_title + '-cover.jpg') print 'Downloading movie poster' if not disklibrary.file_rename(poster_path, save_poster): print 'Error creating movie cover' if nfo_xml.backdrop is not None: backdrop_path = movielibrary.movie_backdrop( file_parts.file_path, nfo_xml.backdrop) save_backdrop = disklibrary.file_path( file_parts.file_path, file_parts.file_title + '-fanart.jpg') print 'Downloading movie background' if not disklibrary.file_rename(backdrop_path, save_backdrop): print 'Error creating movie background' return 0