From f08be08730a82fd70b64e1044a0a78093370a6d8 Mon Sep 17 00:00:00 2001 From: Dennis <52335835+mastrolube@users.noreply.github.com> Date: Wed, 23 Dec 2020 07:28:39 +0100 Subject: [PATCH] --interact-from-file improvements (#155) * in --interact-from-file the user will not search in recent anymore * don't refresh if not needed * swipe_to_fit_posts improvement --- GramAddict/core/device_facade.py | 3 +- GramAddict/core/interaction.py | 15 +++- GramAddict/core/views.py | 110 ++++++++++++++--------- GramAddict/plugins/force_interact.dis | 2 +- GramAddict/plugins/interact_usernames.py | 13 +-- GramAddict/version.py | 2 +- setup.py | 2 +- 7 files changed, 93 insertions(+), 54 deletions(-) diff --git a/GramAddict/core/device_facade.py b/GramAddict/core/device_facade.py index 2b444106..e65c5108 100644 --- a/GramAddict/core/device_facade.py +++ b/GramAddict/core/device_facade.py @@ -74,6 +74,7 @@ def find(self, *args, **kwargs): return DeviceFacade.View(version=2, view=view, device=self.deviceV2) def back(self): + logger.debug("Press back button") if self.deviceV1 is not None: self.deviceV1.press.back() else: @@ -466,7 +467,7 @@ def click(self, mode=None): visible_bounds["top"] + (visible_bounds["bottom"] - visible_bounds["top"]) * y_offset ) - logger.debug(f"Single click ({x_abs}, {y_abs})") + logger.debug(f"Single click ({x_abs},{y_abs})") self.viewV2.click(UI_TIMEOUT_LONG, offset=(x_offset, y_offset)) except uiautomator2.JSONRPCError as e: diff --git a/GramAddict/core/interaction.py b/GramAddict/core/interaction.py index d1c613c1..ca2e5ff3 100644 --- a/GramAddict/core/interaction.py +++ b/GramAddict/core/interaction.py @@ -94,7 +94,15 @@ def interact_with_user( ) swipe_amount = ProfileView(device).swipe_to_fit_posts() + if swipe_amount == -1: + return False, False random_sleep() + + 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 + start_time = time() full_rows, columns_last_row = profile_view.count_photo_in_view() end_time = format(time() - start_time, ".2f") @@ -162,8 +170,11 @@ def interact_with_user( random_sleep() if can_follow: - return True, _follow( - device, username, follow_percentage, args, session_state, swipe_amount + return ( + True, + _follow( + device, username, follow_percentage, args, session_state, swipe_amount + ), ) return True, False diff --git a/GramAddict/core/views.py b/GramAddict/core/views.py index af4dba99..d638690f 100644 --- a/GramAddict/core/views.py +++ b/GramAddict/core/views.py @@ -3,6 +3,7 @@ import re from enum import Enum, auto from colorama import Fore, Style +from random import randint from GramAddict.core.device_facade import DeviceFacade from GramAddict.core.resources import ClassName, ResourceID as resources, TabBarText @@ -296,24 +297,35 @@ def _searchTabWithTextPlaceholder(self, tab: SearchTabs): return item return None - def navigateToUsername(self, username): - logger.debug("Navigate to profile @" + username) + def navigateToUsername( + self, username, interact_usernames=False, swipe_to_accounts=True + ): + logger.debug("Search for @" + username) search_edit_text = self._getSearchEditText() search_edit_text.click() - logger.debug("Close the keyboad") - DeviceFacade.back(self.device) random_sleep(1, 2) - searched_user_recent = self._getUsernameRow(username) - if searched_user_recent.exists(True): - searched_user_recent.click() + if swipe_to_accounts: + logger.debug("Close the keyboard") + DeviceFacade.back(self.device) + random_sleep(1, 2) + DeviceFacade.swipe(self.device, DeviceFacade.Direction.LEFT, 0.8) + random_sleep(1, 2) + if interact_usernames: + search_edit_text.set_text(username) else: + searched_user_recent = self._getUsernameRow(username) + if searched_user_recent.exists(True): + searched_user_recent.click() + return ProfileView(self.device, is_own_profile=False) search_edit_text.set_text(username) - random_sleep(1, 2) - username_view = self._getUsernameRow(username) - if not username_view.exists(): - logger.error("Cannot find user @" + username + ".") - return None - username_view.click() + logger.debug("Close the keyboard") + DeviceFacade.back(self.device) + random_sleep(1, 2) + username_view = self._getUsernameRow(username) + if not username_view.exists(True): + logger.error("Cannot find user @" + username + ".") + return None + username_view.click() return ProfileView(self.device, is_own_profile=False) @@ -334,7 +346,7 @@ def navigateToHashtag(self, hashtag): return None hashtag_tab.click() random_sleep(1, 2) - logger.debug("Close the keyboad") + logger.debug("Close the keyboard") DeviceFacade.back(self.device) random_sleep(1, 2) # check if that hashtag already exists in the recent search list -> act as human @@ -1148,29 +1160,35 @@ def swipe_to_fit_posts(self): element_to_swipe_over_obj = self.device.find( resourceIdMatches=ResourceID.PROFILE_TABS_CONTAINER ) - if not element_to_swipe_over_obj.exists(): - UniversalActions(self.device)._swipe_points(direction=Direction.DOWN) - element_to_swipe_over_obj = self.device.find( - resourceIdMatches=ResourceID.PROFILE_TABS_CONTAINER - ) - - element_to_swipe_over = element_to_swipe_over_obj.get_bounds()["top"] - try: - bar_countainer = self.device.find( - resourceIdMatches=ResourceID.ACTION_BAR_CONTAINER - ).get_bounds()["bottom"] - - logger.info("Scrolled down to see more posts.") - self.device.swipe_points( - displayWidth / 2, - element_to_swipe_over, - displayWidth / 2, - bar_countainer, - ) - return element_to_swipe_over - bar_countainer - except: - logger.info("I'm not able to scroll down.") - return 0 + for _ in range(2): + if not element_to_swipe_over_obj.exists(): + UniversalActions(self.device)._swipe_points( + direction=Direction.DOWN, delta_y=randint(300, 350) + ) + element_to_swipe_over_obj = self.device.find( + resourceIdMatches=ResourceID.PROFILE_TABS_CONTAINER + ) + continue + + element_to_swipe_over = element_to_swipe_over_obj.get_bounds()["top"] + try: + bar_countainer = self.device.find( + resourceIdMatches=ResourceID.ACTION_BAR_CONTAINER + ).get_bounds()["bottom"] + + logger.info("Scrolled down to see more posts.") + self.device.swipe_points( + displayWidth / 2, + element_to_swipe_over, + displayWidth / 2, + bar_countainer, + ) + return element_to_swipe_over - bar_countainer + except: + logger.info("I'm not able to scroll down.") + return 0 + logger.warning("Maybe a private or empty profile in which check failed.. Skip") + return -1 def navigateToPostsTab(self): self._navigateToTab(TabBarText.POSTS_CONTENT_DESC) @@ -1274,13 +1292,16 @@ class UniversalActions: def __init__(self, device: DeviceFacade): self.device = device - def _swipe_points(self, direction: Direction, start_point_y=0, delta_y=450): - middle_point_x = self.device.get_info()["displayWidth"] / 2 - if start_point_y == 0: - start_point_y = self.device.get_info()["displayHeight"] / 2 - if start_point_y - delta_y < 0: - delta_y = start_point_y / 2 + def _swipe_points(self, direction: Direction, start_point_y=-1, delta_y=450): + displayWidth = self.device.get_info()["displayWidth"] + displayHeight = self.device.get_info()["displayHeight"] + middle_point_x = displayWidth / 2 + if start_point_y == -1: + start_point_y = displayHeight / 2 if direction == Direction.UP: + if start_point_y + delta_y > displayHeight: + delta = start_point_y + delta_y - displayHeight + start_point_y = start_point_y - delta self.device.swipe_points( middle_point_x, start_point_y, @@ -1288,6 +1309,9 @@ def _swipe_points(self, direction: Direction, start_point_y=0, delta_y=450): start_point_y + delta_y, ) elif direction == Direction.DOWN: + if start_point_y - delta_y < 0: + delta = abs(start_point_y - delta_y) + start_point_y = start_point_y + delta self.device.swipe_points( middle_point_x, start_point_y, diff --git a/GramAddict/plugins/force_interact.dis b/GramAddict/plugins/force_interact.dis index 263c24fd..369a995b 100644 --- a/GramAddict/plugins/force_interact.dis +++ b/GramAddict/plugins/force_interact.dis @@ -243,7 +243,7 @@ class ForceIteract(Plugin): def open_user(self, device, username): search_view = TabBarView(device).navigateToSearch() - profile_view = search_view.navigateToUsername(username) + profile_view = search_view.navigateToUsername(username, True) random_sleep() if not profile_view: return False diff --git a/GramAddict/plugins/interact_usernames.py b/GramAddict/plugins/interact_usernames.py index 3ba8c2c7..3b026ba3 100644 --- a/GramAddict/plugins/interact_usernames.py +++ b/GramAddict/plugins/interact_usernames.py @@ -163,7 +163,7 @@ def handle_username_file( source=current_file, session_state=self.session_state, ) - + need_to_refresh = True if path.isfile(current_file): with open(current_file, "r") as f: for line in f: @@ -175,10 +175,13 @@ def handle_username_file( elif storage.check_user_was_interacted(username): logger.info(f"@{username}: already interacted. Skip.") continue - - search_view = TabBarView(device).navigateToSearch() - random_sleep() - profile_view = search_view.navigateToUsername(username) + if need_to_refresh: + search_view = TabBarView(device).navigateToSearch() + random_sleep() + profile_view = search_view.navigateToUsername( + username, True, need_to_refresh + ) + need_to_refresh = False if not profile_view: continue random_sleep() diff --git a/GramAddict/version.py b/GramAddict/version.py index c68196d1..a955fdae 100644 --- a/GramAddict/version.py +++ b/GramAddict/version.py @@ -1 +1 @@ -__version__ = "1.2.0" +__version__ = "1.2.1" diff --git a/setup.py b/setup.py index 3613b938..53045fdf 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="gramaddict", - version="1.2.0b2", + version="1.2.1", author="GramAddict Team", author_email="maintainers@gramaddict.org", description="Completely free and open source human-like Instagram bot. Powered by UIAutomator2 and compatible with basically any android device that can run instagram - real or emulated.",