# 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/cron/base.py
# class cronBase(threading.Thread):
# 
#     abort = False
#     running = False
#     debug = False
# 
#     def isRunning(self):
#         return self.running
# 
#     def quit(self):
#         self.abort = True
# 
#     def canShutdown(self):
#         return (self.abort and not self.running)


from app.config.cplog import CPLog
from app.config.db import Movie, MovieETA, Session as Db
from app.lib.cron.base import cronBase
from app.lib.provider.rss import rss
from dateutil.parser import parse
from sqlalchemy.sql.expression import or_
from urllib import urlencode
import Queue
import cherrypy
import time
import json
                if queue.get('id'):
                    movie = Db.query(Movie).filter_by(id = queue.get('id')).first()
                else:
                    movie = queue.get('movie')

                if not cherrypy.config.get('debug'):
                    #do a search
                    self.running = True
                    result = self.search(movie)
                    if result:
                        self.save(movie, result)
                    self.running = False

                etaQueue.task_done()
                time.sleep(timeout)
            except Queue.Empty:
                pass

        log.info('ETA thread shutting down.')

    def all(self):
        activeMovies = Db.query(Movie).filter(or_(Movie.status == u'want', Movie.status == u'waiting')).all()
        for movie in activeMovies:
            etaQueue.put({'movie':movie})

    def save(self, movie, result):

        row = Db.query(MovieETA).filter_by(movieId = movie.id).first()
        if not row:
            row = MovieETA()

# 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/cron/base.py
# class cronBase(threading.Thread):
# 
#     abort = False
#     running = False
#     debug = False
# 
#     def isRunning(self):
#         return self.running
# 
#     def quit(self):
#         self.abort = True
# 
#     def canShutdown(self):
#         return (self.abort and not self.running)


# Path: app/lib/cron/eta.py
from app.config.cplog import CPLog
from app.config.db import Movie, MovieETA, Session as Db
from app.lib.cron.base import cronBase
from app.lib.provider.rss import rss
from dateutil.parser import parse
from sqlalchemy.sql.expression import or_
from urllib import urlencode
import Queue
import cherrypy
import time
import json

                if queue.get('id'):
                    movie = Db.query(Movie).filter_by(id = queue.get('id')).first()
                else:
                    movie = queue.get('movie')

                if not cherrypy.config.get('debug'):
                    #do a search
                    self.running = True
                    result = self.search(movie)
                    if result:
                        self.save(movie, result)
                    self.running = False

                etaQueue.task_done()
                time.sleep(timeout)
            except Queue.Empty:
                pass

        log.info('ETA thread shutting down.')

    def all(self):
        activeMovies = Db.query(Movie).filter(or_(Movie.status == u'want', Movie.status == u'waiting')).all()
        for movie in activeMovies:
            etaQueue.put({'movie':movie})

    def save(self, movie, result):

        row = Db.query(MovieETA).filter_by(movieId = movie.id).first()
        if not row:
            row = MovieETA()

            Db.add(row)