async def open_posts(_, callback): username = callback.data.split(' ')[1] language = get_language(callback.from_user.id, callback.from_user.language_code) key = f"{callback.message.chat.id}_{callback.message.message_id}" profile: Profile = cached_profiles.get(key) right_user_id = cached_ids.get(key) if profile is None or right_user_id is None: await callback.answer(get_message(language, "errors/not_cached_post"), show_alert=True) await callback.edit_message_text("", reply_markup="") return if callback.from_user.id != right_user_id: await callback.answer(get_message(language, "errors/wrong_id"), show_alert=True) return iterator: PostsIterator = cached_posts.get(key) if iterator is None: posts = get_user_posts(username) if posts == "Fail": await callback.answer(get_message(language, "errors/fail"), show_alert=True) return next_max_id = posts.get("next_max_id") iterator = PostsIterator(posts["post_list"], username, next_max_id) cached_posts[key] = iterator await callback.answer() post: Post = iterator.next( ) if iterator.index != -1 else iterator.collection[iterator.index] caption = create_caption_posts(post.caption, post.taken_at, post.views, post.is_video) keyboard = create_keyboard_posts(post.likes, post.comment_number, iterator.username, len(iterator.collection), language, callback.from_user.id, from_profile=True) media = InputMediaVideo(post.source) if post.is_video else InputMediaPhoto( post.source) media.caption = caption await callback.edit_message_media(media) await callback.edit_message_reply_markup(keyboard)
async def next_post(_, callback): message_id = callback.message.message_id chat_id = callback.message.chat.id key = f"{chat_id}_{message_id}" iterator: PostsIterator = cached_posts.get(key) language = get_language(callback.from_user.id, callback.from_user.language_code) if iterator is None: await callback.answer(get_message(language, "errors/not_cached_post"), show_alert=True) await callback.edit_message_text("", reply_markup="") return if iterator.right_user_id is None: iterator.right_user_id = cached_ids.get(key) if iterator.right_user_id is None: await callback.answer(get_message(language, "errors/not_cached_stories"), show_alert=True) await callback.edit_message_text("", reply_markup="") return if callback.from_user.id != iterator.right_user_id: await callback.answer(get_message(language, "errors/wrong_id"), show_alert=True) return post = iterator.next() if post == "Fail": await callback.answer(get_message(language, "errors/fail"), show_alert=True) return await callback.answer() caption = create_caption_posts(post.caption, post.taken_at, post.views, post.is_video) from_profile = len(callback.data.split(' ')) > 1 keyboard = create_keyboard_posts(post.likes, post.comment_number, iterator.username, len(iterator.collection), callback.from_user.language_code, callback.from_user.id, from_profile=from_profile) media = InputMediaVideo(post.source) if post.is_video else InputMediaPhoto( post.source) media.caption = caption await callback.edit_message_media(media, reply_markup=keyboard)
async def on_posts(_, message): username = '******'.join(message.command[1:]) language = get_language(message.from_user.id, message.from_user.language_code) if username == "": await message.reply_text( get_message(language, "errors/username_not_specified")) return wait_message = await message.reply_text(get_message(language, "loading")) posts = get_user_posts(username) if posts == "Fail": await message.reply_text(get_message(language, "errors/fail")) return next_max_id = posts.get("next_max_id") iterator = PostsIterator(posts["post_list"], username, next_max_id, message.from_user.id) post = iterator.next() caption = create_caption_posts(post.caption, post.taken_at, post.views, post.is_video) keyboard = create_keyboard_posts(post.likes, post.comment_number, iterator.username, len(iterator.collection), message.from_user.language_code, message.from_user.id) try: await wait_message.delete() except: pass if post.is_video: message = await message.reply_video(post.source, caption=caption, reply_markup=keyboard) else: message = await message.reply_photo(post.source, caption=caption, reply_markup=keyboard) key = f"{message.chat.id}_{message.message_id}" cached_posts[key] = iterator