File size: 1,809 Bytes
0ee2df7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
eBay -- Finding API (App ID only, JSON response).
"""
import httpx
from supabase_client import require_tool_keys

BASE = "https://svcs.ebay.com/services/search/FindingService/v1"


def run(params: dict) -> dict:
    creds = require_tool_keys("ebay")
    app_id = creds["api_key"]

    keywords = params.get("keywords")
    if not keywords:
        raise ValueError("keywords is required")

    operation = "findCompletedItems" if params.get("sold_only") else "findItemsByKeywords"
    query = {
        "OPERATION-NAME": operation,
        "SERVICE-VERSION": "1.13.0",
        "SECURITY-APPNAME": app_id,
        "RESPONSE-DATA-FORMAT": "JSON",
        "keywords": keywords,
        "paginationInput.entriesPerPage": min(int(params.get("max_results", 25)), 100),
    }
    if params.get("condition"):
        query["itemFilter(0).name"] = "Condition"
        query["itemFilter(0).value"] = params["condition"]

    resp = httpx.get(BASE, params=query, timeout=15.0)
    resp.raise_for_status()
    data = resp.json()

    key = f"{operation}Response"
    items = []
    try:
        items = data[key][0]["searchResult"][0].get("item", [])
    except Exception:
        pass

    results = []
    for it in items:
        price = it.get("sellingStatus", [{}])[0].get("currentPrice", [{}])[0]
        results.append(
            {
                "title": it.get("title", [None])[0],
                "price": price.get("__value__"),
                "currency": price.get("@currencyId"),
                "condition": it.get("condition", [{}])[0].get("conditionDisplayName", [None])[0],
                "end_time": it.get("listingInfo", [{}])[0].get("endTime", [None])[0],
                "url": it.get("viewItemURL", [None])[0],
            }
        )
    return {"count": len(results), "results": results}