# Path: app/config/cplog.py
# class CPLog():
# 
#     context = ''
# 
#     def __init__(self, context = ''):
#         self.context = context
#         self.logger = cherrypy.log
# 
#     def info(self, msg):
#         self.log(msg, severity = logging.INFO)
# 
#     def debug(self, msg):
#         self.log(msg, severity = logging.DEBUG)
# 
#     def error(self, msg):
#         self.log(msg, severity = logging.ERROR)
# 
#     def log(self, msg = '', severity = logging.INFO):
#         try:
#             self.logger.error(msg = app.latinToAscii(msg), context = self.context, severity = severity)
#         except:
#             self.logger.error(msg = msg, context = self.context, severity = severity)
# 
#     def config(self, logPath, debug = False):
# 
#         level = logging.DEBUG if debug else logging.INFO
# 
#         # Overwrite functions
#         self.logger.time = self.time
#         self.logger.error = self.logError
#         self.logger.access = self.access
# 
#         # Set screen and level
#         self.logger.screen = debug
#         self.logger.error_log.setLevel(level)
# 
#         # Create log dir
#         if not os.path.isdir(logPath):
#             os.mkdir(logPath)
# 
#         self.logger.error_file = ""
# 
#         # Create RotatingFileHandler
#         h = handlers.RotatingFileHandler(os.path.join(logPath, 'CouchPotato.log'), 'a', 500000, 4)
#         h.setLevel(level)
#         h.setFormatter(logging.Formatter('%(asctime)s %(levelname)-5.5s %(message)s', '%H:%M:%S'))
#         self.logger.error_log.addHandler(h)
# 
#     def access(self):
#         pass
# 
#     def logError(self, msg = '', context = '', severity = logging.INFO, traceback = False):
# 
#         if traceback:
#             msg += _cperror.format_exc()
# 
#         context = context if context else self.context
#         self.logger.error_log.log(severity, '[%+25.25s] %s' % (context[-25:], msg))
# 
#     def time(self):
#         now = datetime.datetime.now()
#         return ('[%02d:%02d:%02d]' % (now.hour, now.minute, now.second))
# 
# Path: app/config/db.py
# class DbVersion(object):
# class Movie(object):
# class MovieQueue(object):
# class MovieETA(object):
# class MovieExtra(object):
# class RenameHistory(object):
# class History(object):
# class SubtitleHistory(object):
# class QualityTemplate(object):
# class QualityTemplateType(object):
#     def __init__(self, version):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
# def initDb():
# def upgradeDb():
# def migrateVersion4():
# def migrateVersion3():
# def migrateVersion2():


from app import latinToAscii
from app.config.cplog import CPLog
from app.config.db import Movie, Session as Db, MovieQueue
from app.lib import hashFile
from app.lib.qualities import Qualities
import cherrypy
import fnmatch
import json
import os
import re
import subprocess
        movieYear = self.findYear(movie['folder'])

        #check to see if the downloaded movie nfo file agrees with what we thought we were downloading
        if movie['info']['cpnfoImdb'] and movie['info']['imdb']:
            cpnfoimdb = 'tt' + movie['info']['cpnfoImdb'].replace("tt", '')
            nfoimdb = 'tt' + movie['info']['imdb'].replace("tt", '')
            if cpnfoimdb != nfoimdb:
                log.info("Downloaded movie's nfo has imdb id that doesn't match what we though we downloaded")
            movie['info']['imdb'] = cpnfoimdb

        if movie['info']['imdb']:
            byImdb = self.getMovieByIMDB(movie['info']['imdb'])
            if byImdb:
                return byImdb
            else:
                result = cherrypy.config['searchers']['movie'].findByImdbId(movie['info']['imdb'])

        if not result:

            # Check and see if name is in queue
            try:
                queue = Db.query(MovieQueue).filter_by(name = movie['folder']).first()
                if queue:
                    log.info('Found movie via MovieQueue.')
                    return queue.Movie
            except:
                pass

            # Find movie via movie name
            try:

