kroger/kroger.py

105 lines
3.0 KiB
Python
Raw Normal View History

2024-09-02 14:31:11 +00:00
import sys
2024-09-01 22:52:41 +00:00
import config
2024-09-02 14:31:11 +00:00
import sqlite3
2024-09-01 22:52:41 +00:00
import requests
2024-09-02 14:31:11 +00:00
from base64 import b64encode
2024-09-01 22:52:41 +00:00
from datetime import datetime
2024-09-02 14:31:11 +00:00
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()
2024-09-01 22:52:41 +00:00
def get_token():
2024-09-02 14:31:11 +00:00
headers = {
'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={
2024-09-01 22:52:41 +00:00
'grant_type': 'client_credentials',
'scope': 'product.compact'
2024-09-02 14:31:11 +00:00
})
2024-09-01 22:52:41 +00:00
response.raise_for_status()
return response.json()['access_token']
2024-09-02 14:31:11 +00:00
def get_stores(token, zip_code):
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):
2024-09-01 22:52:41 +00:00
headers = {'Authorization': f'Bearer {token}'}
params = {
'filter.term': 'eggs',
2024-09-02 14:31:11 +00:00
'filter.locationId': location_id,
2024-09-01 22:52:41 +00:00
'filter.limit': 10
}
response = requests.get('https://api.kroger.com/v1/products', headers=headers, params=params)
response.raise_for_status()
return response.json()['data']
2024-09-02 14:31:11 +00:00
def db_save(products, store_info):
2024-09-01 22:52:41 +00:00
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS prices (
id INTEGER PRIMARY KEY AUTOINCREMENT,
2024-09-02 14:31:11 +00:00
date TEXT,
store_id INTEGER,
store_name TEXT,
store_address TEXT,
store_city TEXT,
store_state TEXT,
store_zip TEXT,
product_id INTEGER,
2024-09-01 22:52:41 +00:00
product_name TEXT,
2024-09-02 14:31:11 +00:00
price REAL
2024-09-01 22:52:41 +00:00
)
''')
2024-09-02 14:31:11 +00:00
for item in products:
product_id = item['productId']
2024-09-01 22:52:41 +00:00
product_name = item['description']
price = item['items'][0]['price']['regular']
date = datetime.now().strftime('%Y-%m-%d')
2024-09-02 14:31:11 +00:00
cursor.execute('INSERT INTO prices (date, store_id, store_name, store_address, store_city, store_state, store_zip, product_id, product_name, price) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
(date, store_info['id'], store_info['name'], store_info['address'], store_info['city'], store_info['state'], store_info['zip'], product_id, product_name, price))
2024-09-01 22:52:41 +00:00
conn.commit()
conn.close()
2024-09-02 14:31:11 +00:00
def price_check():
2024-09-01 22:52:41 +00:00
token = get_token()
2024-09-02 14:31:11 +00:00
stores = get_stores(token, zip_code)
for store in stores:
store_info = {
'id': store['locationId'],
'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)
2024-09-01 22:52:41 +00:00
if __name__ == '__main__':
2024-09-02 14:31:11 +00:00
price_check()
sys.exit(0)