From c8286c6ad33cdd6857e12c976b5f176fdd3ed10d Mon Sep 17 00:00:00 2001 From: Alexander Mishchenko Date: Fri, 9 Oct 2020 15:15:15 +0300 Subject: [PATCH] Fix not finishing session sometimes when the end of followers list is reached #119 --- src/action_handle_blogger.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/action_handle_blogger.py b/src/action_handle_blogger.py index 1ebb1dd..df4ddba 100644 --- a/src/action_handle_blogger.py +++ b/src/action_handle_blogger.py @@ -105,11 +105,12 @@ def scrolled_to_top(): className='android.widget.EditText') return row_search.exists() + prev_screen_iterated_followers = [] while True: print("Iterate over visible followers") random_sleep() - screen_iterated_followers = 0 - screen_skipped_followers = 0 + screen_iterated_followers = [] + screen_skipped_followers_count = 0 try: for item in device.find(resourceId='com.instagram.android:id/follow_list_container', @@ -121,14 +122,14 @@ def scrolled_to_top(): break username = user_name_view.get_text() - screen_iterated_followers += 1 + screen_iterated_followers.append(username) if not is_myself and storage.check_user_was_interacted(username): print("@" + username + ": already interacted. Skip.") - screen_skipped_followers += 1 + screen_skipped_followers_count += 1 elif is_myself and storage.check_user_was_interacted_recently(username): print("@" + username + ": already interacted in the last week. Skip.") - screen_skipped_followers += 1 + screen_skipped_followers_count += 1 else: print("@" + username + ": interact") user_name_view.click() @@ -153,9 +154,15 @@ def scrolled_to_top(): if is_myself and scrolled_to_top(): print(COLOR_OKGREEN + "Scrolled to top, finish." + COLOR_ENDC) return - elif screen_iterated_followers > 0: + elif len(screen_iterated_followers) > 0: load_more_button = device.find(resourceId='com.instagram.android:id/row_load_more_button') - need_swipe = screen_skipped_followers == screen_iterated_followers + load_more_button_exists = load_more_button.exists() + + if not load_more_button_exists and screen_iterated_followers == prev_screen_iterated_followers: + print(COLOR_OKGREEN + "Iterated exactly the same followers twice, finish." + COLOR_ENDC) + return + + need_swipe = screen_skipped_followers_count == len(screen_iterated_followers) list_view = device.find(resourceId='android:id/list', className='android.widget.ListView') if is_myself: @@ -163,7 +170,7 @@ def scrolled_to_top(): list_view.scroll(DeviceFacade.Direction.TOP) else: pressed_retry = False - if load_more_button.exists(): + if load_more_button_exists: retry_button = load_more_button.child(className='android.widget.ImageView') if retry_button.exists(): retry_button.click() @@ -176,6 +183,9 @@ def scrolled_to_top(): else: print(COLOR_OKGREEN + "Need to scroll now" + COLOR_ENDC) list_view.scroll(DeviceFacade.Direction.BOTTOM) + + prev_screen_iterated_followers.clear() + prev_screen_iterated_followers += screen_iterated_followers else: print(COLOR_OKGREEN + "No followers were iterated, finish." + COLOR_ENDC) return