wendy.py (7853B)
1 # Python modules. 2 import random as r 3 from datetime import date 4 import os 5 6 # Selenium modules. 7 from selenium import webdriver 8 from selenium.webdriver.chrome.service import Service as ChromeService 9 from selenium.webdriver.common.by import By 10 from selenium.webdriver.support.ui import Select 11 from selenium.webdriver.common.keys import Keys 12 13 # Fake agent module 14 from fake_useragent import UserAgent 15 16 # Load in .env file with secret restaurant. 17 from dotenv import load_dotenv 18 load_dotenv('.env') 19 20 # Constants for the survey. 21 SURVEY_WEBSITE = 'https://wendyswantstoknow.com/' 22 CHROME_DRIVER_PATH = '/opt/homebrew/bin/chromedriver' 23 RESTAURANT_NUMBER = os.environ.get("WENDY_CODE") 24 25 # Date and time constants. 26 MAX_MINUTE_TIME = 59 27 MIN_MINUTE_TIME = 0 28 MAX_HOUR_TIME = 12 29 MIN_HOUR_TIME = 7 30 31 # Used for the implicitly_wait function. Delay the time of responses. 32 DELAY_TIME = 1 33 34 # ID's for specific question. 35 GENDER_ID = "R000037" 36 AGE_ID = "R000038" 37 HOUSE_HOLD_INCOME_ID = "R000039" 38 39 def setup_selenium(): 40 global driver 41 42 print("Loading up Selenium...") 43 44 # Options for google chrome. 45 options = webdriver.ChromeOptions() 46 options.add_argument("window-size=1000,750") # The window size of the Chrome Browser. 47 options.add_experimental_option('excludeSwitches', ['enable-logging']) 48 49 # Generate a random user agent. 50 ua = UserAgent(verify_ssl=False) 51 user_agent = ua.random 52 print("Using FAKE user agent: " + "\n" + user_agent) 53 options.add_argument("user-agent=" + user_agent) 54 55 # Locate the path to the chrome driver and run it. 56 chrome_service = ChromeService(executable_path=CHROME_DRIVER_PATH) 57 driver = webdriver.Chrome(service=chrome_service, options=options) 58 print("Selenium loaded.") 59 60 # Get the survey page. 61 driver.get(SURVEY_WEBSITE) 62 print("Survey page loaded.") 63 64 # Wait for the content of the page to load. 65 driver.implicitly_wait(DELAY_TIME) 66 67 68 def information_from_receipt(): 69 print("Filling out receipt information...") 70 71 # Put the restaurant number in the text box. 72 driver.find_element(By.ID, 'InputStoreNum').send_keys(RESTAURANT_NUMBER) 73 74 # Generate a date for the survey that's between the first day of current month and present day. 75 random_date = str(date.today().month) + "/" + str(r.randint(1, date.today().day-1)) + "/" + str(date.today().year) 76 driver.execute_script("document.getElementsByClassName('datePickerBox')[0].removeAttribute('readonly')") 77 driver.find_element(By.ID, 'Index_VisitDateDatePicker').send_keys(random_date) 78 79 # Choose a random visiting hour in the morning. 80 driver.find_element(By.ID, 'InputHour').send_keys(str(r.randint(MIN_HOUR_TIME, MAX_HOUR_TIME)).zfill(2)) 81 driver.find_element(By.ID, 'InputMinute').send_keys(str(r.randint(MIN_MINUTE_TIME, MAX_MINUTE_TIME)).zfill(2)) 82 driver.find_element(By.ID, 'InputMeridian').send_keys('AM') 83 84 # Complete the introduction. 85 driver.find_element(By.ID, "NextButton").click() 86 87 88 def fill_out_survey(): 89 print("Filling out survey...") 90 91 # Confirm that the store is correct. 92 driver.find_elements(By.CLASS_NAME, "radioSimpleInput")[0].click() 93 next_button = driver.find_element(By.ID, "NextButton") 94 next_button.click() 95 96 97 next_button = driver.find_elements(By.ID, "NextButton") 98 while len(next_button) > 0: 99 xpath_expression = "" 100 101 # Select the multiple choice questions. 102 multiple_choice = driver.find_elements(By.CLASS_NAME, "radioSimpleInput") 103 104 # Question: Did you have a bad experience? 105 experience = driver.find_elements(By.ID, "textR000017") 106 107 # Choice where you placed an order with an employee. 108 employee = driver.find_elements(By.XPATH, "//label[@for='R000108.1']") 109 110 # Choice where you ordered your food through dine in. 111 dine_in = driver.find_elements(By.XPATH, "//label[@for='R000006.1']") 112 113 # Select text box to write a good generic comment. 114 text_area = driver.find_elements(By.ID, "S000024") 115 116 # Question that ask the customer yes or no questions. 117 yes_no_table = driver.find_elements(By.CLASS_NAME, "YesNoASCQuestion") 118 119 # Select the best option on the table questions (highly satifised). 120 xpath_expression = '//td[@class="HighlySatisfiedNeitherDESCQuestion" or "HighlyLikelyDESCQuestion"]' 121 122 # Check if the highly satisfied table exist. 123 best_option_table = driver.find_elements(By.XPATH, xpath_expression) 124 125 # Options that need specific answers. 126 if experience: 127 multiple_choice[1].click() 128 elif employee: 129 employee[0].click() 130 elif dine_in: 131 dine_in[0].click() 132 elif text_area: 133 try: 134 text_area = text_area[0] 135 text_area.send_keys(Keys.TAB) 136 text_area.clear() 137 text_area.send_keys(r.choice(list(open('good_comments.txt')))) 138 except FileNotFoundError: 139 print("[ERROR] File not found. Skipping writing a comment.") 140 elif yes_no_table: 141 xpath_expression = "//td[@class='Opt1 inputtyperbloption']//span[@class='radioSimpleInput']" 142 best_option = driver.find_elements(By.XPATH, xpath_expression) 143 for i in range(len(best_option)): 144 best_option[i].click() 145 elif best_option_table: 146 # If the highly satisfied table exist, choose the best option. 147 xpath_expression = "//td[@class='Opt5 inputtyperbloption']//span[@class='radioSimpleInput']" 148 best_option = driver.find_elements(By.XPATH, xpath_expression) 149 for i in range(len(best_option)): 150 best_option[i].click() 151 else: 152 # If does not meet any creteria above choose a random option. 153 multiple_choice[r.randint(0, len(multiple_choice))-1].click() 154 155 # Click the next button. 156 next_button = driver.find_elements(By.ID, "NextButton") 157 158 # Reached the end page where you choose question. 159 fill_out_data = driver.find_elements(By.ID, "FNSBlock1200") 160 if len(fill_out_data) > 0: 161 break 162 163 # Proceed to the next page. 164 next_button[0].click() 165 166 # Select a randomized attributes of a person. 167 randomize_person_option() 168 169 # No employee went above and beyond. 170 multiple_choice = driver.find_elements(By.CLASS_NAME, "radioSimpleInput") 171 multiple_choice[1].click() 172 driver.find_element(By.ID, "NextButton").click() 173 174 # I don't want no cash rewards. 175 multiple_choice = driver.find_elements(By.CLASS_NAME, "radioSimpleInput") 176 multiple_choice[1].click() 177 driver.find_element(By.ID, "NextButton").click() 178 179 180 def randomize_person_option(): 181 print("Randomizing person options...") 182 183 # Randomize the person's gender. 184 Select(driver.find_element(By.ID, GENDER_ID)).select_by_value(str(r.randint(1,2))) 185 186 # Randomize the person's age. 187 Select(driver.find_element(By.ID, AGE_ID)).select_by_value(str(r.randint(2,6))) 188 189 # Randomize the person's house hold income. 190 Select(driver.find_element(By.ID, HOUSE_HOLD_INCOME_ID)).select_by_value(str(r.randint(1,6))) 191 192 193 def save_validation_code(): 194 print("Saving validation code...") 195 # Save the validation code at the end. 196 valid_code = driver.find_elements(By.CLASS_NAME, "ValCode")[0] 197 with open('validation_code.txt', 'a') as f: 198 f.write(str(valid_code.get_attribute("textContent")) + "\n") 199 200 201 def main(): 202 setup_selenium() 203 information_from_receipt() 204 fill_out_survey() 205 save_validation_code() 206 print("Done.") 207 driver.quit() 208 209 if __name__ == '__main__': 210 main() 211 212