more updates

This commit is contained in:
crate 2024-09-02 09:31:11 -05:00
parent 5a2e594451
commit f34100947c

View File

@ -1,28 +1,51 @@
import sys
import config import config
import requests
import schedule
import time
from datetime import datetime
import sqlite3 import sqlite3
import requests
from base64 import b64encode
from datetime import datetime
DATABASE = 'prices.db'
client_id = config.CLIENT_ID
client_secret = config.CLIENT_SECRET
zip_code = 38138
auth = b64encode(f"{client_id}:{client_secret}".encode()).decode()
DATABASE = 'kroger_prices.db'
def get_token(): def get_token():
"""Authenticate with Kroger API and get access token.""" headers = {
response = requests.post(config.TOKEN_URL, data={ 'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': f'Basic {auth}'
}
response = requests.post('https://api.kroger.com/v1/connect/oauth2/token', headers=headers, data={
'grant_type': 'client_credentials', 'grant_type': 'client_credentials',
'scope': 'product.compact' 'scope': 'product.compact'
}, auth=(config.CLIENT_ID, config.CLIENT_SECRET)) })
response.raise_for_status() response.raise_for_status()
return response.json()['access_token'] return response.json()['access_token']
def get_prices(token, zip=config.ZIP_CODE): def get_stores(token, zip_code):
"""Fetch egg prices from Kroger API.""" headers = {'Authorization': f'Bearer {token}'}
params = {
'filter.zipCode.near': zip_code,
'filter.limit': 50
}
response = requests.get('https://api.kroger.com/v1/locations', headers=headers, params=params)
response.raise_for_status()
return response.json()['data']
def get_prices(token, location_id):
headers = {'Authorization': f'Bearer {token}'} headers = {'Authorization': f'Bearer {token}'}
params = { params = {
'filter.term': 'eggs', 'filter.term': 'eggs',
'filter.locationId': zip, 'filter.locationId': location_id,
'filter.limit': 10 'filter.limit': 10
} }
@ -30,46 +53,53 @@ def get_prices(token, zip=config.ZIP_CODE):
response.raise_for_status() response.raise_for_status()
return response.json()['data'] return response.json()['data']
def db_save(data): def db_save(products, store_info):
"""Save product data to SQLite database."""
conn = sqlite3.connect(DATABASE) conn = sqlite3.connect(DATABASE)
cursor = conn.cursor() cursor = conn.cursor()
# Create table if not exists
cursor.execute(''' cursor.execute('''
CREATE TABLE IF NOT EXISTS prices ( CREATE TABLE IF NOT EXISTS prices (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT,
store_id INTEGER,
store_name TEXT,
store_address TEXT,
store_city TEXT,
store_state TEXT,
store_zip TEXT,
product_id INTEGER,
product_name TEXT, product_name TEXT,
price REAL, price REAL
currency TEXT,
date TEXT
) )
''') ''')
# Insert data for item in products:
for item in data: product_id = item['productId']
product_name = item['description'] product_name = item['description']
price = item['items'][0]['price']['regular'] price = item['items'][0]['price']['regular']
currency = item['items'][0]['price']['currency']
date = datetime.now().strftime('%Y-%m-%d') date = datetime.now().strftime('%Y-%m-%d')
cursor.execute('INSERT INTO prices (product_name, price, currency, date) VALUES (?, ?, ?, ?)', cursor.execute('INSERT INTO prices (date, store_id, store_name, store_address, store_city, store_state, store_zip, product_id, product_name, price) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
(product_name, price, currency, date)) (date, store_info['id'], store_info['name'], store_info['address'], store_info['city'], store_info['state'], store_info['zip'], product_id, product_name, price))
conn.commit() conn.commit()
conn.close() conn.close()
def daily_check(): def price_check():
"""Perform daily check for egg prices."""
token = get_token() token = get_token()
egg_price = get_prices(token) stores = get_stores(token, zip_code)
db_save(egg_price) for store in stores:
store_info = {
# Schedule the script to run daily at the specified time 'id': store['locationId'],
# schedule.every().day.at(config.CHECK_TIME).do(daily_check) 'name': store['name'],
'address': store['address']['addressLine1'],
'city': store['address']['city'],
'state': store['address']['state'],
'zip': store['address']['zipCode']
}
eggs = get_prices(token, store_info['id'])
db_save(eggs, store_info)
if __name__ == '__main__': if __name__ == '__main__':
while True: price_check()
daily_check() sys.exit(0)
# schedule.run_pending()
# time.sleep(60)