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 open_stories(_, 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/no_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: StoriesIterator = cached_stories.get(key) if iterator is None: user = get_user_id(username) if "username" not in user: await callback.answer(get_message(language, "errors/fail")) return stories = _request_story(user["user_id"]) if stories == "private_account": await callback.answer( get_message(language, "errors/private_account")) return if stories == "no_stories": await callback.answer(get_message(language, "errors/no_stories")) return iterator = StoriesIterator(stories, username) cached_stories[key] = iterator await callback.answer() story: Story = iterator.next( ) if iterator.index != -1 else iterator.collection[iterator.index] caption = format_date(story.taken_at) keyboard = create_keyboard_stories(iterator.username, len(iterator.collection), language, from_profile=True) media = InputMediaVideo( story.url ) if story.type_story == "mp4/video/boomerang" else InputMediaPhoto( story.url) media.caption = caption await callback.edit_message_media(media, reply_markup=keyboard)