diff --git a/GramAddict/core/device_facade.py b/GramAddict/core/device_facade.py index 60185b8a..5c06a687 100644 --- a/GramAddict/core/device_facade.py +++ b/GramAddict/core/device_facade.py @@ -1,5 +1,6 @@ import logging import string +import time from datetime import datetime from enum import Enum, auto from inspect import stack @@ -12,7 +13,7 @@ import uiautomator2 -from GramAddict.core.utils import random_sleep +from GramAddict.core.utils import random_sleep, wait_for_element logger = logging.getLogger(__name__) diff --git a/GramAddict/core/handle_sources.py b/GramAddict/core/handle_sources.py index 3b65de9a..38e8fd85 100644 --- a/GramAddict/core/handle_sources.py +++ b/GramAddict/core/handle_sources.py @@ -33,6 +33,7 @@ UniversalActions, case_insensitive_re, ) +from uiautomator2.exceptions import UiObjectNotFoundError logger = logging.getLogger(__name__) @@ -715,14 +716,23 @@ def scrolled_to_top(): return row_search.exists() while True: - logger.info("Iterate over visible followers.") - screen_iterated_followers = [] - screen_skipped_followers_count = 0 - scroll_end_detector.notify_new_page() - user_list = device.find( - resourceIdMatches=self.ResourceID.USER_LIST_CONTAINER, - ) - row_height, n_users = inspect_current_view(user_list) + try: + logger.info("Iterate over visible followers.") + screen_iterated_followers = [] + screen_skipped_followers_count = 0 + scroll_end_detector.notify_new_page() + user_list = device.find( + resourceIdMatches=self.ResourceID.USER_LIST_CONTAINER, + ) + row_height, n_users = inspect_current_view(user_list) + except UiObjectNotFoundError: + logger.info( + "Unable to find USER_LIST_CONTAINER elements, clicking on the back button again", + extra={"color": f"{Fore.RED}"}, + ) + device.back() + continue + try: for item in user_list: cur_row_height = item.get_height() @@ -792,6 +802,7 @@ def scrolled_to_top(): extra={"color": f"{Fore.GREEN}"}, ) + if is_myself and scrolled_to_top(): logger.info("Scrolled to top, finish.", extra={"color": f"{Fore.GREEN}"}) return diff --git a/GramAddict/core/navigation.py b/GramAddict/core/navigation.py index 49fa76e5..5bf796cc 100644 --- a/GramAddict/core/navigation.py +++ b/GramAddict/core/navigation.py @@ -68,18 +68,20 @@ def nav_to_hashtag_or_place(device, target, current_job): TargetView = HashTagView if current_job.startswith("hashtag") else PlacesView - if current_job.endswith("recent"): - logger.info("Switching to Recent tab.") - recent_tab = TargetView(device)._getRecentTab() - if recent_tab.exists(Timeout.MEDIUM): - recent_tab.click() - else: - return False - - if UniversalActions(device)._check_if_no_posts(): - UniversalActions(device)._reload_page() - if UniversalActions(device)._check_if_no_posts(): - return False + # when searching for a hashtag, the recent tab is not available anymore in version 263.2.0.19.104 + + # if current_job.endswith("recent"): + # logger.info("Switching to Recent tab.") + # recent_tab = TargetView(device)._getRecentTab() + # if recent_tab.exists(Timeout.MEDIUM): + # recent_tab.click() + # else: + # return False + # + # if UniversalActions(device)._check_if_no_posts(): + # UniversalActions(device)._reload_page() + # if UniversalActions(device)._check_if_no_posts(): + # return False result_view = TargetView(device)._getRecyclerView() FistImageInView = TargetView(device)._getFistImageView(result_view) diff --git a/GramAddict/core/utils.py b/GramAddict/core/utils.py index 5f8be6ad..da8ad735 100644 --- a/GramAddict/core/utils.py +++ b/GramAddict/core/utils.py @@ -731,6 +731,15 @@ def inspect_current_view(user_list) -> Tuple[int, int]: logger.debug(f"There are {n_users} users fully visible in that view.") return row_height, n_users +# used by back function device_facade.py - attempt to fix bug #367 +def wait_for_element(device, selector, timeout=15): + end_time = time.time() + timeout + while time.time() < end_time: + if device(**selector).exists: + return True + time.sleep(1) + return False + class ActionBlockedError(Exception): pass diff --git a/GramAddict/core/views.py b/GramAddict/core/views.py index e91f77a3..ec763a30 100644 --- a/GramAddict/core/views.py +++ b/GramAddict/core/views.py @@ -919,6 +919,11 @@ def detect_media_type(content_desc) -> Tuple[Optional[MediaType], Optional[int]] ) obj_count = n_photos + n_videos media_type = MediaType.CAROUSEL + # workaround test for bug #285/#301 + if obj_count == 0: + media_type = MediaType.REEL # it might be better to set it to UNKNOWN + logger.info("Activating workaround test for Bug #285 - switching to REEL media type") + # end of workaround test return media_type, obj_count def _like_in_post_view(