#!/usr/bin/env python # coding: utf-8 # In[1]: import os from google.cloud import pubsub_v1 # In[2]: # Set the project ID and topic name project_id = 'price-extraction-project' topic_name = 'nfl-zipcodes' # In[3]: # Create a publisher client publisher = pubsub_v1.PublisherClient() # In[4]: # Get the topic path topic_path = publisher.topic_path(project_id, topic_name) # In[5]: # Define the zip codes for all NFL cities nfl_zipcodes = { 'Arizona Cardinals': '85305', 'Atlanta Falcons': '30363', 'Baltimore Ravens': '21230', 'Buffalo Bills': '14127', 'Carolina Panthers': '28202', 'Chicago Bears': '60605', 'Cincinnati Bengals': '45202', 'Cleveland Browns': '44115', 'Dallas Cowboys': '76011', 'Denver Broncos': '80204', 'Detroit Lions': '48226', 'Green Bay Packers': '54304', 'Houston Texans': '77054', 'Indianapolis Colts': '46225', 'Jacksonville Jaguars': '32202', 'Kansas City Chiefs': '64129', 'Las Vegas Raiders': '89118', 'Los Angeles Chargers': '92626', 'Los Angeles Rams': '90305', 'Miami Dolphins': '33056', 'Minnesota Vikings': '55415', 'New England Patriots': '02035', 'New Orleans Saints': '70112', 'New York Giants': '07073', 'New York Jets': '07932', 'Philadelphia Eagles': '19145', 'Pittsburgh Steelers': '15212', 'San Francisco 49ers': '95054', 'Seattle Seahawks': '98056', 'Tampa Bay Buccaneers': '33607', 'Tennessee Titans': '37213', 'Washington Football Team': '20785' } # In[6]: # Publish each zip code to the topic for team, zipcode in nfl_zipcodes.items(): data = f'{team} - {zipcode}'.encode('utf-8') publisher.publish(topic_path, data=data) print('All zip codes published.') # In[ ]: import os import time from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from google.cloud import pubsub_v1 # Set the project ID and subscription name project_id = 'price-extraction-project' topic_name = 'nfl-zipcodes' subscription_name = 'sloetjes' # Set the path to your ChromeDriver executable chrome_driver_path = "/Users/sloetjeswong/Downloads/chromedriver_mac_arm64/chromedriver" # Set up a Google Cloud Pub/Sub subscriber subscriber = pubsub_v1.SubscriberClient() subscription_path = subscriber.subscription_path(project_id, subscription_name) # Set up a ChromeDriver instance chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--headless") # Run Chrome in headless mode chrome_service = Service(chrome_driver_path) chrome_service.start() driver = webdriver.Remote(chrome_service.service_url, options=chrome_options) def callback(message): # Get the zip code from the message zip_code = message.data.decode("utf-8") print(f"Received message with zip code: {zip_code}") # Open the product page for the rice crackers for the given zip code product_url = f"https://www.sayweee.com/zh/product/Want-Want-Senbei-Rice-Crackers/17119?trace_id=89f7c636-05b8-4f52-abdd-74de3a83f45d&zip={zip_code}" driver.get(product_url) # Wait for the price to load price_xpath = "//span[@class='price']" price_element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, price_xpath))) # Extract the price and print it price = price_element.text print(f"Price for zip code {zip_code}: {price}") # Acknowledge the message message.ack() # Subscribe to the topic streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback) print(f"Listening for messages on {subscription_path}...") # Keep the script running while True: time.sleep(60)