import requests
from bs4 import BeautifulSoup
import re
import random

# قائمة User-Agent شائعة


def get_seller_info(url):
    USER_AGENTS = [
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/114.0.0.0 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 Version/14.0 Safari/605.1.15",
        "Mozilla/5.0 (X11; Linux x86_64) Gecko/20100101 Firefox/89.0",
        "Mozilla/5.0 (iPhone; CPU iPhone OS 14_2 like Mac OS X) AppleWebKit/605.1.15 Version/14.0 Mobile Safari/604.1",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0"
    ]
    headers = {
        "User-Agent": random.choice(USER_AGENTS)
    }

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        soup = BeautifulSoup(response.text, "html.parser")

        data = {}

        # seller name and url
        container = soup.select_one(".x-sellercard-atf__info__about-seller")
        if container:
            a_tag = container.find("a", href=True)
            if a_tag:
                data["seller"] = a_tag.get_text(strip=True)
                data["seller_url"] = a_tag["href"]

        # ratings count
        ratings_tag = soup.select_one(".x-sellercard-atf__about-seller")
        if ratings_tag:
            match = re.search(r"\((\d+)\)", ratings_tag.get_text())
            if match:
                data["ratings_count"] = int(match.group(1))

        # store highlights
        highlights = soup.select_one("h4.x-store-information__highlights")
        if highlights:
            # positive feedback %
            pf_tag = highlights.select_one(".ux-textspans")
            if pf_tag:
                match = re.search(r"(\d+(\.\d+)?)%", pf_tag.get_text())
                if match:
                    data["positive_feedback"] = match.group(1) + "%"

            # quantity sold
            sold_tag = highlights.select_one(".ux-textspans.ux-textspans--SECONDARY")
            if sold_tag:
                match = re.search(r"(\d[\d,]*)", sold_tag.get_text())
                if match:
                    data["quantity_sold"] = int(match.group(1).replace(",", ""))

        return data

    return {}

# استخدام الدالة
url = "https://www.ebay.com/itm/305491177374"
result = get_seller_info(url)
print(result)
