def comment_image(browser, username, comments, blacklist, logger, logfolder, Settings): """Checks if it should comment on the image""" # check action availability if quota_supervisor(Settings, "comments") == "jump": return False, "jumped" rand_comment = random.choice(comments).format(username) rand_comment = emoji.demojize(rand_comment) rand_comment = emoji.emojize(rand_comment, use_aliases=True) open_comment_section(browser, logger) comment_input = get_comment_input(browser) try: if len(comment_input) > 0: comment_input[0].clear() comment_input = get_comment_input(browser) # below, an extra space is added to force # the input box to update the reactJS core comment_to_be_sent = rand_comment + " " browser.execute_script( "arguments[0].value = arguments[1];", comment_input[0], comment_to_be_sent, ) # below, it also will remove that extra space added above # COS '\b' is a backspace char in ASCII comment_input[0].send_keys("\b") comment_input = get_comment_input(browser) comment_input[0].submit() update_activity(Settings, "comments") if blacklist["enabled"] is True: action = "commented" add_user_to_blacklist( username, blacklist["campaign"], action, logger, logfolder ) else: logger.warning( "--> Comment Action Likely Failed!" "\t~comment Element was not found" ) return False, "commenting disabled" except InvalidElementStateException: logger.warning( "--> Comment Action Likely Failed!" "\t~encountered `InvalidElementStateException` :/" ) return False, "invalid element state" logger.info("--> Commented: {}".format(rand_comment.encode("utf-8"))) # get the post-comment delay time to sleep naply = get_action_delay("comment", Settings) sleep(naply) return True, "success"
def like_image(browser, username, blacklist, logger, logfolder, Settings): """Likes the browser opened image""" # check action availability if quota_supervisor(Settings, "likes") == "jump": return False, "jumped" like_xpath = "//section/span/button/span[@aria-label='Like']" unlike_xpath = "//section/span/button/span[@aria-label='Unlike']" # find first for like element like_elem = browser.find_elements_by_xpath(like_xpath) if len(like_elem) == 1: # sleep real quick right before clicking the element sleep(2) click_element(browser, Settings, like_elem[0]) # check now we have unlike instead of like liked_elem = browser.find_elements_by_xpath(unlike_xpath) if len(liked_elem) == 1: logger.info('--> Image Liked!') update_activity(Settings, 'likes') if blacklist['enabled'] is True: action = 'liked' add_user_to_blacklist(username, blacklist['campaign'], action, logger, logfolder) # get the post-like delay time to sleep naply = get_action_delay("like", Settings) sleep(naply) return True, "success" else: # if like not seceded wait for 2 min logger.info('--> Image was not able to get Liked! maybe blocked ?') sleep(120) else: liked_elem = browser.find_elements_by_xpath(unlike_xpath) if len(liked_elem) == 1: logger.info('--> Image already liked!') return False, "already liked" logger.info('--> Invalid Like Element!') return False, "invalid element"
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"