diff --git a/fp2_11.py b/fp2_11.py index 65313b8..f0564e6 100644 --- a/fp2_11.py +++ b/fp2_11.py @@ -49,7 +49,8 @@ except: # Define callback function for Pub/Sub messages def callback(message): # Extract zip code from message data - zip_code = message.data.decode('utf-8') + zip_code = message.message.data.decode('utf-8') + print(f"Received zip code: {zip_code}") try: @@ -57,7 +58,7 @@ def callback(message): time.sleep(5) # Clear any modal dialog modal_close = driver.find_element(By.XPATH, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-icon-close-s ')]") - print(f"modal_close = {modal_close}") + # print(f"modal_close = {modal_close}") modal_close.click() time.sleep(2) except: @@ -68,7 +69,7 @@ def callback(message): time.sleep(5) # Clear any notice notice_close = driver.find_element(By.XPATH, "/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[1]/div[2]/button") - print(f"notice_close = {notice_close}") + # print(f"notice_close = {notice_close}") notice_close.click() time.sleep(2) except: @@ -76,18 +77,34 @@ def callback(message): # Enter zip code in the input field and click the "Check" button zip_button = driver.find_element(By.XPATH, '/html/body/div[2]/div/header/div[3]/div/div/div[2]/div[1]') - print(zip_button) + # print(f"{zip_button:}") zip_button.click() time.sleep(2) zip_input = driver.find_element(By.XPATH, '//div/label/following-sibling::input') - zip_input.clear() + # print(f"{zip_input:}") + zip_input.click() + # print("clearing zip_input") + zip_input.send_keys("\b\b\b\b\b") + # print("sending zip code") zip_input.send_keys(zip_code) check_button = driver.find_element(By.XPATH, "//button[text() = 'Confirm']") + # print(f"{check_button:}") check_button.click() time.sleep(2) - + + try: + time.sleep(5) + # Clear any notice + notice_close = driver.find_element(By.XPATH, "/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[1]/div[2]/button") + print(f"notice_close = {notice_close}") + notice_close.click() + time.sleep(2) + except: + pass + + # Extract the price of rice crackers for this zip code ''' @@ -104,26 +121,36 @@ def callback(message): price = next_data['offers']['price'] # Print the price print(price) - message.ack() # acknowledge receipt of message - except: + subscriber.acknowledge(ack_ids = [message.ack_id], subscription = subscription_path) # acknowledge receipt of message + except Exception as e: + print(f"Caught exception: {e}") print(f"Price not found for zip code {zip_code}") price = 'Price not found' +# Start receiving messages (sync version) +while True: + pull_request = pubsub_v1.types.PullRequest(max_messages = 1, subscription = subscription_path) + pull_response = subscriber.pull(request = pull_request) + callback(pull_response.received_messages[0]) -# Start receiving messages -streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback) -print(f'Started receiving messages from subscription {subscription_path}...') +# Start receiving messages (async version) +#flow_control = pubsub_v1.types.FlowControl(max_messages = 1) +#streaming_pull_future = subscriber.subscribe(subscription_path, +# callback=callback, +# flow_control=flow_control) + +# print(f'Started receiving messages from subscription {subscription_path}...') # Wrap subscriber in a 'with' block to automatically call close() when done. -with subscriber: - try: +# with subscriber: +# try: # When `timeout` is not set, result() will block indefinitely, # unless an exception is encountered first. - streaming_pull_future.result(timeout=timeout) - except TimeoutError: - streaming_pull_future.cancel() # Trigger the shutdown. - streaming_pull_future.result() # Block until the shutdown is complete. +# streaming_pull_future.result(timeout=timeout) +# except TimeoutError: +# streaming_pull_future.cancel() # Trigger the shutdown. +# streaming_pull_future.result() # Block until the shutdown is complete. # Keep the script running indefinitely to continue receiving messages -while True: - time.sleep(10) +#while True: +# time.sleep(10)