-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_parser_iiko.py
51 lines (46 loc) · 1.78 KB
/
data_parser_iiko.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from utils.iiko_menu import main
from dotenv import load_dotenv
from datetime import datetime
import psycopg2
import os
load_dotenv('.env')
try:
# Настройка соединение с базой данных
connection = psycopg2.connect(
database = os.environ.get("DB_NAME"),
user = os.environ.get("DB_USER"),
password = os.environ.get("DB_USER_PASSWORD"),
host = os.environ.get("DB_HOST"),
port = os.environ.get("DB_PORT")
)
connection.autocommit = True
print(connection)
print("База данных готова к работе...")
cursor = connection.cursor()
cursor.execute("SELECT * FROM products_product;")
result = cursor.fetchall()
print(result)
menu_data = main()
# Получаем информацию о продуктах
items = menu_data.get('itemCategories', [])[0].get('items')
products = []
for item in items:
price = item['itemSizes'][0].get('prices')[0].get('price')
description = item.get('description', None) # Устанавливаем значение None, если описание отсутствует
if price:
item_info = {
'title': item['name'],
'price': price,
'description': description,
}
products.append(item_info)
for product in products:
cursor.execute(f"""INSERT INTO products_product (title, description, price, image, created)
VALUES ('{product['title']}', '{product['description']}',
{product['price']}, 'no_image.jpg', '{datetime.now()}');""")
print(product)
print("Данные успешно переданы")
except Exception as error:
print(error)
finally:
connection.close()