<commit_before>#!/usr/bin/env python

import sys
import os
import guessit
import locale
import glob
import argparse
import struct
from readSettings import ReadSettings
from tvdb_mp4 import Tvdb_mp4
from tmdb_mp4 import tmdb_mp4
from mkvtomp4 import MkvtoMp4
from tvdb_api import tvdb_api
from tmdb_api import tmdb
from extensions import tmdb_api_key

settings = ReadSettings(os.path.dirname(sys.argv[0]), "autoProcess.ini")


def mediatype():
    print "Select media type:"
    print "1. Movie (via IMDB ID)"
    print "2. Movie (via TMDB ID)"
    print "3. TV"
    print "4. Convert without tagging"
    print "5. Skip file"
    result = raw_input("#: ")
    if 0 < int(result) < 6:
        return int(result)
    else:
        print "Invalid selection"
        return mediatype()


def getValue(prompt, num=False):
    print prompt + ":"
    value = raw_input("#: ").strip(' \"')
    # Remove escape characters in non-windows environments
    if os.name != 'nt': value = value.replace('\\', '')
    value = value.decode(sys.stdout.encoding)
    if num is True and value.isdigit() is False:
        print "Must be a numerical value"
        return getValue(prompt, num)
    else:
        return value


def getYesNo():
    yes = ['y', 'yes', 'true', '1']
    no = ['n', 'no', 'false', '0']
    data = raw_input("# [y/n]: ")
    if data.lower() in yes:
        return True
    elif data.lower() in no:
        return False
    else:
        print "Invalid selection"
        return getYesNo()


def getinfo(fileName=None, silent=False, tag=settings.tagfile, tvdbid=None):
    tagdata = None
    # Try to guess the file is guessing is enabled
    if fileName is not None: tagdata = guessInfo(fileName, tvdbid)
    if silent is False:
        if tagdata:
            print "Proceed using guessed identification from filename?"
            if getYesNo():
                return tagdata
        else:
            print "Unable to determine identity based on filename, must enter manually"
        m_type = mediatype()
        if m_type is 3:
            tvdbid = getValue("Enter TVDB Series ID", True)
            season = getValue("Enter Season Number", True)
            episode = getValue("Enter Episode Number", True)
            return m_type, tvdbid, season, episode
        elif m_type is 1:
            imdbid = getValue("Enter IMDB ID")
            return m_type, imdbid
        elif m_type is 2:
            tmdbid = getValue("Enter TMDB ID", True)
            return m_type, tmdbid
        elif m_type is 4:
            return None
        elif m_type is 5:
            return False
    else:
        if tagdata and tag: 
            return tagdata
        else:
            return None


def guessInfo(fileName, tvdbid=None):
    if not settings.fullpathguess:
        fileName = os.path.basename(fileName)
    guess = guessit.guess_video_info(fileName)
    try:
        if guess['type'] == 'movie':
            return tmdbInfo(guess)
        elif  guess['type'] == 'episode':
            return tvdbInfo(guess, tvdbid)
        else:
            return None
    except Exception as e:
        print e
        return None


def tmdbInfo(guessData):
    tmdb.configure(tmdb_api_key)
    movies = tmdb.Movies(guessData["title"].encode('ascii', errors='ignore'), limit=4)
    for movie in movies.iter_results():
        #Identify the first movie in the collection that matches exactly the movie title
        foundname = ''.join(e for e in movie["title"] if e.isalnum())
        origname = ''.join(e for e in guessData["title"] if e.isalnum())
        #origname = origname.replace('&', 'and')
        if foundname.lower() == origname.lower():
            print "Matched movie title as: %s %s" % (movie["title"].encode(sys.stdout.encoding, errors='ignore'), movie["release_date"].encode(sys.stdout.encoding, errors='ignore'))
            movie = tmdb.Movie(movie["id"])
            if isinstance(movie, dict):
                tmdbid = movie["id"]
            else:
                tmdbid = movie.get_id()
            return 2, tmdbid
    return None


