# 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.config.cplog import CPLog
from app.config.db import Session as Db, MovieExtra, Movie
from app.lib.provider.movie.sources.imdbWrapper import imdbWrapper
from app.lib.provider.movie.sources.theMovieDb import theMovieDb
from sqlalchemy.sql.expression import and_, or_
import cherrypy
import os
                self.saveExtra(movieId, 'overview', overview)

            hasPosterThumb = False

            images = movieInfo.findall('images/image')
            hasPosterCover = False
            hasPosterMid = False
            hasPosterThumb = False
            for image in images:
                imageFile = str(theMovieDbId) + '-' + image.get('type') + '-' + image.get('size') + '.jpg'
                if image.get('type') == 'poster' and image.get('size') == 'cover' and not hasPosterCover:
                    poster = self.theMovieDb.saveImage(image.get('url'), imageFile)
                    if poster: self.saveExtra(movieId, 'poster_cover', poster)
                    hasPosterCover = True
                if image.get('type') == 'poster' and image.get('size') == 'mid' and not hasPosterMid:
                    poster = self.theMovieDb.saveImage(image.get('url'), imageFile)
                    if poster: self.saveExtra(movieId, 'poster_mid', poster)
                    hasPosterMid = True
                if image.get('type') == 'poster' and image.get('size') == 'thumb' and not hasPosterThumb:
                    poster = self.theMovieDb.saveImage(image.get('url'), imageFile)
                    if poster: self.saveExtra(movieId, 'poster_thumb', poster)
                    hasPosterThumb = True

            handle.close()

    def saveExtra(self, id, name, value):

        exists = Db.query(MovieExtra).filter(and_(MovieExtra.movieId == id, MovieExtra.name == name)).first()
        if not exists:
            exists = MovieExtra()

# 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/provider/movie/search.py
from app.config.cplog import CPLog
from app.config.db import Session as Db, MovieExtra, Movie
from app.lib.provider.movie.sources.imdbWrapper import imdbWrapper
from app.lib.provider.movie.sources.theMovieDb import theMovieDb
from sqlalchemy.sql.expression import and_, or_
import cherrypy
import os

                self.saveExtra(movieId, 'overview', overview)

            hasPosterThumb = False

            images = movieInfo.findall('images/image')
            hasPosterCover = False
            hasPosterMid = False
            hasPosterThumb = False
            for image in images:
                imageFile = str(theMovieDbId) + '-' + image.get('type') + '-' + image.get('size') + '.jpg'
                if image.get('type') == 'poster' and image.get('size') == 'cover' and not hasPosterCover:
                    poster = self.theMovieDb.saveImage(image.get('url'), imageFile)
                    if poster: self.saveExtra(movieId, 'poster_cover', poster)
                    hasPosterCover = True
                if image.get('type') == 'poster' and image.get('size') == 'mid' and not hasPosterMid:
                    poster = self.theMovieDb.saveImage(image.get('url'), imageFile)
                    if poster: self.saveExtra(movieId, 'poster_mid', poster)
                    hasPosterMid = True
                if image.get('type') == 'poster' and image.get('size') == 'thumb' and not hasPosterThumb:
                    poster = self.theMovieDb.saveImage(image.get('url'), imageFile)
                    if poster: self.saveExtra(movieId, 'poster_thumb', poster)
                    hasPosterThumb = True

            handle.close()

    def saveExtra(self, id, name, value):

        exists = Db.query(MovieExtra).filter(and_(MovieExtra.movieId == id, MovieExtra.name == name)).first()
        if not exists:
            exists = MovieExtra()

            Db.add(exists)