def verify_action(browser, action, track, username, person, person_id, logger, logfolder): """ Verify if the action has succeeded """ # currently supported actions are follow & unfollow if action in ["follow", "unfollow"]: if action == "follow": post_action_text = "//button[text()='Following' or text(" ")='Requested']" elif action == "unfollow": post_action_text = "//button[text()='Follow' or text()='Follow " "Back']" button_change = explicit_wait(browser, "VOEL", [post_action_text, "XPath"], logger, 7, False) if not button_change: reload_webpage(browser, Settings) following_status, follow_button = get_following_status( browser, track, username, person, person_id, logger, logfolder) # find action state *.^ if following_status in ["Following", "Requested"]: action_state = False if action == "unfollow" else True elif following_status in ["Follow", "Follow Back"]: action_state = True if action == "unfollow" else False else: action_state = None # handle it! if action_state is True: logger.info( "Last {} is verified after reloading the page!".format( action)) elif action_state is False: # try to do the action one more time! click_visibly(browser, Settings, follow_button) if action == "unfollow": sleep(4) # TODO: use explicit wait here confirm_unfollow(browser) button_change = explicit_wait(browser, "VOEL", [post_action_text, "XPath"], logger, 9, False) if not button_change: logger.warning("Phew! Last {0} is not verified." "\t~'{1}' might be temporarily blocked " "from {0}ing\n".format(action, username)) sleep(210) return False, "temporary block" elif action_state is None: logger.error( "Hey! Last {} is not verified out of an unexpected " "failure!".format(action)) return False, "unexpected" return True, "success"
def friend_user(browser, track, login, userid_to_friend, times, blacklist, logger, logfolder): """ Friend a user either from the profile page or post page or dialog box """ # list of available tracks to friend in: ["profile", "post" "dialog"] # check action availability if quota_supervisor(Settings, "friends") == "jump": return False, "jumped" # check URL of the webpage, if it already is user's profile # page, then do not navigate to it again if friend_restriction("read", userid_to_friend, 1, logger): logger.info("Already connected {} or more times".format(times)) return False, "already friended" user_link = "https://www.facebook.com/{}/".format(userid_to_friend) web_address_navigator(browser, user_link, logger, Settings) # find out CURRENT friending status friending_status, friend_button = get_friending_status( browser, track, login, userid_to_friend, None, logger, logfolder) logger.info(friending_status) if friending_status in ["Add Friend"]: click_visibly(browser, Settings, friend_button) # click to friend friend_state, msg = verify_action(browser, "friend", track, login, userid_to_friend, None, logger, logfolder) if friend_state is not True: return False, msg elif friending_status is None: # TODO:BUG:2nd login has to be fixed with userid of loggedin user sirens_wailing, emergency_state = emergency_exit( browser, Settings, "https://facebook.com", login, login, logger, logfolder) if sirens_wailing is True: return False, emergency_state else: logger.warning( "--> Add Friend button not present '{}'!\t~unexpected failure". format(userid_to_friend)) return False, "unexpected failure" # general tasks after a successful friend logger.info("--> Friended '{}'!".format(userid_to_friend.encode("utf-8"))) update_activity(Settings, 'friendeds') logtime = datetime.now().strftime('%Y-%m-%d %H:%M') log_friended_pool(login, userid_to_friend, logger, logfolder, logtime, userid_to_friend) friend_restriction("write", userid_to_friend, None, logger) return True, "success"
def follow_user(browser, track, login, userid_to_follow, button, blacklist, logger, logfolder, Settings): """ Follow a user either from the profile page or post page or dialog box """ # list of available tracks to follow in: ["profile", "post" "dialog"] # check action availability if quota_supervisor(Settings, "follows") == "jump": return False, "jumped" if track in ["profile", "post"]: if track == "profile": # check URL of the webpage, if it already is user's profile # page, then do not navigate to it again user_link = "https://www.facebook.com/{}/".format(userid_to_follow) web_address_navigator(browser, user_link, Settings) # find out CURRENT following status following_status, follow_button = \ get_following_status(browser, track, login, userid_to_follow, None, logger, logfolder) if following_status in ["Follow", "Follow Back"]: click_visibly(browser, Settings, follow_button) # click to follow follow_state, msg = verify_action(browser, "follow", track, login, userid_to_follow, None, logger, logfolder) if follow_state is not True: return False, msg elif following_status in ["Following", "Requested"]: if following_status == "Following": logger.info( "--> Already following '{}'!\n".format(userid_to_follow)) elif following_status == "Requested": logger.info("--> Already requested '{}' to follow!\n".format( userid_to_follow)) sleep(1) return False, "already followed" elif following_status in ["Unblock", "UNAVAILABLE"]: if following_status == "Unblock": failure_msg = "user is in block" elif following_status == "UNAVAILABLE": failure_msg = "user is inaccessible" logger.warning("--> Couldn't follow '{}'!\t~{}".format( userid_to_follow, failure_msg)) return False, following_status elif following_status is None: # TODO:BUG:2nd login has to be fixed with userid of loggedin user sirens_wailing, emergency_state = emergency_exit( browser, Settings, "https://www.facebook.com", login, login, logger, logfolder) if sirens_wailing is True: return False, emergency_state else: logger.warning( "--> Couldn't unfollow '{}'!\t~unexpected failure".format( userid_to_follow)) return False, "unexpected failure" elif track == "dialog": click_element(browser, Settings, button) sleep(3) # general tasks after a successful follow logger.info("--> Followed '{}'!".format(userid_to_follow.encode("utf-8"))) update_activity(Settings, 'follows') # get user ID to record alongside username user_id = get_user_id(browser, track, userid_to_follow, logger) logtime = datetime.now().strftime('%Y-%m-%d %H:%M') log_followed_pool(login, userid_to_follow, logger, logfolder, logtime, user_id) follow_restriction("write", userid_to_follow, None, logger) if blacklist['enabled'] is True: action = 'followed' add_user_to_blacklist(userid_to_follow, blacklist['campaign'], action, logger, logfolder) # get the post-follow delay time to sleep naply = get_action_delay("follow", Settings) sleep(naply) return True, "success"
def verify_action(browser, action, track, username, person, person_id, logger, logfolder): """ Verify if the action has succeeded """ # currently supported actions are follow & unfollow retry_count = 0 if action in ["follow", "unfollow"]: # assuming button_change testing is relevant to those actions only button_change = False if action == "follow": post_action_text_correct = ["Following", "Requested"] post_action_text_fail = ["Follow", "Follow Back", "Unblock"] elif action == "unfollow": post_action_text_correct = ["Follow", "Follow Back", "Unblock"] post_action_text_fail = ["Following", "Requested"] while True: # count retries at beginning retry_count += 1 # find out CURRENT follow status (this is safe as the follow button is before others) following_status, follow_button = get_following_status( browser, track, username, person, person_id, logger, logfolder) if following_status in post_action_text_correct: button_change = True elif following_status in post_action_text_fail: button_change = False else: logger.error( "Hey! Last {} is not verified out of an unexpected " "failure!".format(action)) return False, "unexpected" if button_change: break else: if retry_count == 1: reload_webpage(browser) elif retry_count == 2: # handle it! # try to do the action one more time! click_visibly(browser, follow_button) if action == "unfollow": confirm_unfollow(browser) sleep(4) elif retry_count == 3: logger.warning("Phew! Last {0} is not verified." "\t~'{1}' might be temporarily blocked " "from {0}ing\n".format(action, username)) sleep(210) return False, "temporary block" if retry_count == 2: logger.info( "Last {} is verified after reloading the page!".format(action)) return True, "success"