def _job_handle_bloggers(device, bloggers, likes_count, follow_percentage, storage, on_interaction): class State: def __init__(self): pass is_job_completed = False state = State() session_state = sessions[-1] def on_likes_limit_reached(): state.is_job_completed = True on_interaction = partial(on_interaction, on_likes_limit_reached=on_likes_limit_reached) for blogger in bloggers: is_myself = blogger == session_state.my_username print_timeless("") print(COLOR_BOLD + "Handle @" + blogger + (is_myself and " (it\'s you)" or "") + COLOR_ENDC) completed = False on_interaction = partial(on_interaction, blogger=blogger) while not completed and not state.is_job_completed: try: username = None if not is_myself: username = blogger handle_blogger(device, username, likes_count, follow_percentage, storage, _on_like, on_interaction) completed = True except KeyboardInterrupt: print_copyright(session_state.my_username) print_timeless(COLOR_WARNING + "-------- FINISH: " + str(datetime.now().time()) + " --------" + COLOR_ENDC) _print_report() sys.exit(0) except (uiautomator.JsonRPCError, IndexError, HTTPException, timeout): print(COLOR_FAIL + traceback.format_exc() + COLOR_ENDC) take_screenshot(device) print("Try again for @" + blogger + " from the beginning") # Hack for the case when IGTV was accidentally opened close_instagram() random_sleep() open_instagram(device_id) get_my_username(device) except Exception as e: take_screenshot(device) _print_report() raise e
def _job_unfollow(device, count, storage, only_non_followers): class State: def __init__(self): pass unfollowed_count = 0 state = State() session_state = sessions[-1] def on_unfollow(): state.unfollowed_count += 1 session_state.totalUnfollowed += 1 completed = False while not completed and state.unfollowed_count < count: try: unfollow(device, count - state.unfollowed_count, on_unfollow, storage, only_non_followers, session_state.my_username) print("Unfollowed " + str(state.unfollowed_count) + ", finish.") completed = True except KeyboardInterrupt: print_copyright(session_state.my_username) print_timeless(COLOR_WARNING + "-------- FINISH: " + str(datetime.now().time()) + " --------" + COLOR_ENDC) _print_report() sys.exit(0) except (uiautomator.JsonRPCError, IndexError, HTTPException, timeout): print(COLOR_FAIL + traceback.format_exc() + COLOR_ENDC) take_screenshot(device) print("Try unfollowing again, " + str(count - state.unfollowed_count) + " users left") # Hack for the case when IGTV was accidentally opened close_instagram() random_sleep() open_instagram(device_id) get_my_username(device) except Exception as e: take_screenshot(device) _print_report() raise e
def main(): colorama.init() print_timeless(COLOR_HEADER + "Insomniac " + get_version() + "\n" + COLOR_ENDC) ok, args = _parse_arguments() if not ok: return global device_id device_id = args.device device = uiautomator.device if device_id is None else uiautomator.Device( device_id) if not check_adb_connection(is_device_id_provided=(device_id is not None)): return mode = None is_interact_enabled = len(args.interact) > 0 is_unfollow_enabled = int(args.unfollow) > 0 is_unfollow_non_followers_enabled = int(args.unfollow_non_followers) > 0 total_enabled = int(is_interact_enabled) + int(is_unfollow_enabled) + int( is_unfollow_non_followers_enabled) if total_enabled == 0: print_timeless( COLOR_FAIL + "You have to specify one of the actions: --interact, --unfollow, " "--unfollow-non-followers" + COLOR_ENDC) return elif total_enabled > 1: print_timeless( COLOR_FAIL + "Running Insomniac with two or more actions is not supported yet." + COLOR_ENDC) return else: if is_interact_enabled: print("Action: interact with @" + ", @".join(str(blogger) for blogger in args.interact)) mode = Mode.INTERACT elif is_unfollow_enabled: print("Action: unfollow " + str(args.unfollow)) mode = Mode.UNFOLLOW elif is_unfollow_non_followers_enabled: print("Action: unfollow " + str(args.unfollow_non_followers) + " non followers") mode = Mode.UNFOLLOW_NON_FOLLOWERS on_interaction = partial(_on_interaction, interactions_limit=int(args.interactions_count), likes_limit=int(args.total_likes_limit)) while True: session_state = SessionState() sessions.append(session_state) print_timeless(COLOR_WARNING + "\n-------- START: " + str(session_state.startTime) + " --------" + COLOR_ENDC) open_instagram(device_id) session_state.my_username = get_my_username(device) storage = Storage(session_state.my_username) # IMPORTANT: in each job we assume being on the top of the Profile tab already if mode == Mode.INTERACT: _job_handle_bloggers(device, args.interact, int(args.likes_count), int(args.follow_percentage), storage, on_interaction) elif mode == Mode.UNFOLLOW: _job_unfollow(device, int(args.unfollow), storage, only_non_followers=False) elif mode == Mode.UNFOLLOW_NON_FOLLOWERS: _job_unfollow(device, int(args.unfollow_non_followers), storage, only_non_followers=True) close_instagram() print_copyright(session_state.my_username) session_state.finishTime = datetime.now() print_timeless(COLOR_WARNING + "-------- FINISH: " + str(session_state.finishTime) + " --------" + COLOR_ENDC) if args.repeat: _print_report() repeat = int(args.repeat) print_timeless("") print("Sleep for " + str(repeat) + " minutes") try: sleep(60 * repeat) except KeyboardInterrupt: _print_report() sys.exit(0) else: break _print_report()