def tvdbInfo(guessData, tvdbid=None):
    series = guessData["series"]
    if 'year' in guessData:
        fullseries = series + " (" + str(guessData["year"]) + ")"
    season = guessData["season"]
    episode = guessData["episodeNumber"]
    t = tvdb_api.Tvdb()
    #tvdbid = t[series]['id']
    try:
        tvdbid = str(tvdbid) if tvdbid else t[fullseries]['id']
        series = t[fullseries]['seriesname']
    except:
        tvdbid = t[series]['id']
    try:
        print "Matched TV episode as %s (TVDB ID:%d) S%02dE%02d" % (series.encode(sys.stdout.encoding, errors='ignore'), int(tvdbid), int(season), int(episode))
    except:
        print "Matched TV episode"
    return 3, tvdbid, season, episode


def processFile(inputfile, tagdata, relativePath=None):
    # Gather tagdata
    if tagdata is False:
        return # This means the user has elected to skip the file
    elif tagdata is None:
        tagmp4 = None # No tag data specified but convert the file anyway
    elif tagdata[0] is 1:
        imdbid = tagdata[1]
        tagmp4 = tmdb_mp4(imdbid, language=settings.taglanguage)
        try:
            print "Processing %s" % (tagmp4.title.encode(sys.stdout.encoding, errors='ignore'))
        except:
            print "Processing movie"
    elif tagdata[0] is 2:
        tmdbid = tagdata[1]
        tagmp4 = tmdb_mp4(tmdbid, True, language=settings.taglanguage)
        try:
            print "Processing %s" % (tagmp4.title.encode(sys.stdout.encoding, errors='ignore'))
        except:
            print "Processing movie"
    elif tagdata[0] is 3:
        tvdbid = int(tagdata[1])
        season = int(tagdata[2])
        episode = int(tagdata[3])
        tagmp4 = Tvdb_mp4(tvdbid, season, episode, language=settings.taglanguage)
        try:
            print "Processing %s Season %02d Episode %02d - %s" % (tagmp4.show.encode(sys.stdout.encoding, errors='ignore'), int(tagmp4.season), int(tagmp4.episode), tagmp4.title.encode(sys.stdout.encoding, errors='ignore'))
        except:
            print "Processing TV episode"
    
    # Process
    try: 
        inputfile = inputfile.encode(locale.getpreferredencoding())
    except: 
        raise Exception, "File contains an unknown character that cannot be handled by under Python in your operating system, please rename the file"
    if MkvtoMp4(settings).validSource(inputfile):
        converter = MkvtoMp4(settings)
        output = converter.process(inputfile, True)
        if tagmp4 is not None:
            try:
                tagmp4.setHD(output['x'], output['y'])
                tagmp4.writeTags(output['output'], settings.artwork)
            except:
                print "There was an error tagging the file"
        if settings.relocate_moov:
            converter.QTFS(output['output'])
        converter.replicate(output['output'], relativePath=relativePath)


def walkDir(dir, silent=False, preserveRelative=False, tvdbid=None):
    for r,d,f in os.walk(dir):
        for file in f:
            filepath = os.path.join(r, file)
            relative = os.path.split(os.path.relpath(filepath , dir))[0] if preserveRelative else None
            try:
                if MkvtoMp4(settings).validSource(filepath):
                    try:
                        print "Processing file %s" % (filepath.encode(sys.stdout.encoding, errors='ignore'))
                    except:
                        try:
                            print "Processing file %s" % (filepath.encode('utf-8', errors='ignore'))
                        except:
                            print "Processing file"
                    tagdata = getinfo(filepath, silent, tvdbid=tvdbid)
                    processFile(filepath, tagdata, relativePath=relative)
            except Exception as e:
                print "An unexpected error occurred, processing of this file has failed"
                print str(e)


def main():
    parser = argparse.ArgumentParser(description="Manual conversion and tagging script for sickbeard_mp4_automator")<commit_msg>better error reporting for tagging errors manual.py<commit_after>#!/usr/bin/env python

