This fixes issue #1 #2

Open
j wants to merge 123 commits from Branch_Karen into main
Showing only changes of commit 528d134c65 - Show all commits
+19 -8
View File
@@ -1,4 +1,5 @@
from google.cloud import pubsub_v1 from google.cloud import pubsub_v1
from google.api_core.exceptions import DeadlineExceeded
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
@@ -24,7 +25,6 @@ project_id = "fp-20230425"
subscription_name = "fp-subscription-NFL-zipcodes" subscription_name = "fp-subscription-NFL-zipcodes"
subscriber = pubsub_v1.SubscriberClient() subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_name) subscription_path = subscriber.subscription_path(project_id, subscription_name)
timeout = 5
# Load the product page and extract the __NEXT_DATA__ data # Load the product page and extract the __NEXT_DATA__ data
url = 'https://www.sayweee.com/en/product/Want-Want-Senbei-Rice-Crackers/17119' url = 'https://www.sayweee.com/en/product/Want-Want-Senbei-Rice-Crackers/17119'
@@ -71,12 +71,12 @@ def callback(message):
try: try:
time.sleep(5) time.sleep(1)
# Clear any notice # 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") 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() notice_close.click()
time.sleep(2) time.sleep(1)
except: except:
pass pass
@@ -84,7 +84,7 @@ def callback(message):
zip_button = driver.find_element(By.XPATH, '/html/body/div[2]/div/header/div[3]/div/div/div[2]/div[1]') zip_button = driver.find_element(By.XPATH, '/html/body/div[2]/div/header/div[3]/div/div/div[2]/div[1]')
# print(f"{zip_button:}") # print(f"{zip_button:}")
zip_button.click() zip_button.click()
time.sleep(2) time.sleep(1)
zip_input = driver.find_element(By.XPATH, '//div/label/following-sibling::input') zip_input = driver.find_element(By.XPATH, '//div/label/following-sibling::input')
# print(f"{zip_input:}") # print(f"{zip_input:}")
@@ -127,6 +127,7 @@ def callback(message):
# Print the price # Print the price
print(price) print(price)
subscriber.acknowledge(ack_ids = [message.ack_id], subscription = subscription_path) # acknowledge receipt of message
# Append the zip code and price to the dataframe # Append the zip code and price to the dataframe
#append_to_df(zip_code, price) #append_to_df(zip_code, price)
new_row = pd.DataFrame({'Zip Code': [zip_code], 'Price': [price]}) new_row = pd.DataFrame({'Zip Code': [zip_code], 'Price': [price]})
@@ -134,19 +135,29 @@ def callback(message):
#print(f"Recorded the zip code and price: {zip_code},{price}") #print(f"Recorded the zip code and price: {zip_code},{price}")
#print(f"Updated a dataframe: {df}") #print(f"Updated a dataframe: {df}")
subscriber.acknowledge(ack_ids = [message.ack_id], subscription = subscription_path) # acknowledge receipt of message
except Exception as e: except Exception as e:
print(f"Caught exception: {e}") print(f"Caught exception: {e}")
print(f"Price not found for zip code {zip_code}") print(f"Price not found for zip code {zip_code}")
price = 'Price not found' price = 'Price not found'
return df return df
import time
# Start receiving messages (sync version) # Start receiving messages (sync version)
while True: while True:
pull_request = pubsub_v1.types.PullRequest(max_messages = 1, subscription = subscription_path) pull_request = pubsub_v1.types.PullRequest(max_messages = 1, subscription = subscription_path)
pull_response = subscriber.pull(request = pull_request) messages = []
df = callback(pull_response.received_messages[0]) try:
print(f"Updated a dataframe: {df}") pull_response = subscriber.pull(request = pull_request)
messages = pull_response.received_messages
except DeadlineExceeded:
pass
if messages:
df = callback(messages[0])
print(f"Updated a dataframe: {df}")
else:
print(f"no messages, trying again at {time.asctime()}")
# Start receiving messages (async version) # Start receiving messages (async version)
#flow_control = pubsub_v1.types.FlowControl(max_messages = 1) #flow_control = pubsub_v1.types.FlowControl(max_messages = 1)