Skip to content

Commit

Permalink
Immediately exit the script if action is blocked #27
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Mishchenko committed Aug 31, 2020
1 parent 77d9fe5 commit 4ca1d22
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/action_handle_blogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def open_photo():
except uiautomator.JSONRPCError:
print("Double click worked successfully.")

detect_block(device)
on_like()
print("Back to profile")
device.press("back")
Expand All @@ -291,6 +292,7 @@ def _follow(device, username, follow_percentage):

if follow_button.exists(timeout=UI_TIMEOUT):
follow_button.click(timeout=UI_TIMEOUT)
detect_block(device)
bottom_sheet = device(resourceId='com.instagram.android:id/layout_container_bottom_sheet',
className='android.widget.FrameLayout')
if bottom_sheet.exists(timeout=UI_TIMEOUT):
Expand Down
26 changes: 3 additions & 23 deletions src/action_unfollow.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def _do_unfollow(device, username, my_username, check_if_is_follower):

if check_if_is_follower and _check_is_follower(device, username, my_username):
print("Skip @" + username + ". This user is following you.")
print("Back to the followings list.")
device.press("back")
return False

Expand Down Expand Up @@ -138,15 +139,11 @@ def _do_unfollow(device, username, my_username, check_if_is_follower):
confirm_unfollow_button.click(timeout=UI_TIMEOUT)

random_sleep()

unfollow_succeed = not _check_if_you_follow(device, username, my_username)
if not unfollow_succeed:
print(COLOR_FAIL + "Unfollow didn't work for some reason." + COLOR_ENDC)
raise UnfollowError("Please check why unfollow doesn't work, you may be blocked!")
detect_block(device)

print("Back to the followings list.")
device.press("back")
return unfollow_succeed
return True


def _check_is_follower(device, username, my_username):
Expand All @@ -166,23 +163,6 @@ def _check_is_follower(device, username, my_username):
return result


def _check_if_you_follow(device, username, my_username):
print("Make sure that you don't still follow @" + username)
followers_container = device(resourceId='com.instagram.android:id/row_profile_header_followers_container',
className='android.widget.LinearLayout')
followers_container.click(timeout=UI_TIMEOUT)

random_sleep()

my_username_view = device(resourceId='com.instagram.android:id/follow_list_username',
className='android.widget.TextView',
text=my_username)
result = my_username_view.exists(timeout=UI_TIMEOUT)
print("Back to the profile.")
device.press("back")
return result


@unique
class UnfollowRestriction(Enum):
ANY = 0
Expand Down
16 changes: 16 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from random import randint
from time import sleep

from src.globals import UI_TIMEOUT

COLOR_HEADER = '\033[95m'
COLOR_OKBLUE = '\033[94m'
COLOR_OKGREEN = '\033[92m'
Expand Down Expand Up @@ -85,6 +87,16 @@ def take_screenshot(device):
print(COLOR_FAIL + "Cannot save screenshot." + COLOR_ENDC)


def detect_block(device):
block_dialog = device(resourceId='com.instagram.android:id/dialog_root_view',
className='android.widget.FrameLayout')
is_blocked = block_dialog.exists(UI_TIMEOUT)
if is_blocked:
print(COLOR_FAIL + "Probably block dialog is shown." + COLOR_ENDC)
raise ActionBlockedError("Seems that action is blocked. Consider reinstalling Instagram app and be more careful"
" with limits!")


def print_copyright(username):
if hashlib.sha1(username.encode('utf-8')).hexdigest() not in COPYRIGHT_BLACKLIST:
print_timeless("\nIf you like this script and want it to be improved, " + COLOR_BOLD + "donate please"
Expand Down Expand Up @@ -114,3 +126,7 @@ def wrapper(*args, **kwargs):

print_timeless = _print_with_time_decorator(print, False)
print = _print_with_time_decorator(print, True)


class ActionBlockedError(Exception):
pass

0 comments on commit 4ca1d22

Please sign in to comment.