def _get_posts_count(device): profileView = ProfileView(device) posts_count = 0 try: posts_count = profileView.getPostsCount() except Exception as e: logger.error("Cannot find posts count. Default is 0.") logger.debug(f"Error: {e}") return posts_count
def interact_with_user( device, username, my_username, likes_count, on_like, stories_count, stories_percentage, on_watch, can_follow, follow_percentage, profile_filter, args, session_state, ) -> Tuple[bool, bool]: """ :return: (whether interaction succeed, whether @username was followed during the interaction) """ if username == my_username: logger.info("It's you, skip.") return False, False random_sleep() if not profile_filter.check_profile(device, username): return False, False likes_value = get_value(likes_count, "Likes count: {}", 2) if likes_value > 12: logger.error("Max number of likes per user is 12.") likes_value = 12 profile_view = ProfileView(device) is_private = profile_view.isPrivateAccount() posts_count = profile_view.getPostsCount() is_empty = posts_count == 0 if is_private or is_empty: private_empty = "Private" if is_private else "Empty" logger.info(f"{private_empty} account.", extra={"color": f"{Fore.GREEN}"}) if can_follow and profile_filter.can_follow_private_or_empty(): followed = _follow(device, username, follow_percentage, args, session_state) else: followed = False logger.info("Skip user.", extra={"color": f"{Fore.GREEN}"}) return False, followed _watch_stories( device, profile_view, username, stories_count, stories_percentage, on_watch, args, session_state, ) ProfileView(device).swipe_to_fit_posts() random_sleep() start_time = time() full_rows, columns_last_row = profile_view.count_photo_in_view() end_time = format(time() - start_time, ".2f") photos_indices = list(range(0, full_rows * 3 + (columns_last_row))) logger.info( f"There are {len(photos_indices)} posts fully visible. Calculated in {end_time}s" ) if likes_value > len(photos_indices): logger.info(f"Only {photos_indices} photos available") else: shuffle(photos_indices) photos_indices = photos_indices[:likes_value] photos_indices = sorted(photos_indices) for i in range(0, len(photos_indices)): photo_index = photos_indices[i] row = photo_index // 3 column = photo_index - row * 3 logger.info(f"Open post #{i + 1} ({row + 1} row, {column + 1} column)") opened_post_view = PostsGridView(device).navigateToPost(row, column) random_sleep() like_succeed = False if opened_post_view: logger.info("Double click post.") like_succeed = opened_post_view.likePost() if not like_succeed: logger.debug("Double click failed. Try the like button.") like_succeed = opened_post_view.likePost(click_btn_like=True) if like_succeed: logger.debug("Like succeed. Check for block.") detect_block(device) on_like() else: logger.warning("Fail to like post. Let's continue...") logger.info("Back to profile.") device.back() if not opened_post_view or not like_succeed: reason = "open" if not opened_post_view else "like" logger.info( f"Could not {reason} photo. Posts count: {posts_count}") if can_follow and profile_filter.can_follow_private_or_empty(): followed = _follow(device, username, follow_percentage, args, session_state) else: followed = False if not followed: logger.info("Skip user.", extra={"color": f"{Fore.GREEN}"}) return False, followed random_sleep() if can_follow: return True, _follow(device, username, follow_percentage, args, session_state) return True, False
def interact_with_user( device, username, my_username, likes_count, on_like, can_follow, follow_percentage, profile_filter, ) -> Tuple[bool, bool]: """ :return: (whether interaction succeed, whether @username was followed during the interaction) """ if username == my_username: logger.info("It's you, skip.") return False, False random_sleep() if not profile_filter.check_profile(device, username): return False, False likes_value = get_value(likes_count, "Likes count: {}", 2) if likes_value > 12: logger.error("Max number of likes per user is 12") likes_value = 12 profile_view = ProfileView(device) is_private = profile_view.isPrivateAccount() posts_count = profile_view.getPostsCount() is_empty = posts_count == 0 if is_private or is_empty: private_empty = "Private" if is_private else "Empty" logger.info( f"{private_empty} account.", extra={"color": f"{Fore.GREEN}"}, ) if can_follow and profile_filter.can_follow_private_or_empty(): followed = _follow(device, username, follow_percentage) else: followed = False logger.info( "Skip user.", extra={"color": f"{Fore.GREEN}"}, ) return False, followed posts_tab_view = profile_view.navigateToPostsTab() if posts_tab_view.scrollDown(): # scroll down to view all maximum 12 posts logger.info("Scrolled down to see more posts.") random_sleep() number_of_rows_to_use = min((likes_value * 2) // 3 + 1, 4) photos_indices = list(range(0, number_of_rows_to_use * 3)) shuffle(photos_indices) photos_indices = photos_indices[:likes_value] photos_indices = sorted(photos_indices) for i in range(0, likes_value): photo_index = photos_indices[i] row = photo_index // 3 column = photo_index - row * 3 logger.info(f"Open post #{i + 1} ({row + 1} row, {column + 1} column") opened_post_view = posts_tab_view.navigateToPost(row, column) random_sleep() like_succeed = False if opened_post_view: logger.info("Double click post") opened_post_view.likePost() random_sleep() if not opened_post_view.isPostLiked(): logger.debug("Double click failed. Try the like button.") opened_post_view.likePost(click_btn_like=True) random_sleep() like_succeed = opened_post_view.isPostLiked() if like_succeed: detect_block(device) on_like() logger.info("Back to profile") device.back() if not opened_post_view or not like_succeed: reason = "open" if not opened_post_view else "like" logger.info( f"Could not {reason} photo. Posts count: {posts_count}") if can_follow and profile_filter.can_follow_private_or_empty(): followed = _follow(device, username, follow_percentage) else: followed = False if not followed: logger.info( "Skip user.", extra={"color": f"{Fore.GREEN}"}, ) return False, followed random_sleep() if can_follow: return True, _follow(device, username, follow_percentage) return True, False