import sys
import os
import guessit
import locale
import glob
import argparse
import struct
from readSettings import ReadSettings
from tvdb_mp4 import Tvdb_mp4
from tmdb_mp4 import tmdb_mp4
from mkvtomp4 import MkvtoMp4
from tvdb_api import tvdb_api
from tmdb_api import tmdb
from extensions import tmdb_api_key

settings = ReadSettings(os.path.dirname(sys.argv[0]), "autoProcess.ini")


def mediatype():
    print "Select media type:"
    print "1. Movie (via IMDB ID)"
    print "2. Movie (via TMDB ID)"
    print "3. TV"
    print "4. Convert without tagging"
    print "5. Skip file"
    result = raw_input("#: ")
    if 0 < int(result) < 6:
        return int(result)
    else:
        print "Invalid selection"
        return mediatype()


def getValue(prompt, num=False):
    print prompt + ":"
    value = raw_input("#: ").strip(' \"')
    # Remove escape characters in non-windows environments
    if os.name != 'nt': value = value.replace('\\', '')
    value = value.decode(sys.stdout.encoding)
    if num is True and value.isdigit() is False:
        print "Must be a numerical value"
        return getValue(prompt, num)
    else:
        return value


def getYesNo():
    yes = ['y', 'yes', 'true', '1']
    no = ['n', 'no', 'false', '0']
    data = raw_input("# [y/n]: ")
    if data.lower() in yes:
        return True
    elif data.lower() in no:
        return False
    else:
        print "Invalid selection"
        return getYesNo()


def getinfo(fileName=None, silent=False, tag=settings.tagfile, tvdbid=None):
    tagdata = None
    # Try to guess the file is guessing is enabled
    if fileName is not None: tagdata = guessInfo(fileName, tvdbid)
    if silent is False:
        if tagdata:
            print "Proceed using guessed identification from filename?"
            if getYesNo():
                return tagdata
        else:
            print "Unable to determine identity based on filename, must enter manually"
        m_type = mediatype()
        if m_type is 3:
            tvdbid = getValue("Enter TVDB Series ID", True)
            season = getValue("Enter Season Number", True)
            episode = getValue("Enter Episode Number", True)
            return m_type, tvdbid, season, episode
        elif m_type is 1:
            imdbid = getValue("Enter IMDB ID")
            return m_type, imdbid
        elif m_type is 2:
            tmdbid = getValue("Enter TMDB ID", True)
            return m_type, tmdbid
        elif m_type is 4:
            return None
        elif m_type is 5:
            return False
    else:
        if tagdata and tag: 
            return tagdata
        else:
            return None


def guessInfo(fileName, tvdbid=None):
    if not settings.fullpathguess:
        fileName = os.path.basename(fileName)
    guess = guessit.guess_video_info(fileName)
    try:
        if guess['type'] == 'movie':
            return tmdbInfo(guess)
        elif  guess['type'] == 'episode':
            return tvdbInfo(guess, tvdbid)
        else:
            return None
    except Exception as e:
        print e
        return None


def tmdbInfo(guessData):
    tmdb.configure(tmdb_api_key)
    movies = tmdb.Movies(guessData["title"].encode('ascii', errors='ignore'), limit=4)
    for movie in movies.iter_results():
        #Identify the first movie in the collection that matches exactly the movie title
        foundname = ''.join(e for e in movie["title"] if e.isalnum())
        origname = ''.join(e for e in guessData["title"] if e.isalnum())
        #origname = origname.replace('&', 'and')
        if foundname.lower() == origname.lower():
            print "Matched movie title as: %s %s" % (movie["title"].encode(sys.stdout.encoding, errors='ignore'), movie["release_date"].encode(sys.stdout.encoding, errors='ignore'))
            movie = tmdb.Movie(movie["id"])
            if isinstance(movie, dict):
                tmdbid = movie["id"]
            else:
                tmdbid = movie.get_id()
            return 2, tmdbid
    return None


