This fixes issue #1 #2

Open
j wants to merge 123 commits from Branch_Karen into main
Showing only changes of commit ed955015fe - Show all commits
+18 -14
View File
@@ -5,6 +5,7 @@ from selenium.webdriver.common.by import By
import json import json
import time import time
import os import os
from concurrent.futures import TimeoutError
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "C:/Users/Karen/AppData/Roaming/gcloud/application_default_credentials.json" os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "C:/Users/Karen/AppData/Roaming/gcloud/application_default_credentials.json"
@@ -18,6 +19,7 @@ project_id = "fp-20230425"
subscription_name = "fp-subscription-zipcodes" subscription_name = "fp-subscription-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'
@@ -30,14 +32,13 @@ if next_data_element is not None:
next_data_end_index = next_data_text.rindex('}') + 1 next_data_end_index = next_data_text.rindex('}') + 1
next_data_json = next_data_text[next_data_start_index:next_data_end_index] next_data_json = next_data_text[next_data_start_index:next_data_end_index]
next_data = json.loads(next_data_json) next_data = json.loads(next_data_json)
price = next_data['offers']['price']
else: else:
print('__NEXT_DATA__ element not found') print('__NEXT_DATA__ element not found')
# Define callback function for Pub/Sub messages # Define callback function for Pub/Sub messages
def callback(message): def callback(message):
# Extract zip code from message data # Extract zip code from message data
zip_code = message.data.decode('utf-8').strip() zip_code = message.data.decode('utf-8')
print(f"Received zip code: {zip_code}") print(f"Received zip code: {zip_code}")
# Enter zip code in the input field and click the "Check" button # Enter zip code in the input field and click the "Check" button
@@ -47,28 +48,31 @@ def callback(message):
check_button = driver.find_element(By.XPATH, '/html/body/div[4]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div/div/div[1]/div/div/div[3]/div') check_button = driver.find_element(By.XPATH, '/html/body/div[4]/div/div[2]/div/div[2]/div[2]/div/div[1]/form/div/div/div[1]/div/div/div[3]/div')
check_button.click() check_button.click()
time.sleep(2) time.sleep(2)
'''
# Extract the price of rice crackers for this zip code # Extract the price of rice crackers for this zip code
try: try:
price_element = driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/div[1]/div/div/div[2]/div[2]/div[1]/div[4]/div[1]/div') price_element = driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/div[1]/div/div/div[2]/div[2]/div[1]/div[4]/div[1]/div')
price = price_element.text.strip()[1:] price = price_element.text.strip()[1:]
print(price)
except: except:
price = 'Price not found' price = 'Price not found'
''' finally:
print(next_data) message.ack() # acknowledge receipt of message
price = next_data['offers']['price']
# Print the price
print(price)
# Print the zip code and its corresponding price
print(f'{zip_code}: {price}')
message.ack() # acknowledge receipt of message
# Start receiving messages # Start receiving messages
subscriber.subscribe(subscription_path, callback=callback) streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f'Started receiving messages from subscription {subscription_path}...') print(f'Started receiving messages from subscription {subscription_path}...')
# Wrap subscriber in a 'with' block to automatically call close() when done.
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.
# Keep the script running indefinitely to continue receiving messages # Keep the script running indefinitely to continue receiving messages
while True: while True:
time.sleep(10) time.sleep(10)