def Insert_Secondary(driver,email): try: driver = Initiate_Settings_Page(driver) add_email_button = driver.find_element_by_xpath(CustomConstants.AddSecondaryXpath1) driver.execute_script("arguments[0].click();", add_email_button) time.sleep(5) new_email_input = driver.find_element_by_xpath(CustomConstants.AddSecondaryXpath2) new_email_input.send_keys(email) time.sleep(5) add_email_btns = driver.find_elements_by_xpath(CustomConstants.AddSecondaryXpath3) for add_email_btn in add_email_btns: try: driver.execute_script("arguments[0].click();", add_email_btn) time.sleep(5) break except: pass except Exception as e: ActionLog("[FAILED AT INSERTING SECONDARY EMAIL]") ActionLog(str(e)) return -1
def ChangePrimaryEmail(driver, current_secondary_email, password): try: driver = Initiate_Settings_Page(driver) secondary_email_input_label = driver.find_element_by_xpath( CustomConstants.ChangePrimaryXpath1.format( current_secondary_email)) secondary_email_input_label = driver.find_element_by_xpath( CustomConstants.ChangePrimaryXpath2) driver.execute_script("arguments[0].click();", secondary_email_input_label) time.sleep(2) if (ReEnterPassword(driver, password) == False): ActionLog( "[FAILED AT RE-ENTER PASSWORD WHILE CHANGING PRIMARY EMAIL]") return driver, False time.sleep(random.randint(3, 7)) return driver, True except Exception as e: ActionLog("[FAILED] [EMAIL: {} NOT ROTATED TO PRIMARY]".format( current_secondary_email)) ActionLog(str(e)) return driver, False
def InstallApp(driver, email): ActionLog("[STATUS] [STARTED INSTALLATION APP FUNCTION]") ### TRY CHECKING AVAILABILITY WITH OLD XPATH ### ### TRY CHECKING AVAILABILITY WITH OLD XPATH ### ### TRY CHECKING AVAILABILITY WITH OLD XPATH ### driver, answer = ProcessWebsite(driver, [ActiveApplication.OAuth], ActiveApplication.XPath, ActiveApplication.AppRank) if (answer['Found'] == -1): ActionLog("[FAILED AT INSTALL APPLICATION WITH OLD XPATH]", 1) ### TRY INSTALL APPLICATION WITH DEFAULT XPATH ### ### TRY INSTALL APPLICATION WITH DEFAULT XPATH ### ### TRY INSTALL APPLICATION WITH DEFAULT XPATH ### if (answer['Found'] != 1): ShutDownBrowser(driver) driver = DriverStart() driver, answer = ProcessWebsite(driver, [ActiveApplication.OAuth], CustomConstants.MakeCheckStr(), ActiveApplication.AppRank) if (answer['Found'] == -1): ActionLog("[FAILED AT INSTALL APPLICATION WITH NEW XPATH]", 1) if (answer['Found'] != 1): return -1, driver else: return str(answer['App_Name']), driver
def AddProNames(dev_mode): try: f = open(ConfigurationObject['PROCESSED_APPS'], 'a') writer = csv.writer(f, delimiter=",") cleaned = [] for attr in dir(ActiveApplication): if (attr.startswith('__')): continue cleaned.append(str(ActiveApplication[attr]).replace("\n", " ")) writer.writerow([ cleaned[0], cleaned[1], cleaned[2], cleaned[3], cleaned[4], cleaned[5], cleaned[6], str(datetime.datetime.now()), str(dev_mode) ]) f.close() return 1 except Exception as e: ActionLog(str(e)) ActionLog("[FAILED AT ADDING PROCESSED APP TO CSV]") return 0
def GetApps(): s = ConfigurationObject.URL_START e = ConfigurationObject.URL_END APPLICATIONS_FILE = ConfigurationObject.APPLICATIONS_FILE ActionLog("[LOADING APPLICATIONS_FILE] [APPLICATIONS_FILE {}]".format( APPLICATIONS_FILE)) i = 1 apps = [] with open(APPLICATIONS_FILE) as csvfile: reader = csv.reader(csvfile, delimiter=",") next(reader) for row in reader: if (i >= s and i <= e): apps.append(row) i += 1 ActionLog( "[LOADED APPLICATIONS_FILE] [APPLICATIONS_FILE PATH {}]".format( APPLICATIONS_FILE), 1) return apps
def Process_Clicks(driver, tags, url, rank, tag_skips): result = {} result['Skips'] = tag_skips result['App_Name'] = 0 result['Found'] = 0 tag_index = tag_skips tags = Reduce(tags, 50, tag_skips) for tag in tags: tag_text = tag.text tag_name = tag.tag_name try: ActionLog("Tag Text: {}".format(tag_text)) ActionLog("Tag Name: {}".format(tag_name)) handles = driver.window_handles temp_url = driver.current_url tag_index = tag_index + 1 sleep(5) driver.execute_script("arguments[0].click();", tag) sleep(5) except Exception as e: continue driver, result = FindContinue(driver, result, handles, temp_url) if (result['Found'] != 0): result['Skips'] = tag_index break return driver, result
def GetCurrentAccountState(driver, FB_PASS): driver = Initiate_Settings_Page(driver) try: parent_div = driver.find_element_by_xpath( CustomConstants.GetCurrentStateXpath1) email_divs = parent_div.find_elements_by_xpath( CustomConstants.GetCurrentStateXpath2) emails = [] for i in range(len(email_divs)): if (i % 2 != 0): text = email_divs[i].text e = text.split("\n")[0] emails.append(e) return emails except Exception as e: ActionLog("[EXPERIMENT FAILED] [GETTING CURRENT STATE FAILED]") ActionLog("[ERROR: " + str(e) + "]") TakeScreenShot( driver, CustomConstants.GetScreenShotFailedAtGetCurrentEmailStatus( secondary)) return []
def GetProcessedApps(): ##### RETURNS APP_ID and EMAIL ##### ##### RETURNS APP_ID and EMAIL ##### ##### RETURNS APP_ID and EMAIL ##### PATH = ConfigurationObject.PROCESSED_APPS names = {} ActionLog("[LOADING PROCESSED_APPS] [PROCESSED_APPS {}]".format(PATH)) if (not os.path.exists(PATH)): f = open(PATH, 'w') writer = csv.writer(f, delimiter=",") writer.writerow([ "App_ID", "Rank", "App", "Oauth", "Xpath", 'Email', 'Time', 'Dev Type' ]) f.close() with open(PATH, 'r') as f: reader = csv.reader(f, delimiter=",") next(reader) for row in reader: names[row[0]] = row[5] ActionLog("[LOADED PROCESSED_APPS] [PROCESSED_APPS {}]".format(PATH), 1) return names
def GetAppInfo(driver,ans): ActiveApplication.AppName = ans app_inf = {} app_inf['Found'] = 0 app_type = "applications" for item in CustomConstants.Pages: url = CustomConstants.FbTabAddr.format(item) driver,app_inf = ExtractInfo(driver,url) app_type = item if(app_inf['Found'] == 1): TakeScreenShot(driver,CustomConstants.GetScreenShotConfirmAtInstallApp(str(App_Name))) break if(app_inf['Found'] == 0): ActionLog("[FAILED] [COULD NOT GET APP INFO]") return driver,0 else: app_inf['Data'].append(app_type) AddResults(app_inf) ActionLog("---------------Taking a break of 10-15 minutes----------------") ActionLog(str(datetime.datetime.now())) time.sleep(random.randint(350,600)) return driver,1
def CheckStatus(driver): primary_email, current_emails = GetCurrentAccountState( driver, ConfigurationObject.FB_PASS) ActionLog("[STATUS] [FACEBOOK ACCOUNT CURRENT STATE]") ActionLog("[CURRENT PRIMARY] [" + str(primary_email) + "]") ActionLog("[CURRENT PRIMARY] [" + str(len(current_emails)) + "]") return primary_email, current_emails
def VerifyExperimentType(EXPERIMENT_TYPE): ##### VERIFY EXPERIMENT INITIATED CORRECT ##### ##### VERIFY EXPERIMENT INITIATED CORRECT ##### ##### VERIFY EXPERIMENT INITIATED CORRECT ##### if (not (EXPERIMENT_TYPE == CustomConstants.LinearExperiment or EXPERIMENT_TYPE == CustomConstants.MatrixExperiment)): ActionLog( "[ERROR CONFIGURATION] [EXPERIMENT TYPE IS POORLY CONFIGURED]") ActionLog("[EXITING WITH ERROR]", 1) return 0 return 1
def VerifyDeployementFolder(DEPLOYMENT_TYPE): ##### UPDATE CLASS VARIABLES WITH CONFIG ##### ##### UPDATE CLASS VARIABLES WITH CONFIG ##### ##### UPDATE CLASS VARIABLES WITH CONFIG ##### ConfigurationObject.DeploymentType = DEPLOYMENT_TYPE if (not os.path.exists(DEPLOYMENT_TYPE)): ActionLog( "[ERROR FILE SETUP] [PLEASE CREATE DEPLOYMENT DIRECTORY {}] [UPDATE FOLDER WITH FILES]" .format(DEPLOYMENT_TYPE), 1) ActionLog("[EXITING WITH ERROR]", 1) return 0 return 1
def InitiatingConfirmation(driver): try: driver = Initiate_Settings_Page(driver) confirm_button = driver.find_element_by_xpath(CustomConstants.AddSecondaryXpath4) driver.execute_script("arguments[0].click();", confirm_button) ActionLog("[STATUS] [WAITING FOR CONFIRMATION CODE]") time.sleep(60) return 0 except Exception as e: ActionLog("[FAILED AT INITIATING CONFIRMATION WHILE ADDING SECONDARY EMAIL]") ActionLog(str(e)) return -1
def ProcessContinueTag(driver, result): button_not_div = 1 try: try: Install_btn = driver.find_element_by_xpath( CustomConstants.INSTALLATION_XPATH1) except: Install_btn = driver.find_element_by_xpath( CustomConstants.INSTALLATION_XPATH2) button_not_div = 0 try: try: App_Name = driver.find_element_by_xpath( CustomConstants.INSTALLATION_XPATH3) App_Name = str(App_Name.text).strip() except: App_Name = driver.find_element_by_xpath( CustomConstants.INSTALLATION_XPATH4) App_Name = (App_Name.text).split("'s")[0] driver.execute_script("arguments[0].click();", Install_btn) result['App_Name'] = App_Name result['Found'] = 1 sleep(30) if (button_not_div == 1): driver = PressContinueTag(driver, CustomConstants.INSTALLATION_XPATH5) else: driver = PressContinueTag(driver, CustomConstants.INSTALLATION_XPATH6) return driver, result except Exception as e: ActionLog("[FAILED AT FINDING APP NAME IN FIND CONTINUE FUNCTION]") ActionLog(str(e)) result['Found'] = -1 return driver, result except Exception as e: driver = CloseWindowsExceptFirst(driver) ActionLog("[FAILED] [COULD NOT FIND CONTINUE AS BUTTON ANYWHERE]") ActionLog(str(e)) result['Found'] = -1 return driver, result
def RotateEmail(driver,secondary,FB_PASS): ################################################################################### ################################# EMAIL ROTATION ################################## ################################################################################### ActionLog("[EMAIL ROTATION STARTED]") driver, ans = ChangePrimaryEmail(driver, secondary, FB_PASS) if(ans == False): TakeScreenShot(driver,CustomConstants.GetScreenShotFailedAtChangePrimary(app_id)) return driver,-1 else: ActionLog("EMAIL ROTATION COMPLETE") TakeScreenShot(driver,CustomConstants.GetScreenShotSuccessAtChangePrimary(app_id)) time.sleep(random.randint(30,40)) return driver,1
def CleanCheckStr(check_str): try: c = check_str parts = c.split("[") tag_name = parts[0] parts = parts[1].split(") or") parts[-1] = parts[-1].replace("]", "") parts[-1] = parts[-1] + ")" parts[-1] = parts[-1].replace("))", "") new_check_str = tag_name + "[" arr = [] for p in parts: p = p.strip(" ") p = p + ")" q = (p.split("'")[1]) if (q != ""): arr.append(p) conds = " or ".join(arr) new_check_str += conds + "]" except: ActionLog("[FAILED AT CLEANING OLD STRING]") return check_str return new_check_str
def IsOauth(driver, url): result = {} result['Found'] = 0 result['Skips'] = 0 result['App_Name'] = 0 try: driver.get(url) time.sleep(3) curr_url = driver.current_url handles = driver.window_handles if (CustomConstants.URL_KEYWORD in curr_url): driver, result = FindContinue(driver, result, 0, handles, 0) return driver, result return driver, result except Exception as e: ActionLog("[ERROR WHILE OPENING OAUTH URL] [{}]".format(url)) time.sleep(3) result['Found'] = -1 return result
def ClickRemove(driver,emails_ignore): driver = Initiate_Settings_Page(driver) email_items = driver.find_element_by_xpath(CustomConstants.RemoveAllSecondaryXpath1) for email in email_items: txt = str(email.text.split("\n")[0]) if(txt not in emails_ignore): try: parent = driver.find_element_by_xpath(CustomConstants.RemoveAllSecondaryXpath2.format(txt)) remtag = parent.find_elements_by_xpath(CustomConstants.RemoveAllSecondaryXpath3)[-1] driver.execute_script("arguments[0].click();", remtag) time.sleep(random.randint(3,9)) except Exception as e: ActionLog("[FAILED] [WHILE CLICKING REMOVE IN REMOVING ALL SECONDARY]") ActionLog(str(e)) return driver,-1 return driver,1
def Load_Config(): config_filename = "config_email_server.json" fpath = os.path.join(ConfigurationObject.DEPLOYMENT_TYPE, ConfigurationObject.CONFIG_PATH, config_filename) config = {} ActionLog("[LOADING DATA] [LOADING EMAIL SERVER CONFIG FILE {}]".format( config_filename)) with open(fpath, 'r') as file: config = json.load(file) ActionLog("[LOADED DATA] [EMAIL SERVER CONFIGURATION DATA LOADED]") return config
def CheckIfTextPresentOnPage(driver, txt): try: driver.find_element_by_xpath( CustomConstants.TextTempXpath.format("*", txt)) return True except Exception as e: ActionLog(str(e), 0) return False
def LoadConfigData(config_filename): fpath = os.path.join(ConfigurationObject.DEPLOYMENT_TYPE, ConfigurationObject.CONFIG_PATH, config_filename) ActionLog( "[LOADING DATA] [LOADING CONFIG FILE {}]".format(config_filename)) with open(fpath, 'r') as file: config = json.load(file) ActionLog("[LOADED DATA] [CONFIGURATION DATA LOADED]") ConfigurationObject.SetConfigurationObj(config) LogClass(ConfigurationObject) return config
def Account_Login(driver): is_logged_in = False log_in_retries = 5 while (log_in_retries >= 0): if (LoginToFacebook(ConfigurationObject.FB_USER, ConfigurationObject.FB_PASS, driver)): is_logged_in = True break log_in_retries -= 1 if (is_logged_in == False): ActionLog("[LOGIN ATTEMPT FAILED] [EXITING]") TakeScreenShot( driver, CustomConstants.GetScreenShotFailedAtLoginPrefix(FB_USER)) return 0 ActionLog("[LOGIN ATTEMPT SUCCESSFUL]") return 1
def AddingConfirmationCode(email): retry = 5 code = 0 while(retry > 0): try: ActionLog("[STATUS] [STARTED RETRIEVING CONFIRMATION CODE TRY: {}]".format(str(retry)),1) code = GetConfirmationCode(email) except Exception as e: ActionLog("[FAILED WHILE RETRIEVING CONFIRMATION CODE RETRY NUMBER: {}]".format(str(retry))) ActionLog(str(e)) if code != 0 and code != None: code_input = driver.find_element_by_xpath(CustomConstants.ConfirmationCodeXpath1) ActionLog("[SERVER CODE RETRIEVED] [CODE IS: {}]".format(str(code))) code_input.send_keys(code) time.sleep(3) code_input.send_keys(Keys.ENTER) time.sleep(3) time.sleep(random.randint(10,15)) break else: retry -= 1 ##### IF NO MORE RETRIES LEFT, REMOVE THE EMAIL CONFIRMATION ##### ##### IF NO MORE RETRIES LEFT, REMOVE THE EMAIL CONFIRMATION ##### ##### IF NO MORE RETRIES LEFT, REMOVE THE EMAIL CONFIRMATION ##### if(retry == 0): try: driver = Initiate_Settings_Page(driver) time.sleep(random.randint(5,15)) remove_pending_btn = driver.find_element_by_xpath(CustomConstants.ConfirmationCodeXpath2) driver.execute_script("arguments[0].click();", remove_pending_btn) time.sleep(random.randint(10,12)) except: ActionLog("[FAILED WHILE REMOVING PENDING CONFIRMATION]") return -1 time.sleep(random.randint(60,90)) return 0
def HandleNoPopUp(driver, temp_url, result): ActionLog( "[STATUS] [HANDLING NON-POP UP] [SHIFTING TO WINDOW WITH URL {}]". format(driver.current_url)) try: sleep(5) if (CustomConstants.URL_KEYWORD in driver.current_url): driver, result = ProcessContinueTag(driver, result) if (result['Found'] == -1): result['Found'] = 2 return driver, result except Exception as e: ActionLog( "[FAILED IN HANDLING NO-POP UP. CHECK DRIVER CLOSE / MANAGEMENT]") ActionLog(str(e)) result['Found'] = -1 return driver, result
def ReEnterPassword(driver, password): try: time.sleep(10) re_enter_password_div = driver.find_element_by_xpath( CustomConstants.RenterPasswordXpath) try: password_field = driver.find_element_by_id('ajax_password') password_field.send_keys(password) time.sleep(3) password_field.send_keys(Keys.ENTER) time.sleep(3) return True except Exception as e: ActionLog(str(e)) return False except Exception as e: ActionLog("[STATUS] [PASSWORD CHANGE NOT REQUIRED]") return True
def HandlePopUp(driver, _handles, result): ActionLog( "[STATUS] [HANDLING POP UP] [SHIFTING TO WINDOW WITH URL {}]".format( driver.current_url)) try: time.sleep(5) driver.switch_to.window(_handles[-1]) if (CustomConstants.URL_KEYWORD in driver.current_url): driver, result = ProcessContinueTag(driver, result) return driver, result else: driver = CloseWindowsExceptFirst(driver) return driver, result except Exception as e: ActionLog( "[FAILED IN HANDLING POP UP. CHECK DRIVER CLOSE / MANAGEMENT]") ActionLog(str(e)) result['Found'] = -1 return driver, result
def RemoveAllSecondary(driver,secondary=None): ActionLog("[STATUS] [REMOVING ALL UNWANTED EMAILS]",1) try: driver = Initiate_Settings_Page(driver) emails_ignore = {} emails_ignore[ConfigurationObject.FB_USER] = 1 if secondary != None: emails_ignore[secondary] = 1 except Exception as e: ActionLog("[FAILED] [FAILED AT STARTING ON REMOVING ALL SECONDARY EMAILS]") return driver,-1 driver,ans = ChangePrimaryEmail(driver,ConfigurationObject.FB_USER) if(ans == False): return driver,-1 driver,ans = ClickRemove(driver,emails_ignore) if(ans == -1): return driver,-1 time.sleep(random.randint(4,16)) return driver,1
def FindContinue(driver, result, handles, temp_url='null'): _handles = driver.window_handles if (len(_handles) > 2): ActionLog("[FAILED] [TOO MANY WINDOWS IN FINDING CONTINUE BUTTON]") result['Found'] = -1 return driver, result if (len(handles) != len(_handles)): driver, result = HandlePopUp(driver, _handles, result) else: driver, result = HandleNoPopUp(driver, temp_url, result) return driver, result
def AddSecondary(driver, email, fbUser, password): ######### ADD EMAIL AS SECONDARY ########## ######### ADD EMAIL AS SECONDARY ########## ######### ADD EMAIL AS SECONDARY ########## ActionLog("[ADDING NEW SECONDARY EMAIL] [{}]".format(email),1) if(Insert_Secondary(driver,email) == -1): return -1 if(ReEnterPassword(driver, password) == False): ActionLog("[FAILED AT RE-ENTER PASSWORD WHILE ADDING SECONDARY EMAIL]") return -1 if(VerifyInsertion(driver,email) == False): return -1 if(InitiatingConfirmation(driver) == -1): return -1 if(AddingConfirmationCode(email) == -1): return -1
def AddSecondaryEmail(current_emails,secondary,driver): if current_emails[0] == secondary: ActionLog("[NEW EMAIL CHANGE NOT REQUIRED] [PRIMARY EMAIL IS: {}]".format(secondary),1) return 1,driver else: if secondary in current_emails: ActionLog("[NEW EMAIL IS ADDED, NEEDS ROTATION]",1) else: process_status = AddSecondary(driver,secondary,ConfigurationObject.FB_USER,ConfigurationObject.FB_PASS) if(process_status == -1): TakeScreenShot(driver,CustomConstants.GetScreenShotFailedAtAddSecondary(secondary)) return 0,driver ActionLog("[STATUS] [SECONDARY EMAIL ADDED]",1) TakeScreenShot(driver,CustomConstants.GetScreenShotSuccessAtAddSecondary(secondary)) time.sleep(random.randint(30,40)) driver, ans = RotateEmail(driver,secondary,FB_PASS) if(ans == -1): return 0,driver return 1,driver