# Path: app/config/cplog.py
# class CPLog():
# 
#     context = ''
# 
#     def __init__(self, context = ''):
#         self.context = context
#         self.logger = cherrypy.log
# 
#     def info(self, msg):
#         self.log(msg, severity = logging.INFO)
# 
#     def debug(self, msg):
#         self.log(msg, severity = logging.DEBUG)
# 
#     def error(self, msg):
#         self.log(msg, severity = logging.ERROR)
# 
#     def log(self, msg = '', severity = logging.INFO):
#         try:
#             self.logger.error(msg = app.latinToAscii(msg), context = self.context, severity = severity)
#         except:
#             self.logger.error(msg = msg, context = self.context, severity = severity)
# 
#     def config(self, logPath, debug = False):
# 
#         level = logging.DEBUG if debug else logging.INFO
# 
#         # Overwrite functions
#         self.logger.time = self.time
#         self.logger.error = self.logError
#         self.logger.access = self.access
# 
#         # Set screen and level
#         self.logger.screen = debug
#         self.logger.error_log.setLevel(level)
# 
#         # Create log dir
#         if not os.path.isdir(logPath):
#             os.mkdir(logPath)
# 
#         self.logger.error_file = ""
# 
#         # Create RotatingFileHandler
#         h = handlers.RotatingFileHandler(os.path.join(logPath, 'CouchPotato.log'), 'a', 500000, 4)
#         h.setLevel(level)
#         h.setFormatter(logging.Formatter('%(asctime)s %(levelname)-5.5s %(message)s', '%H:%M:%S'))
#         self.logger.error_log.addHandler(h)
# 
#     def access(self):
#         pass
# 
#     def logError(self, msg = '', context = '', severity = logging.INFO, traceback = False):
# 
#         if traceback:
#             msg += _cperror.format_exc()
# 
#         context = context if context else self.context
#         self.logger.error_log.log(severity, '[%+25.25s] %s' % (context[-25:], msg))
# 
#     def time(self):
#         now = datetime.datetime.now()
#         return ('[%02d:%02d:%02d]' % (now.hour, now.minute, now.second))
# 
# Path: app/config/db.py
# class DbVersion(object):
# class Movie(object):
# class MovieQueue(object):
# class MovieETA(object):
# class MovieExtra(object):
# class RenameHistory(object):
# class History(object):
# class SubtitleHistory(object):
# class QualityTemplate(object):
# class QualityTemplateType(object):
#     def __init__(self, version):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
# def initDb():
# def upgradeDb():
# def migrateVersion4():
# def migrateVersion3():
# def migrateVersion2():


# Path: app/lib/library.py
from app import latinToAscii
from app.config.cplog import CPLog
from app.config.db import Movie, Session as Db, MovieQueue
from app.lib import hashFile
from app.lib.qualities import Qualities
import cherrypy
import fnmatch
import json
import os
import re
import subprocess

        movieYear = self.findYear(movie['folder'])

        #check to see if the downloaded movie nfo file agrees with what we thought we were downloading
        if movie['info']['cpnfoImdb'] and movie['info']['imdb']:
            cpnfoimdb = 'tt' + movie['info']['cpnfoImdb'].replace("tt", '')
            nfoimdb = 'tt' + movie['info']['imdb'].replace("tt", '')
            if cpnfoimdb != nfoimdb:
                log.info("Downloaded movie's nfo has imdb id that doesn't match what we though we downloaded")
            movie['info']['imdb'] = cpnfoimdb

        if movie['info']['imdb']:
            byImdb = self.getMovieByIMDB(movie['info']['imdb'])
            if byImdb:
                return byImdb
            else:
                result = cherrypy.config['searchers']['movie'].findByImdbId(movie['info']['imdb'])

        if not result:

            # Check and see if name is in queue
            try:
                queue = Db.query(MovieQueue).filter_by(name = movie['folder']).first()
                if queue:
                    log.info('Found movie via MovieQueue.')
                    return queue.Movie
            except:
                pass

            # Find movie via movie name
            try:

                m = Db.query(Movie).filter_by(name = movieName).first()