import requests from bs4 import BeautifulSoup import json, os, time from slugify import slugify # تنظیمات SITE_URL = "https://your-site.com" PRODUCTS_API = f"{SITE_URL}/api/products" TOROB_API = "https://api.torob.com/update_products" API_KEY = "کلید-ترب-شما" PAGES_DIR = "./pages" IMAGE_DIR = "./images" MAIN_PAGE = "./pages/index.html" os.makedirs(PAGES_DIR, exist_ok=True) os.makedirs(IMAGE_DIR, exist_ok=True) # دریافت محصولات def get_products(): try: r = requests.get(PRODUCTS_API) r.raise_for_status() return r.json() except: return [] # دانلود تصویر و ایجاد alt def download_image(url, slug): if not url: return "" try: path = os.path.join(IMAGE_DIR, f"{slug}.jpg") img = requests.get(url).content with open(path, 'wb') as f: f.write(img) return path except: return "" # ایجاد صفحه محصول def create_product_page(p): slug = slugify(p['name']) image_path = download_image(p.get('image',''), slug) filename = os.path.join(PAGES_DIR, f"{slug}.html") html = f"""
قیمت: {p['price']} تومان
موجودی: {p['stock']}
{p.get('description','')}
""" with open(filename,'w',encoding='utf-8') as f: f.write(html) # ساخت صفحه اصلی خودکار def update_main_page(products): html = "" html += "🎁 کد تخفیف ویژه اولین خرید: naniwa | ارسال سریع به سراسر کشور 🚀.