import time import random import cloudscraper import re import json import os import requests TELEGRAM_TOKEN = "your_token_here" CHAT_ID = "your_chat_id_here" TESLA_URL = "https://www.tesla.com/tr_TR/inventory/new/my?arrangeby=plh&zip=&range=0" SEEN_VINS_FILE= "seen_vins.txt" scraper = cloudscraper.create_scraper( browser={'browser': 'chrome', 'platform': 'windows'}, delay=5 ) HEADERS = { "User-Agent": ( "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " "AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/114.0.0.0 Safari/537.36" ), "Accept-Language": "tr-TR,tr;q=0.9", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9", "Referer": "https://www.tesla.com/" } def notify_telegram_with_button(msg, btn_text, btn_url): payload = { "chat_id": CHAT_ID, "text": msg, "parse_mode": "HTML", "reply_markup": json.dumps({ "inline_keyboard": [[{"text": btn_text, "url": btn_url}]] }) } requests.post( f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage", data=payload, headers=HEADERS, timeout=30 ) def load_seen_vins(): if not os.path.exists(SEEN_VINS_FILE): return set() return set(open(SEEN_VINS_FILE).read().splitlines()) def save_seen_vins(vins): with open(SEEN_VINS_FILE, "w") as f: f.write("\n".join(vins)) def get_page(url): time.sleep(random.uniform(10, 60)) try: return scraper.get(url, headers=HEADERS, timeout=60) except Exception as e: print("Request failed:", e) notify_telegram_with_button(f"⚠️ Failed to fetch Tesla page: {e}", "Retry", TESLA_URL) return None def check_inventory(): resp = get_page(TESLA_URL) if not resp or resp.status_code in (403,429) or "captcha" in resp.text.lower(): notify_telegram_with_button(f"⚠️ Bot likely detected. Status: {getattr(resp,'status_code','N/A')}", "Open Tesla", TESLA_URL) return try: m = re.search(r'"results"\s*:\s*(\[\{.*?\}\])', resp.text, re.DOTALL) cars = json.loads(m.group(1)) if m else [] seen = load_seen_vins() for car in cars: vin = car.get("vin") if not vin or vin in seen: continue price = car.get("price","N/A") ext = car.get("paint",{}).get("value","N/A").upper() inn = car.get("interior",{}).get("value","N/A").upper() link= f"https://www.tesla.com/tr_TR/my/order/{vin}" msg = (f"🚨 NEW CAR!\n" f"💰 Price: {price:,} TL\n" f"🎨 Exterior: {ext}\n" f"🪑 Interior: {inn}\n" f"🔑 VIN: {vin}") notify_telegram_with_button(msg,"🚗 ORDER NOW",link) seen.add(vin) save_seen_vins(seen) except Exception as e: print("Parsing error:",e) notify_telegram_with_button("❌ Parsing error occurred.","Retry",TESLA_URL) if __name__=="__main__": check_inventory()