def end_chain(self, data: ParserData): if self.year is None: self.messages.append( send_message( ComponentType.PARSER, { 'user_id': data.client_id, 'message_text': 'В следующий раз добавляй год к поиску, для более точного поиска', 'choices': [] })) if self.serial: if self.season is None: self.messages.append( send_message( ComponentType.PARSER, { 'user_id': data.client_id, 'message_text': 'Для поиска сериала, необходимо указать сезон!' ' Пример: /serial Игра престолов 2011 сезон 1', 'choices': [] })) if (self.serial and self.season is not None) or not self.serial: self.messages.append( parser_message(ComponentType.PARSER, self.next_data, data.client_id)) return self.messages
def add_user(cls, data: CommandData, db_manager: DbManager, config): """ Add user to base :return: """ messages = [] session = db_manager.get_session() user = db_manager.find_user(data.command_data['client_id'], session) if user is not None: message_text = 'Пользователь с id:{} уже есть в базе.'.format(data.command_data['client_id']) messages.append( send_message( ComponentType.COMMAND_HANDLER, { 'user_id': data.client_id, 'message_text': message_text, 'choices': [] } ) ) session.close() return messages db_manager.add_user( data.command_data['client_id'], data.command_data['name'], data.command_data['last_name'], data.command_data['nick'], session=session, ) session.close() message_text = 'Пользователь с id:{} добавлен.'.format(data.command_data['client_id']) messages.append( send_message( ComponentType.COMMAND_HANDLER, { 'user_id': data.client_id, 'message_text': message_text, 'choices': [] } ) ) messages.append( send_message( ComponentType.COMMAND_HANDLER, { 'user_id': data.command_data['client_id'], 'message_text': 'Теперь ты в теме.', 'choices': [] } ) ) return messages
def end_chain(self, data): text_type = 'Фильм' if data.data[ 'media_type'].value == MediaType.FILMS.value else 'Сериал' title = data.data['title'] if data.data['media_status'] != LockingStatus.ENDED: message_text = f'{text_type} {title} уже ищется.' else: message_text = f'Поиск {text_type}а по запросу {title} завершен.' self.messages.append( send_message( ComponentType.PARSER, { 'user_id': data.client_id, 'message_text': message_text, 'choices': [] })) self.messages.append( command_message( ComponentType.PARSER, ClientCommands.ADD_MEDIA_TO_USER_LIST, { 'kinopoisk_id': data.data['kinopoisk_id'], 'media_type': MediaType.FILMS if 'serial' in data.data.keys() and not data.data['serial'] else MediaType.SERIALS, 'season': data.data['season'] if 'season' in data.data.keys() else 0, }, data.client_id, )) return self.messages
def end_chain(self, data: ParserData) -> list: action_name = 'kinopoisk' if self.next_data.get('choices') is None or len( self.next_data['choices']) == 0: message_text = 'В кинопоиске, по запросу {0}, ' \ 'ничего не найдено, уточни свой запрос.'.format(data.data['query']) self.next_data['choices'] = [] else: message_text = 'В кинопоиске, по запросу {0}, ' \ 'найдено более одного совпадения, выбери, что скачать.'.format(data.data['query']) choice_list = [] a = 0 for elem in self.next_data['choices']: elem.update({'action': action_name}) choice_list.append({ 'message_text': elem['url'], 'button_text': str(a), 'call_back_data': elem, }) a += 1 choices = {'action': action_name, 'data': choice_list} self.messages.append( send_message( ComponentType.PARSER, { 'user_id': data.client_id, 'message_text': message_text, 'choices': choices })) return self.messages
def add_serial(cls, data: CommandData, db_manager: DbManager, config): session = db_manager.get_session() serial = db_manager.add_serial( data.client_id, data.command_data['kinopoisk_id'], data.command_data['title'],data.command_data['year'], data.command_data['season'], data.command_data['url'], data.command_data['series'], session=session, cover_url=data.command_data['cover_url'] ) message_text = 'Сериал {0} сезон {1} добавлен к поиску \n {2}'.format( data.command_data['title'], data.command_data['season'], data.command_data['url'] ) messages = [ send_message( ComponentType.COMMAND_HANDLER, {'user_id': data.client_id, 'message_text': message_text, 'choices': []} ), crawler_message( ComponentType.COMMAND_HANDLER, data.client_id, { 'media_id': serial.kinopoisk_id, 'media_type': MediaType.SERIALS, 'season': data.command_data['season'] } ) ] session.close() return messages
def add_film(cls, data: CommandData, db_manager: DbManager, config): session = db_manager.get_session() film = db_manager.add_film( data.client_id, data.command_data['kinopoisk_id'], data.command_data['title'], data.command_data['year'], data.command_data['url'], session=session, cover_url=data.command_data['cover_url'] ) message_text = 'Фильм {0} добавлен к поиску \n {1}'.format( data.command_data['title'], data.command_data['url'] ) messages = [send_message( ComponentType.COMMAND_HANDLER, { 'user_id': data.client_id, 'message_text': message_text, 'choices': [] } ), crawler_message( ComponentType.COMMAND_HANDLER, data.client_id, { 'media_id': film.kinopoisk_id, 'media_type': MediaType.FILMS } )] session.close() return messages
def auth_query(cls, data: CommandData, db_manager: DbManager, config): """ Query for authentication from new user/ :param data: :param db_manager: :return: """ messages = [] session = db_manager.get_session() if db_manager.is_admin(data.client_id): messages.append( command_message( ComponentType.COMMAND_HANDLER, ClientCommands.ADD_DATA_USER, { 'client_id': data.command_data['client_id'], 'name': data.command_data['name'], 'last_name': data.command_data['last_name'], 'nick': data.command_data['nick'] }, data.client_id ) ) else: message_text = 'У тебя нет прав на добавление пользователей.' messages.append( send_message( ComponentType.COMMAND_HANDLER, { 'user_id': data.client_id, 'message_text': message_text, 'choices': [] } ) ) session.close() return messages
def result(self): try: data = self.returned_data.get(block=False) except Empty: data = None if data is None: if self.ended: logger.debug('Поиск по задаче {0} не дал результата'.format( self.job)) if not self.job.action_type.value == ActionType.FORCE_CHECK.value: return [] message_text = '{0} по запросу {1} не найден, ' \ 'но я буду искать его непрестанно.'\ .format('Фильм' if self.job.season == '' else 'Сериал', self.job.text_query) return [ send_message( ComponentType.CRAWLER, { 'user_id': self.job.client_id, 'message_text': message_text, 'choices': [] }) ] else: return [] messages = [] cmd_message = success_message(data, self.job) messages.append(cmd_message) return messages
def serial_success_message(data, job): action_name = 'select_torrent' choice_list = [] a = 0 for elem in data: call_back_data = {} add_media_keys(job, call_back_data) call_back_data.update({ 'action': action_name, 'download_url': elem.url, 'theam_id': elem.theam_url, 'torrent_tracker': elem.tracker, }) choice_list.append({ 'message_text': '{0}'.format(elem.theam_url), 'button_text': str(a), 'call_back_data': call_back_data }) a += 1 choices = {'action': action_name, 'data': choice_list} return send_message( ComponentType.PARSER, { 'user_id': job.client_id, 'message_text': f'Уточни какой именно торрент стоит скачать по запросу {job.text_query} ' f'(звук, качество и т.д.).', 'choices': choices })
def construct_send_message(cls, user_id, message_text, choices): return send_message( ComponentType.COMMAND_HANDLER, { 'user_id': user_id, 'message_text': message_text, 'choices': choices, } )
def end_chain(self, data): message_text = '{1} {0} уже есть на plex.'.format( data.data['title'], 'Фильм' if 'serial' in data.data.keys() and not data.data['serial'] else 'Сериал') self.messages.append( send_message( ComponentType.PARSER, { 'user_id': data.client_id, 'message_text': message_text, 'choices': [] })) return self.messages
def exsecute_command(cls, message_data: CommandData, db_manager, config): if not cls.check_rule(message_data.client_id, db_manager): return [send_message( ComponentType.COMMAND_HANDLER, { 'user_id': message_data.client_id, 'message_text': 'Для авторизации скинь свой id Морозу. Вот он: {}'.format(message_data.client_id), 'choices': [] } )] command_dict = cls.get_command_list() message = command_dict[message_data.command.value](message_data, db_manager, config) result = [] if isinstance(message, MediatorActionMessage): result.append(message) elif isinstance(message, list): result = message return result
def set_user_option(cls, data: CommandData, db_manager: DbManager, config): """ Change some user option :return: """ messages = [] session = db_manager.get_session() new_value = db_manager.change_user_option(data.client_id, data.command_data['option'], session=session) message_text = 'Статус оповещения установлен. Новое значение "{}"'.format( 'оповещать' if new_value == 1 else 'не оповещать' ) messages.append( send_message( ComponentType.COMMAND_HANDLER, { 'user_id': data.client_id, 'message_text': message_text, 'choices': [] } ) ) session.close() return messages
def watcher_messages(job, data): messages = [] torrent_inform = data['torrent_information'] if torrent_inform['progress'] == 100: message_text = 'Скачивание {} завершено, беги скорей на плекс.'.format( job.text_query) choices = [] messages += [ command_message(ComponentType.CRAWLER, ClientCommands.UPDATE_PLEX_LIB, {}, job.client_id) ] else: message_text = 'Прогресс скачивания {0}: {1}% {2}/{3}'.format( job.text_query, torrent_inform['progress'], convert_size(torrent_inform['total_done']), convert_size(torrent_inform['total_size']), ) if 'key_board' in job.crawler_data.data.keys( ) and not job.crawler_data.data['key_board']: choices = [] else: data = {} add_media_keys(job, data) data.update({ 'force': True, }) choices = {'action': 'download_callback', 'data': data} messages.append( send_message( ComponentType.CRAWLER, { 'user_id': job.client_id, 'message_text': message_text, 'choices': choices })) return messages