diff --git a/app/client_bot/routers/content.py b/app/client_bot/routers/content.py index 5f4e745..f8b9406 100644 --- a/app/client_bot/routers/content.py +++ b/app/client_bot/routers/content.py @@ -20,53 +20,58 @@ router = Router() async def t_inline_query_node_content(query: types.InlineQuery, memory=None, user=None, db_session=None, chat_wrap=None, **extra): - args = query.query[1:] - cid = ContentId.deserialize(args) - - content_list = [] - content = db_session.query(StoredContent).filter_by(hash=cid.content_hash_b58).first() - decrypted_content = None - if content: - if content.encrypted: - decrypted_content = db_session.query(StoredContent).filter_by(id=content.decrypted_content_id).first() - else: - decrypted_content = content - - if not decrypted_content: - make_log("OwnedContent", f"Can't get decrypted content: {content.id}", level='warning') - return await query.answer(content_list) - try: - metadata_content = StoredContent.from_cid(db_session, content.json_format()['metadata_cid']) - with open(metadata_content.filepath, 'r') as f: - metadata_content_json = json.loads(f.read()) - except BaseException as e: - make_log("OwnedContent", f"Can't get metadata content: {e}", level='warning') - return await query.answer(content_list) + args = query.query[1:] + cid = ContentId.deserialize(args) - audio_title = metadata_content_json.get('name', "").split(' - ') - title, performer = None - if len(audio_title) > 1: - performer = audio_title[0].strip() - audio_title = audio_title[1:] + content_list = [] + content = db_session.query(StoredContent).filter_by(hash=cid.content_hash_b58).first() + decrypted_content = None + if content: + if content.encrypted: + decrypted_content = db_session.query(StoredContent).filter_by(id=content.decrypted_content_id).first() + else: + decrypted_content = content - title = audio_title[0].strip() + if not decrypted_content: + make_log("OwnedContent", f"Can't get decrypted content: {content.id}", level='warning') + return await query.answer(content_list) - content_list.append( - types.InlineQueryResultAudio( - id=content.id, - audio_url=decrypted_content.web_url, - title=title, - performer=performer, - reply_markup=get_inline_keyboard([ - [{ - 'text': user.translated('viewTrack_button'), - 'url': f"https://t.me/{CLIENT_TELEGRAM_BOT_USERNAME}?start=C{content.cid.serialize_v2()}" - }] - ]) + try: + metadata_content = StoredContent.from_cid(db_session, content.json_format()['metadata_cid']) + with open(metadata_content.filepath, 'r') as f: + metadata_content_json = json.loads(f.read()) + except BaseException as e: + make_log("OwnedContent", f"Can't get metadata content: {e}", level='warning') + return await query.answer(content_list) + + audio_title = metadata_content_json.get('name', "").split(' - ') + title, performer = None + if len(audio_title) > 1: + performer = audio_title[0].strip() + audio_title = audio_title[1:] + + title = audio_title[0].strip() + + content_list.append( + types.InlineQueryResultAudio( + id=content.id, + audio_url=decrypted_content.web_url, + title=title, + performer=performer, + reply_markup=get_inline_keyboard([ + [{ + 'text': user.translated('viewTrack_button'), + 'url': f"https://t.me/{CLIENT_TELEGRAM_BOT_USERNAME}?start=C{content.cid.serialize_v2()}" + }] + ]) + ) ) - ) - return await query.answer(content_list) + return await query.answer(content_list) + except BaseException as e: + logger.error(f"Error in t_inline_query_node_content: {e}") + traceback.print_exc() + return await query.answer([]) router.inline_query.register(t_inline_query_node_content, F.query.startswith('C'))