import requests, os, json, time from slugify import slugify SITE_URL = "https://your-site.com" TOROB_API = "https://api.torob.com/update_products" API_KEY = "کلید-ترب-شما" PAGES_DIR = "./pages" IMAGE_DIR = "./images" PRODUCTS_FILE = "products.json" os.makedirs(PAGES_DIR, exist_ok=True) os.makedirs(IMAGE_DIR, exist_ok=True) # خواندن محصولات def load_products(): if os.path.exists(PRODUCTS_FILE): with open(PRODUCTS_FILE,'r',encoding='utf-8') as f: return json.load(f) return [] def save_products(products): with open(PRODUCTS_FILE,'w',encoding='utf-8') as f: json.dump(products,f,ensure_ascii=False, indent=2) # دانلود تصویر 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']) img_path = download_image(p.get('image',''), slug) filename = os.path.join(PAGES_DIR,f"{slug}.html") html = f"""

{p['name']}

{p[

قیمت: {p['price']} تومان

موجودی: {p['stock']}

{p.get('description','')}

دسته: {p.get('category','')}

""" with open(filename,'w',encoding='utf-8') as f: f.write(html) # بروزرسانی صفحه اصلی def update_main_page(products): html = "" html += "

جدیدترین محصولات

پرفروش‌ترین‌ها

پیشنهاد ویژه

" with open(os.path.join(PAGES_DIR,"index.html"),'w',encoding='utf-8') as f: f.write(html) # ارسال به ترب def sync_torob(products): for p in products: data = { "product_id": p['id'], "name": p['name'], "price": p['price'], "stock": p['stock'], "url": f"{SITE_URL}/products/{slugify(p['name'])}" } headers = {"Authorization": f"Bearer {API_KEY}"} try: requests.post(TOROB_API,json=data,headers=headers) except: pass # اجرای ربات def run_bot(): products = load_products() for p in products: create_product_page(p) update_main_page(products) sync_torob(products) print("Bot executed successfully!") # زمان‌بندی (اختیاری: می‌توان با Cron اجرا شود) if __name__=="__main__": while True: run_bot() time.sleep(14400) # 4 ساعت
جستجو در محصولات
کالایی در این صفحه موجود نیست
    پروفایلسبد خریددسته‌بندیخانه