def __init__(self, max_download_size, download_path, lang, initial_state): self.db_manager = Database.get_instance() self.scheduler = CronJobManager.get_instance() self.logger = logging.getLogger(__name__) self.download_path = utils.get_abs_path(download_path) self.max_download_size = max_download_size self.lang = lang self.initial_state = initial_state self.PROCESS_PASSWORD = range( initial_state, initial_state + BookDownloadHandler.num_states ) self.entry_points = [CallbackQueryHandler( self.callback, pass_user_data=True )] self.states = { self.PROCESS_PASSWORD: [RegexHandler( '.*', self.process_password, pass_user_data=True )] } self.fallbacks=[RegexHandler('3248BC7547CE97B2A197B2A06CF7283D', self.cancel)] ConversationHandler.__init__( self, entry_points=self.entry_points, states=self.states, fallbacks=self.fallbacks, per_chat=False )
def __init__(self): if CronJobManager.__instance != None: raise Exception("This class is a singleton!") else: self.update_cron_start_time() self.db_manager = Database.get_instance() self.dmm_ripper = DMMRipper.get_instance( CronJobManager.webdriver_config) jobstores = { # 'alchemy': SQLAlchemyJobStore(url='sqlite:///jobs.sqlite'), 'default': MemoryJobStore() } executors = { 'default': { 'type': 'threadpool', 'max_workers': 20 }, 'processpool': ProcessPoolExecutor(max_workers=5) } job_defaults = {'coalesce': False, 'max_instances': 3} self.scheduler = BackgroundScheduler() self.scheduler.configure(jobstores=jobstores, executors=executors, job_defaults=job_defaults, timezone=CronJobManager.time_zone, daemon=False) self.scheduler.start() CronJobManager.__instance = self
def __init__(self, lang, language_codes, initial_state): self.db_manager = Database.get_instance() self.scheduler = CronJobManager.get_instance() self.logger = logging.getLogger(__name__) self.lang = lang self.language_codes = language_codes self.initial_state = initial_state self.LANGUAGE, self.EMAIL, self.PASSWORD, self.STORE_PASS = range( initial_state, initial_state + StartWizard.num_states) self.entry_points = [CommandHandler('start', self.start)] self.states = { self.LANGUAGE: [RegexHandler('.*', self.language)], self.EMAIL: [RegexHandler('.*', self.email)], self.STORE_PASS: [RegexHandler('.*', self.save_credentials)], self.PASSWORD: [RegexHandler('.*', self.password)], } self.fallbacks = [ CommandHandler('10aec35353f9c4096a71c38654c3d402', self.cancel) ] ConversationHandler.__init__(self, entry_points=self.entry_points, states=self.states, fallbacks=self.fallbacks)
def download_book_pages_job(book_path, missing_images, start_toc_missing, book): db_manager = Database.get_instance() db_session = db_manager.create_session() dmm_cookies = None CronJobManager.logger.info('Starting download job of book %s', book.id) db_manager.set_volume_now_downloading(db_session, book.id, True) num_missing_images = len(missing_images) CronJobManager.notify_subscribers_download_progress( book, book.pages - num_missing_images, start_toc_missing, start_toc_missing=start_toc_missing) dmm_cookies = CronJobManager.get_dmm_cookies_for_book_download(book) download_failed = False if dmm_cookies: toc_path = path.join(book_path, 'toc.txt') if start_toc_missing: try: CronJobManager.__instance.dmm_ripper.download_book_toc( \ book, toc_path) is_toc_missing = False CronJobManager.notify_subscribers_download_progress( book, book.pages - num_missing_images, is_toc_missing, edit_message=True) except Exception as e: CronJobManager.logger.exception(e) CronJobManager.__instance.dmm_ripper.close_broser_reader() is_toc_missing = True else: is_toc_missing = False for index, page_num in enumerate(missing_images): try: CronJobManager.__instance.dmm_ripper.download_book_page( \ book, page_num, \ path.join(book_path, '{}'.format(page_num)) ) except: CronJobManager.__instance.dmm_ripper.close_broser_reader() download_failed = True CronJobManager.notify_subscribers_download_progress( book, book.pages - num_missing_images + index + 1, is_toc_missing, start_toc_missing=start_toc_missing, edit_message=True) CronJobManager.__instance.dmm_ripper.close_broser_reader() CronJobManager.logger.info('Download of book %s has finished', book.id) for subscriber in CronJobManager.book_job[book.id]['download']: user = subscriber['user'] subscriber['bot'].send_message( chat_id=user.id, text=CronJobManager.lang[user.language_code] \ ['download_finished'].format( FileFormat(user.file_format).name.upper() ) ) CronJobManager.__instance.subscribe_to_book_conversion( \ book, book_path, subscriber['user'], subscriber['bot'], \ from_download=True ) if not dmm_cookies or download_failed: CronJobManager.logger.info('Unable to start the download of ' \ + 'book %s', book.id) for subscriber in CronJobManager.book_job[book.id]: user = subscriber['user'] CronJobManager.logger.info('Sending download error message ' \ + 'to subscriber %s', user.id) subscriber['bot'].send_message( chat_id = user.id, text = CronJobManager.lang[user.language_code] \ ['download_error'] ) db_manager.set_volume_now_downloading(db_session, book.id, False) CronJobManager.logger.info('Removing the registration of download ' \ + 'job for book %s', book.id) CronJobManager.book_job[book.id]['download'] = []
def cache_user_library(user, session=None, password=None, fast=False): db_manager = Database.get_instance() db_session = db_manager.create_session() db_manager.set_user_now_caching(db_session, user.id, True) CronJobManager.logger.info('Caching %s user\'s library', user.id) try: if session == None: if user.save_credentials: password = user.password session = CronJobManager.__instance.dmm_ripper.get_session( user.email, password, fast) CronJobManager.logger.info('Obtaining a new DMM session for ' \ + 'user %s', user.id) books = CronJobManager.__instance.dmm_ripper.get_purchased_books( session) db_session.add(user) for book in books: if book['series']: serie = db_manager.get_manga_serie(db_session, book['url']) if not serie: serie = MangaSeries(title=book['name'], url=book['url'], thumbnail_dmm=book['thumbnail']) db_session.add(serie) CronJobManager.logger.info( 'Adding a new serie to DB: ' + '%s', serie.title) CronJobManager.thumbnail(db_session, serie, db_manager) CronJobManager.logger.info('Processing volumes of ' \ + 'series %s', serie.title) volumes = CronJobManager.__instance.dmm_ripper \ .get_book_volumes( session, book ) for volume in volumes: db_volume = db_manager.get_manga_volume( db_session, volume['url']) if not db_volume: volume_details = CronJobManager.__instance \ .dmm_ripper.get_book_details( session, volume['details_url'] ) db_volume = Manga( title=volume['name'], url=volume['url'], thumbnail_dmm=volume['thumbnail'], description=volume_details['description'], pages=volume_details['pages'], serie=serie) db_session.add(db_volume) CronJobManager.logger.info('Adding a new volume ' \ + ' to DB: %s', db_volume.title) CronJobManager.thumbnail(db_session, db_volume, db_manager, parent=serie) if not db_manager.user_owns_volume( db_session, user.id, db_volume.url): CronJobManager.logger.info('Adding volume to ' \ + 'user %s', user.id) try: user.book_collection.append(db_volume) db_manager.commit(db_session) except Exception as e: CronJobManager.logger.exception('Error ' \ + 'adding volume to user %s', user.id) db_manager.rollback(db_session) else: book = db_manager.get_manga_volume(db_session, book['url']) if not book: book_details = CronJobManager.__instance.dmm_ripper \ .get_book_details( session, book['details_url'] ) book = Manga(title=book['name'], url=book['url'], thumbnail_dmm=book['thumbnail'], description=book_details['description'], pages=book_details['pages'], serie=serie) db_session.add(book) CronJobManager.logger.info('Adding a new non series ' \ + 'book to DB: %s', book.title) CronJobManager.thumbnail(db_session, book, db_manager) if not db_manager.user_owns_volume(db_session, user.id, book.url): CronJobManager.logger.info('Adding non series book ' \ + 'to user %s', user.id) try: user.book_collection.append(book) db_manager.commit(session) except: CronJobManager.logger.exception('Error adding ' \ + 'non series book to user %s', user.id) db_manager.rollback(session) db_manager.set_user_cache_expire_date( db_session, user.id, CronJobManager.get_cache_expire_date()) db_manager.set_user_cache_built(db_session, user.id, True) db_manager.set_user_login_error(db_session, user.id, False) except Exception as e: CronJobManager.logger.info('Unable to login to the DMM account ' \ + 'of user %s', user.id) db_manager.set_user_login_error(db_session, user.id, True) CronJobManager.remove_scheduled_user_cache(user.id) CronJobManager.logger.exception(e) finally: CronJobManager.logger.info('%s user\'s library caching ended', user.id) db_manager.set_user_now_caching(db_session, user.id, False) db_manager.remove_session()
def __init__(self, lang): self.lang = lang self.db_manager = Database.get_instance() InlineQueryHandler.__init__(self, self.inline_query, pass_chat_data=True)
import logging import signal import sys import utilities as utils logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logging.Formatter.converter = utils.logging_tz logger = logging.getLogger(__name__) lang = {'en': english.en, 'ja': japanese.ja, 'common': common.common} language_codes = {'en': ['english', '英語'], 'ja': ['japanese', '日本語']} db_manager = Database.get_instance() scheduler = CronJobManager.get_instance( languages=lang, max_upload_size=Config.MAX_UPLOAD_SIZE, \ webdriver_config=Config.WEBDRIVER ) scheduler.set_download_path(Config.DOWNLOAD_PATH) def error_callback(bot, update, error): logger.exception(error) def shutdown(): scheduler.close_dmm_ripper()
def __init__(self, lang, language_codes, initial_state): self.db_manager = Database.get_instance() self.scheduler = CronJobManager.get_instance() self.lang = lang self.language_codes = language_codes self.initial_state = initial_state self.CONFIG, self.PROCESS_LANGUAGE, self.PROCESS_ACCOUNT, \ self.PROCESS_LIBRARY, self.PROCESS_EMAIL, self.PROCESS_PASSWORD, \ self.PROCESS_CREDENTIALS, self.PROCESS_UPDATE_LIBRARY_PASSWORD, \ self.PROCESS_FILE_FORMAT = range( initial_state, initial_state + ConfigWizard.num_states ) self.entry_points = [ CommandHandler('config', self.request_config_menu) ] self.states = { self.CONFIG: [RegexHandler( self.mc_ml_regex(['language','account', 'library', 'exit']), self.config_menu )], self.PROCESS_LANGUAGE: [RegexHandler( '.*', self.process_language )], self.PROCESS_ACCOUNT: [RegexHandler( self.mc_ml_regex( [ 'email', 'password', 'save_credentials', 'back' ] ), self.account )], self.PROCESS_LIBRARY: [RegexHandler( self.mc_ml_regex( [ 'update_library', 'file_format', 'back' ] ), self.process_library )], self.PROCESS_EMAIL: [RegexHandler( '.*', self.process_email )], self.PROCESS_PASSWORD: [RegexHandler( '.*', self.process_password )], self.PROCESS_CREDENTIALS: [RegexHandler( self.mc_ml_regex(['yes', 'no']), self.process_save_credentials )], self.PROCESS_UPDATE_LIBRARY_PASSWORD: [RegexHandler('.*', self.process_password_update_library )], self.PROCESS_FILE_FORMAT: [RegexHandler( '^(' + self.lang['common']['zip'] + '|' \ + self.lang['common']['pdf'] + '|' \ + self.lang['common']['epub'] + ')$', self.process_file_format )] } self.fallbacks = [ RegexHandler('3248BC7547CE97B2A197B2A06CF7283D', self.cancel) ] ConversationHandler.__init__(self, entry_points=self.entry_points, states=self.states, fallbacks=self.fallbacks)