def tvdbInfo(guessData, tvdbid=None):
    series = guessData["series"]
    if 'year' in guessData:
        fullseries = series + " (" + str(guessData["year"]) + ")"
    season = guessData["season"]
    episode = guessData["episodeNumber"]
    t = tvdb_api.Tvdb()
    #tvdbid = t[series]['id']
    try:
        tvdbid = str(tvdbid) if tvdbid else t[fullseries]['id']
        series = t[fullseries]['seriesname']
    except:
        tvdbid = t[series]['id']
    try:
        print "Matched TV episode as %s (TVDB ID:%d) S%02dE%02d" % (series.encode(sys.stdout.encoding, errors='ignore'), int(tvdbid), int(season), int(episode))
    except:
        print "Matched TV episode"
    return 3, tvdbid, season, episode


def processFile(inputfile, tagdata, relativePath=None):
    # Gather tagdata
    if tagdata is False:
        return # This means the user has elected to skip the file
    elif tagdata is None:
        tagmp4 = None # No tag data specified but convert the file anyway
    elif tagdata[0] is 1:
        imdbid = tagdata[1]
        tagmp4 = tmdb_mp4(imdbid, language=settings.taglanguage)
        try:
            print "Processing %s" % (tagmp4.title.encode(sys.stdout.encoding, errors='ignore'))
        except:
            print "Processing movie"
    elif tagdata[0] is 2:
        tmdbid = tagdata[1]
        tagmp4 = tmdb_mp4(tmdbid, True, language=settings.taglanguage)
        try:
            print "Processing %s" % (tagmp4.title.encode(sys.stdout.encoding, errors='ignore'))
        except:
            print "Processing movie"
    elif tagdata[0] is 3:
        tvdbid = int(tagdata[1])
        season = int(tagdata[2])
        episode = int(tagdata[3])
        tagmp4 = Tvdb_mp4(tvdbid, season, episode, language=settings.taglanguage)
        try:
            print "Processing %s Season %02d Episode %02d - %s" % (tagmp4.show.encode(sys.stdout.encoding, errors='ignore'), int(tagmp4.season), int(tagmp4.episode), tagmp4.title.encode(sys.stdout.encoding, errors='ignore'))
        except:
            print "Processing TV episode"
    
    # Process
    try: 
        inputfile = inputfile.encode(locale.getpreferredencoding())
    except: 
        raise Exception, "File contains an unknown character that cannot be handled by under Python in your operating system, please rename the file"
    if MkvtoMp4(settings).validSource(inputfile):
        converter = MkvtoMp4(settings)
        output = converter.process(inputfile, True)
        if tagmp4 is not None:
            try:
                tagmp4.setHD(output['x'], output['y'])
                tagmp4.writeTags(output['output'], settings.artwork)
            except Exception as e:
                print "There was an error tagging the file"
                print e
        if settings.relocate_moov:
            converter.QTFS(output['output'])
        converter.replicate(output['output'], relativePath=relativePath)


def walkDir(dir, silent=False, preserveRelative=False, tvdbid=None):
    for r,d,f in os.walk(dir):
        for file in f:
            filepath = os.path.join(r, file)
            relative = os.path.split(os.path.relpath(filepath , dir))[0] if preserveRelative else None
            try:
                if MkvtoMp4(settings).validSource(filepath):
                    try:
                        print "Processing file %s" % (filepath.encode(sys.stdout.encoding, errors='ignore'))
                    except:
                        try:
                            print "Processing file %s" % (filepath.encode('utf-8', errors='ignore'))
                        except:
                            print "Processing file"
                    tagdata = getinfo(filepath, silent, tvdbid=tvdbid)
                    processFile(filepath, tagdata, relativePath=relative)
            except Exception as e:
                print "An unexpected error occurred, processing of this file has failed"
                print str(e)


def main():
    parser = argparse.ArgumentParser(description="Manual conversion and tagging script for sickbeard_mp4_automator")
{'fraction_non_alphanumeric': 0.1065952336966562, 'fraction_numerical': 0.008005419052897346, 'mean_word_length': 2.3408763628882947, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7333559', 'n_tokens_mistral': 4895, 'n_tokens_neox': 4789, 'n_words': 1410}