def wrapper(update: Update, context: CallbackContext): try: return func(update, context) except Exception as e: log.exception('Error: %s\nUpdate: %s', context.error, update) Error.create_from(func, e, update) if update: reply_error(ERROR_TEXT, update, context)
def on_update_quote(update: Update, context: CallbackContext): r""" Обновление цитаты в базе с сайта: - /update_quote (\d+) - update[ _]quote (\d+) """ quote_id = get_quote_id(context) if not quote_id: reply_error('Номер цитаты не указан', update, context) return update_quote(quote_id, update, context, log)
def on_error(update: Update, context: CallbackContext): log.error('Error: %s\nUpdate: %s', context.error, update, exc_info=context.error) db.Error.create_from(on_error, context.error, update) # Не отправляем ошибку пользователю при проблемах с сетью (типа, таймаут) if isinstance(context.error, NetworkError): return if update: reply_error(ERROR_TEXT, update, context)
def on_get_used_quote_in_requests(update: Update, context: CallbackContext): r""" Получение порядка вызова указанной цитаты у текущего пользователя: - /get_used_quote <номер цитаты> - get used quote <номер цитаты> - <номер цитаты> """ quote_id = get_quote_id(context) if not quote_id: reply_error('Номер цитаты не указан', update, context) return user_id = update.effective_user.id reply_get_used_quote(user_id, quote_id, update, context)
def on_get_external_quote(update: Update, context: CallbackContext): """ Получение цитаты из сайта: - /get_external_quote <номер цитаты> - get external quote <номер цитаты> """ quote_id = get_quote_id(context) if not quote_id: reply_error('Номер цитаты не указан', update, context) return quote_obj = bash_im.Quote.parse_from(quote_id) if not quote_obj: reply_error(f'Цитаты #{quote_id} на сайте нет', update, context) return reply_quote(quote_obj, update, context)
def reply_local_quote(update: Update, context: CallbackContext, quote_id: int = None, **kwargs) -> Optional[db.Quote]: if not quote_id: quote_id = get_quote_id(context) if not quote_id: reply_error('Номер цитаты не указан', update, context) return quote_obj = db.Quote.get_or_none(quote_id) if not quote_obj: reply_error(f'Цитаты #{quote_id} нет в базе', update, context) return reply_quote(quote_obj, update, context, **kwargs) return quote_obj
def update_quote( quote_id: int, update: Update = None, context: CallbackContext = None, log: logging.Logger = None, ): need_reply = update and context quote_bashim = bash_im.Quote.parse_from(quote_id) if not quote_bashim: text = f'Цитаты #{quote_id} на сайте нет' log and log.info(text) need_reply and reply_error(text, update, context) return quote_db: db.Quote = db.Quote.get_or_none(quote_id) if not quote_db: log and log.info(f'Цитаты #{quote_id} в базе нет, будет создание цитаты') # При отсутствии, цитата будет добавлена в базу db.Quote.get_from(quote_bashim) # Сразу же пробуем скачать комиксы quote_bashim.download_comics(DIR_COMICS) text = f'Цитата #{quote_id} добавлена в базу' log and log.info(text) need_reply and reply_info(text, update, context) else: modified_list = [] if quote_db.text != quote_bashim.text: quote_db.text = quote_bashim.text modified_list.append('текст') # Пробуем скачать комиксы quote_bashim.download_comics(DIR_COMICS) if modified_list: quote_db.modification_date = DT.date.today() quote_db.save() text = f'Цитата #{quote_id} обновлена ({", ".join(modified_list)})' log and log.info(text) need_reply and reply_info(text, update, context) else: text = f'Нет изменений в цитате #{quote_id}' log and log.info(text) need_reply and reply_info(text, update, context)
def on_error(update: Update, context: CallbackContext): reply_error(log, update, context)