wstkeys/API/ggprice.py

129 lines
2.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sqlite3
import requests
import time
# ======================
# CONFIG
# ======================
DB_PATH = "rokky.db"
API_URL = "https://seller.ggsel.com/api_sellers/api/product/edit/prices"
TOKEN = "YOUR_TOKEN"
BATCH_SIZE = 200 # сколько товаров за раз отправлять
SLEEP_BETWEEN = 1 # задержка между батчами (сек)
# ======================
# DB
# ======================
def get_products_for_update(conn):
cursor = conn.cursor()
cursor.execute("""
SELECT product_id, price
FROM products
WHERE product_id IS NOT NULL
AND price IS NOT NULL
""")
return cursor.fetchall()
# ======================
# BUILD PAYLOAD
# ======================
def build_payload(rows):
payload = []
for product_id, price in rows:
payload.append({
"product_id": int(product_id),
"price": float(price)
})
return payload
# ======================
# CHUNKING
# ======================
def chunked(data, size):
for i in range(0, len(data), size):
yield data[i:i + size]
# ======================
# SEND REQUEST
# ======================
def send_prices(payload):
try:
response = requests.post(
f"{API_URL}?token={TOKEN}",
json=payload,
timeout=30
)
print("STATUS:", response.status_code)
if response.status_code != 200:
print("ERROR:", response.text)
return None
data = response.json()
print("RESPONSE:", data)
return data
except Exception as e:
print("REQUEST ERROR:", e)
return None
# ======================
# SYNC
# ======================
def sync_prices():
conn = sqlite3.connect(DB_PATH)
rows = get_products_for_update(conn)
payload = build_payload(rows)
total = len(payload)
print(f"Всего товаров: {total}")
if total == 0:
print("Нет данных для обновления")
return
sent = 0
for batch in chunked(payload, BATCH_SIZE):
print(f"\nОтправка: {sent}{sent + len(batch)}")
result = send_prices(batch)
if result is None:
print("Ошибка при отправке, стоп")
break
sent += len(batch)
time.sleep(SLEEP_BETWEEN)
print(f"\nГотово. Отправлено: {sent}/{total}")
conn.close()
# ======================
# RUN
# ======================
if __name__ == "__main__":
sync_prices()