def send_telegram_message(item): try: msg = 'π λ΄ λ€μ΄λ‘λ - AV μ²λ¦¬κ²°κ³Ό\n' msg += 'μ λͺ© : [%s] %s (%s)\n' % (item.code, item.title, item.date) msg += 'νμΌ : %s\n' % item.filename if item.download_status == 'true': status_str = 'β쑰건μΌμΉ - μμ²' elif item.download_status == 'false': status_str = 'βν¨μ€ ' elif item.download_status == 'no': status_str = 'μλ λ€μ΄λ‘λ μ¬μ©μν¨' elif item.download_status == 'true_only_status': status_str = 'β쑰건μΌμΉ - μνλ§' elif item.download_status == 'false_only_status': status_str = 'β쑰건λΆμΌμΉ - μνλ§' msg += 'κ²°κ³Ό : %s\n' % status_str msg += '%s/%s/list\n' % (SystemModelSetting.get('ddns'), package_name) msg += 'λ‘κ·Έ\n' + item.log ToolBaseNotify.send_message(msg, message_id='bot_downloader_av_result') except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc())
def start(bot_token): try: if TelegramBot.message_loop is None: TelegramBot.bot = Bot(bot_token) me = TelegramBot.bot.getMe() logger.debug('TelegramBot bot : %s', me) TelegramBot.message_loop = MessageLoop( TelegramBot.bot, TelegramBot.receive_callback) TelegramBot.message_loop.run_as_thread() ToolBaseNotify.send_message('ν λ κ·Έλ¨ λ©μμ§ μμ μ μμν©λλ€. %s' % (datetime.datetime.now())) TelegramBot.SUPER_BOT = Bot(TelegramBot.SUPER_TOKEN) if SystemModelSetting.get( 'ddns') == app.config['DEFINE']['MAIN_SERVER_URL']: logger.warning('ADMIN_TOKEN : %s ', TelegramBot.ADMIN_TOKEN) logger.warning('ADMIN_TOKEN : %s ', TelegramBot.ADMIN_TOKEN) logger.warning('ADMIN_TOKEN : %s ', TelegramBot.ADMIN_TOKEN) TelegramBot.ADMIN_BOT = Bot(TelegramBot.ADMIN_TOKEN) MessageLoop( TelegramBot.ADMIN_BOT, TelegramBot.super_receive_callback).run_as_thread() #TelegramBotHandle.super_sendMessage('κ΄λ¦¬λ΄μ΄ ν λ κ·Έλ¨ λ©μμ§ μμ μ μμνμμ΅λλ€.', encryped=False) pass while TelegramBot.message_loop is not None: time.sleep(60 * 60) except Exception as exception: logger.error('Exception:%s', exception) logger.error(traceback.format_exc())
def process_telegram_data(data): try: logger.debug('receive data') logger.debug(data) type_list = ModelSetting.get('cache_save_type_list').split('|') type_list = Util.get_list_except_empty(type_list) if len(type_list) == 0 or data['t'] in type_list: ret = ModelOffcloud2Cache.add(data) if ret is not None: logger.debug('Offcloud2 %s append' % ret.name) if ModelSetting.get_bool( 'cache_receive_info_send_telegram'): msg = 'π Offcloud2 μΊμ¬ μ 보 μμ \n' msg += 'Type : %s\n' % data['t'] msg += '%s\n' % data['n'] from system.model import ModelSetting as SystemModelSetting ddns = SystemModelSetting.get('ddns') url = '%s/%s/api/cache_download?id=%s' % ( ddns, package_name, ret.id) if SystemModelSetting.get_bool('auth_use_apikey'): url += '&apikey=%s' % SystemModelSetting.get( 'auth_apikey') msg += 'β 리λͺ¨νΈ λ€μ΄λ‘λ μΆκ°\n<%s>' % url ToolBaseNotify.send_message( msg, message_id='offcloud2_cache_receive') except Exception as e: logger.error(e) logger.error(traceback.format_exc())
def process_telegram_data(self, data, target=None): try: ret = ModelItem.process_telegram_data(data) logger.debug(data) #ret = None if ret is not None: if ModelSetting.get_bool('receive_send_notify'): msg = 'π AV μ 보 μμ \n' msg += 'μ λͺ© : [%s] %s (%s)\n' % (ret.code, ret.title, ret.date) msg += 'νμΌ : %s\n' % ret.filename msg += 'ν΄λ : %s\n' % ret.dirname msg += 'ν¬κΈ° : %s\n' % Util.sizeof_fmt(ret.total_size) url = '%s/%s/api/%s/add_download?id=%s' % (SystemModelSetting.get('ddns'), package_name, self.name, ret.id) if SystemModelSetting.get_bool('auth_use_apikey'): url += '&apikey=%s' % SystemModelSetting.get('auth_apikey') if app.config['config']['is_server']: msg += '\n' + ret.magnet + '\n' else: msg += '\nβ λ€μ΄λ‘λ μΆκ°\n<%s>\n' % url #msg += '\nβ λ€μ΄λ‘λ μΆκ°\n<%s>\n' % url poster = ret.poster if ModelSetting.get_bool('show_poster_notify') else None ToolBaseNotify.send_message(msg, image_url=poster, message_id='bot_downloader_av_receive') self.invoke() try: if app.config['config']['is_server']: from tool_expand import TorrentProcess TorrentProcess.receive_new_data(ret, package_name) except: pass except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc())
def process_send_alarm(message): try: bot_id = ModelSetting.get('bot_id') ToolBaseNotify.send_message(message, message_id=bot_id) except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc())
def receive_scan_result(id, filename): try: import plex #with db.session.no_autoflush: logger.debug('Receive Scan Completed : %s-%s', id, filename) #modelfile = db.session.query(ModelKtvFile).filter_by(id=int(id)).with_for_update().first() modelfile = db.session.query(ModelKtvFile).filter_by( id=int(id)).first() if modelfile is not None: modelfile.scan_status = 3 modelfile.scan_time = datetime.now() #if modelfile.plex_show_id == -1: plex.Logic.get_section_id(modelfile, more=True) #db.session.commit() if scheduler.is_running('ktv_process'): Logic.plex_update_list.append(modelfile) logger.debug('>> plex_update_list insert!!') else: db.session.add(modelfile) db.session.commit() logger.debug('>> direct commit!!') if ModelSetting.query.filter_by( key='telegram').first().value == 'True': text = '<PLEX μ€μΊ μλ£ - KTV>\n%s\n\n%s' % ( modelfile.filename, modelfile.plex_part) ToolBaseNotify.send_message( text, message_id='fileprocess_ktv_scan_completed') except Exception as exception: logger.debug('>>>>> receive_scan_result') logger.error('Exception:%s', exception) logger.error(traceback.format_exc())
def process_telegram_data(self, data, target=None): try: ret = ModelBotDownloaderKtvItem.process_telegram_data(data) #logger.debug(ret) if ret is not None: if ModelSetting.get_bool('receive_info_send_telegram'): msg = 'π TV μ 보 μμ \n' msg += 'μ λͺ© : %s\n' % data['filename'] if ret is None: msg += 'μ€λ³΅ λ§κ·Έλ·μ λλ€.' #TelegramHandle.sendMessage(msg) else: url = '%s/%s/api/%s/add_download?url=%s' % ( SystemModelSetting.get('ddns'), package_name, self.name, ret.magnet) if SystemModelSetting.get_bool('auth_use_apikey'): url += '&apikey=%s' % SystemModelSetting.get( 'auth_apikey') if app.config['config']['is_server']: msg += '\n' + ret.magnet + '\n' else: msg += '\nβ λ€μ΄λ‘λ μΆκ°\n<%s>\n' % url try: if ret.daum_id is not None: url = 'https://search.daum.net/search?w=tv&q=%s&irk=%s&irt=tv-program&DA=TVP' % ( py_urllib.quote( ret.daum_title.encode('utf8')), ret.daum_id) msg += '\nβ Daum μ 보\n%s' % url except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc()) ToolBaseNotify.send_message( msg, image_url=ret.daum_poster_url, message_id='bot_downloader_ktv_receive') self.invoke() try: if app.config['config']['is_server']: from tool_expand import TorrentProcess TorrentProcess.receive_new_data(ret, package_name) except: pass except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc())
def send_telegram(where, title): try: if LogicNormal.pre_telegram_title == title: return else: LogicNormal.pre_telegram_title = title if where == '0': msg = 'νΈλμ€λ―Έμ ' elif where == '1': msg = 'λ€μ΄λ‘λμ€ν μ΄μ ' elif where == '2': msg = 'νλΉν λ νΈ' elif where == '3': msg = 'aria2' msg += '\n%s λ€μ΄λ‘λ μλ£' % title ToolBaseNotify.send_message(msg, message_id='downloader_completed_remove') except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc())
def process_telegram_data(data): try: ret = ModelMovieItem.process_telegram_data(data) #logger.debug('telegram bot receive. is exist : %s', ret) if ret is not None: if ModelSetting.get_bool('receive_info_send_telegram'): msg = 'π μν μ 보 μμ \n' msg += 'μ λͺ© : %s (%s)\n' % (ret.movie_title, ret.movie_year) msg += 'νμΌ : %s\n' % ret.filename url = '%s/%s/api/add_download?id=%s' % (SystemModelSetting.get('ddns'), package_name, ret.id) if SystemModelSetting.get_bool('auth_use_apikey'): url += '&apikey=%s' % SystemModelSetting.get('auth_apikey') if app.config['config']['is_server']: msg += '\n' + ret.magnet + '\n' else: msg += '\nβ λ€μ΄λ‘λ μΆκ°\n<%s>\n' % url #msg += '\nβ λ€μ΄λ‘λ μΆκ°\n<%s>\n' % url try: if ret.movie_title is not None: if ret.movie_target == 'imdb': url = 'https://www.imdb.com/title/%s' % ret.movie_id msg += '\nβ IMDB μ 보\n%s' % url else: url = 'https://movie.daum.net/moviedb/main?movieId=%s' % (ret.movie_id) msg += '\nβ Daum μ 보\n%s' % url except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc()) ToolBaseNotify.send_message(msg, image_url=ret.daum_poster, message_id='bot_downloader_movie_receive') LogicNormal.invoke() try: if app.config['config']['is_server']: from tool_expand import TorrentProcess TorrentProcess.receive_new_data(ret, package_name) except: pass except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc())
def db_save(data, dest, match_type, is_moved): telegram_flag = ModelSetting.get_bool('telegram') try: entity = {} entity['name'] = data['search_name'] entity['fileName'] = data['name'] entity['dirName'] = data['fullPath'] entity['targetPath'] = dest entity['match_type'] = match_type if is_moved: entity['is_moved'] = 1 else: entity['is_moved'] = 0 ModelItem.save_as_dict(entity) if telegram_flag == 1: text = u'νμΌμ 리\n [%s] %s -> %s\n' % (match_type, data['fullPath'], dest) #import framework.common.notify as Notify #Notify.send_message(text, message_id = 'files_move_result') from tool_base import ToolBaseNotify ToolBaseNotify.send_message(text, message_id = 'files_move_result') except Exception as e: logger.error('Exxception:%s', e) logger.error(traceback.format_exc())
def send_telegram_message(self, item): try: telegram_log = 'π λ΄ λ€μ΄λ‘λ - TV\n' telegram_log += 'μ 보 : %s (%s), %sν, %s\n' % (item.daum_title, item.daum_genre, item.filename_number, item.filename_date) if item.download_status.startswith('True'): status_str = 'βμμ² ' elif item.download_status.startswith('False'): status_str = 'βν¨μ€ ' else: status_str = 'πλκΈ° ' if item.plex_key is not None: if item.plex_key.startswith('P'): status_str += '(PLEX νλ‘κ·Έλ¨β μνΌμλβ) ' elif item.plex_key.startswith('E'): status_str += '(PLEX νλ‘κ·Έλ¨β μνΌμλβ) ' else: status_str += '(PLEX νλ‘κ·Έλ¨β) ' if item.download_status == 'True_by_plex_in_lib_multi_epi': status_str += 'μνΌμλ λ©ν°νμΌ' elif item.download_status == 'False_by_plex_in_one_epi': status_str += 'μνΌμλ μ΄λ―Έ μμ' elif item.download_status == 'True_by_plex_in_lib_no_epi': status_str += 'μνΌμλ μμ' elif item.download_status == 'True_blacklist': status_str += 'λΈλ리μ€νΈμ μμ' elif item.download_status == 'False_whitelist': status_str += 'νμ΄νΈλ¦¬μ€νΈμ μμ' elif item.download_status == 'False_except_program': status_str += 'λΈλ리μ€νΈ' elif item.download_status == 'True_whitelist_program': status_str += 'νμ΄νΈλ¦¬μ€νΈ' elif item.download_status == 'True_whitelist_first_epi': status_str += '첫λ²μ§Έ μνΌμλ' elif item.download_status == 'False_no_meta': status_str += 'Daum κ²μ μ€ν¨' elif item.download_status == 'False_except_genre': status_str += 'λΈλ리μ€νΈ μ₯λ₯΄' elif item.download_status == 'True_whitelist_genre': status_str += 'νμ΄νΈλ¦¬μ€νΈ μ₯λ₯΄' elif item.download_status == 'False_not_allow_duplicate_episode': status_str += 'μ€λ³΅ μ μΈ' elif item.download_status == 'False_exist_download_quality': status_str += 'λμΌ νμ§ λ°μ' elif item.download_status == 'False_not_match_condition_quality': status_str += 'νμ§ μ‘°κ±΄ λΆμΌμΉ' elif item.download_status == 'False_not_match_condition_include_keyword': status_str += 'λ¨μ΄ ν¬ν¨ 쑰건' elif item.download_status == 'False_match_condition_except_keyword': status_str += 'λ¨μ΄ μ μΈ μ‘°κ±΄' telegram_log += 'κ²°κ³Ό : %s\n' % status_str telegram_log += 'νμΌλͺ : %s\n' % item.filename telegram_log += '%s/%s/list\n' % (SystemModelSetting.get('ddns'), package_name) #telegram_log += item.download_status + '\n' telegram_log += 'λ‘κ·Έ\n' + item.log ToolBaseNotify.send_message(telegram_log, message_id='bot_downloader_ktv_result') except Exception as e: logger.error('Exception:%s', e) logger.error(traceback.format_exc())
def process_download_file(self): setting_list = Util.db_list_to_dict( db.session.query(ModelSetting).all()) Logic._DOWNLOAD_PATH = setting_list['download_path'] except_partial = setting_list['except_partial'].split(',') except_genre_remove_epi_number = [ x.strip() for x in setting_list['except_genre_remove_epi_number'].split(',') ] if '' in except_genre_remove_epi_number: except_genre_remove_epi_number.remove('') library_list = db.session.query(ModelKtvLibrary).all() Logic._LIBRARY_ROOT_LIST = [] for item in library_list: if item.library_type == 0: drive_type = EntityLibraryPathRoot.DriveType.LOCAL else: drive_type = EntityLibraryPathRoot.DriveType.RCLONE lib = EntityLibraryPathRoot(drive_type=drive_type, mount_path=item.library_path, rclone_path=item.rclone_path, depth=2, replace_for_plex=[ item.replace_for_plex_source, item.replace_for_plex_target ]) Logic._LIBRARY_ROOT_LIST.append(lib) dir_list = None path = Logic._DOWNLOAD_PATH list_ = os.listdir(Logic._DOWNLOAD_PATH) logger.debug('process_download_file 2') logger.debug('list : %s', len(list_)) # rclone μμμ΄ νμνκ°.. flag_rclone_start = False for var in list_: try: #with db.session.no_autoflush: if True: abspath = os.path.join(path, var) telegram_log = None entity = None if os.path.isfile(abspath): if Logic.check_except_partial(var, except_partial): continue telegram_log = package_name + '\n%s\n' % abspath if dir_list is None: logger.debug('process_download_file') # λ°©μ‘μ΄λ¦ λͺ©λ‘ dir_list = Logic._make_dir_list() logger.debug('process_download_file 1') logger.debug('===================================') logger.debug('File Process: %s', var) # λ°©μ‘μ© νμΌμ΄ μλλ©΄ entity = EntityShow(var, nd_download_path=path, except_genre_remove_epi_number= except_genre_remove_epi_number) if entity.video_type == EntityShow.VideoType.KOREA_TV: logger.debug('<Move>') _find_dir = Logic._get_find_dir(dir_list, entity) if len(_find_dir) == 1: entity.set_find_library_path(_find_dir[0]) logger.debug(' - νλμ ν΄λ μ νλ¨ : %s', _find_dir[0].abspath) entity.move_file() # 2019-09-01 if entity.scan_status == EntityShow.ScanStatus.MOVED and entity.nd_find_library_path.entity_library_root.drive_type == EntityLibraryPathRoot.DriveType.LOCAL: if app.config['config']['use_celery']: self.update_state( state='PROGRESS', meta={ 'filename': entity.move_abspath_local }) else: Logic.send_to_listener( entity.move_abspath_local) elif entity.scan_status == EntityShow.ScanStatus.MOVED and entity.nd_find_library_path.entity_library_root.drive_type == EntityLibraryPathRoot.DriveType.RCLONE: if app.config['config']['use_celery']: self.update_state( state='PROGRESS', meta={ 'filename': entity.move_abspath_cloud }) else: Logic.send_to_listener( entity.move_abspath_cloud) entity.modelfile = ModelKtvFile.create(entity) logger.debug(entity.log) # IDλ₯Ό 보λ΄μΌνκΈ° λλ¬Έμ commit db.session.add(entity.modelfile) db.session.commit() if entity.scan_status == EntityShow.ScanStatus.MOVED: try: import plex plex.Logic.send_scan_command( entity.modelfile, package_name) except Exception as exception: logger.debug('NOT IMPORT PLEX!!') db.session.add(entity.modelfile) db.session.commit() if entity.move_type == EntityLibraryPathRoot.DriveType.RCLONE: flag_rclone_start = True elif len(_find_dir) > 1: logger.debug(' - μ νλ ν΄λκ° 2κ° μ΄μ') logger.debug(' %s', _find_dir[0].abspath) logger.debug(' %s', _find_dir[1].abspath) entity.log += '<νμΌμ΄λ>\n' entity.log += 'μ νλ ν΄λ %sκ°\n' % (len(_find_dir)) entity.log += ' %s\n' % _find_dir[0].abspath entity.log += ' %s\n' % _find_dir[1].abspath tmp = os.path.join( Logic._DOWNLOAD_PATH, setting_list['manual_folder_name']) if not os.path.isdir(tmp): os.mkdir(tmp) if os.path.exists(os.path.join(tmp, var)): os.remove(os.path.join(tmp, var)) shutil.move(abspath, tmp) if app.config['config']['use_celery']: self.update_state(state='PROGRESS', meta={ 'filename': os.path.join( tmp, var) }) else: Logic.send_to_listener( os.path.join(tmp, var)) entity.log += ' %s μ΄λ\n' % tmp elif not _find_dir: #continue logger.debug(' - μ νλ ν΄λ μμ') entity.log += '<νμΌμ΄λ>\n' entity.log += 'μ νλ ν΄λ μμ\n' flag_move = False if entity.daum_info is None: #if FileManagerSetting.HOW_TO_PROCESS_NO_DAUM_FILE == 'MOVE_ON_NOMETA_DIR_IN_DOWNLOAD_DIR': # tmp = os.path.join(Logic._DOWNLOAD_PATH, self._NO_DAUM_INFO_MOVE_DIRECTORY_NAME ) # if not os.path.isdir(tmp): # os.mkdir(tmp) # shutil.move(abspath, tmp) # flag_move = True #elif FileManagerSetting.HOW_TO_PROCESS_NO_DAUM_FILE == 'PROCESS_LIKE_AS_GENRE': #daum = EntityDaumTV(-1) try: import daum_tv daum = daum_tv.ModelDaumTVShow(-1) daum.genre = setting_list[ 'no_daum_folder_name'] daum.title = entity.filename_name except Exception as exception: logger.error('Exception:%s', exception) logger.error(traceback.format_exc()) daum = None entity.daum_info = daum if flag_move == False and entity.daum_info: flag_search = False for library_root in Logic._LIBRARY_ROOT_LIST: for _ in library_root.get_genre_list(): if _ == entity.daum_info.genre: #ν΄λλ§ λ§λ€μ΄μ£Όκ³ λ€μλ²μ μ΄λ. 첫λ²μ§Έ μ νλκ³³λ§ tmp = os.path.join( library_root.mount_path, _, Util. change_text_for_use_filename( entity.daum_info.title) ) if not os.path.isdir(tmp): logger.debug( 'mkdir:%s', tmp) os.mkdir(tmp) entity.log += 'ν΄λμμ± : %s\n' % tmp logger.debug( ' * μ₯λ₯΄:%s [%s] ν΄λ μμ±. λ€μ νμμ μ΄λ', _, tmp) flag_search = True break if flag_search: break if not flag_search: logger.debug(' * μ₯λ₯΄:%s μμ.', entity.daum_info.genre) # TODO μ΅μ # λ‘컬μ μ₯λ₯΄ λ§λ λ€ """ _ = os.path.join(self._DOWNLOAD_PATH, 'tmp') if not os.path.isdir(_): os.mkdir(_) """ # λ€μλ²μ λ°©μ‘λͺ ν΄λλ₯Ό λ§λ λ€??? _ = os.path.join( Logic._LIBRARY_ROOT_LIST[0]. mount_path, entity.daum_info.genre) if not os.path.isdir(_): os.mkdir(_) entity.log += 'μ₯λ₯΄ ν΄λμμ± : %s\n' % _ telegram_log += entity.log logger.debug('===================================') # μ²λ¦¬νμ§ λͺ»νλ ν¬λ©§μ νμΌ else: tmp = os.path.join( Logic._DOWNLOAD_PATH, setting_list['not_ktv_move_folder_name']) if not os.path.isdir(tmp): os.mkdir(tmp) if os.path.exists(os.path.join(tmp, var)): os.remove(os.path.join(tmp, var)) shutil.move(abspath, tmp) if app.config['config']['use_celery']: self.update_state( state='PROGRESS', meta={'filename': os.path.join(tmp, var)}) else: Logic.send_to_listener(os.path.join(tmp, var)) telegram_log += 'μ²λ¦¬νμ§ λͺ»νλ νμΌ νμ\nμ΄λ:%s\n' % tmp else: #ν΄λ tmp = var + '.mp4' match_flag = False for regex in EntityShow._REGEX_FILENAME: match = re.compile(regex).match(tmp) if match: match_flag = True break if match_flag: try: childs = os.listdir(abspath) # λͺ¨λ νμΌμΈ κ²½μ°μλ§ μ²λ¦¬ # 2019-07-13 for c in childs: if os.path.isdir(os.path.join(abspath, c)): continue for c in childs: tmp = os.path.join(abspath, c) if os.stat(tmp).st_size < 1000000: os.remove(tmp) else: if os.path.exists(os.path.join( path, c)): if os.stat( os.path.join(path, c) ).st_size >= os.stat(tmp).st_size: os.remove(tmp) else: os.remove(os.path.join( path, c)) shutil.move(tmp, path) else: shutil.move(tmp, path) shutil.rmtree(abspath) except Exception as exception: logger.error('Exception:%s', exception) logger.error(traceback.format_exc()) except Exception as exception: try: db.session.rollback() logger.debug( 'ROLLBACK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') except: logger.debug('>>>>>>>>>>>>>>>>>>>>>>>>>>') logger.error('Exception:%s', exception) logger.error(traceback.format_exc()) finally: try: if ModelSetting.query.filter_by(key='telegram').first( ).value == 'True' and telegram_log is not None: img = None if entity is not None and entity.daum_info is not None and entity.daum_info.poster_url is not None: img = entity.daum_info.poster_url ToolBaseNotify.send_message( telegram_log, image_url=img, message_id='fileprocess_ktv_result') except Exception as exception: logger.error('Exception:%s', exception) logger.error(traceback.format_exc()) logger.debug('flag_rclone_start : %s', flag_rclone_start) #self.update_state(state='PROGRESS',meta={'flag_rclone_start':flag_rclone_start}) #if flag_rclone_start: # scheduler.execute_job('rclone') Logic.check_library_completed() return flag_rclone_start