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 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
from __future__ import annotations

from io import StringIO
import csv
import json
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional

import requests
from bs4 import BeautifulSoup
from fastapi import FastAPI, HTTPException
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field
from urllib.parse import urlencode


app = FastAPI(title="Nike Scraper API", version="1.0.0")

NIKE_BASE_SEARCH = "https://www.nike.com/w"
NIKE_BASE_URL = "https://www.nike.com"

CATEGORY_ALIASES = {
    "t-shirt": "t-shirt",
    "tee": "t-shirt",
    "shirt": "shirt",
    "hoodie": "hoodie",
    "sweatshirt": "sweatshirt",
    "jacket": "jacket",
    "gilet": "gilet",
    "top": "top",
    "tank": "tank top",
    "polo": "polo",
    "jersey": "jersey",
    "bra": "sports bra",
    "pant": "pants",
    "pants": "pants",
    "trousers": "trousers",
    "shorts": "shorts",
    "short": "shorts",
    "leggings": "leggings",
    "tights": "tights",
    "joggers": "joggers",
    "sweatpants": "sweatpants",
    "skirt": "skirt",
    "dress": "dress",
    "tracksuit": "tracksuit",
    "jumpsuit": "jumpsuit",
    "socks": "socks",
    "sock": "socks",
    "hat": "hat",
    "cap": "cap",
    "bag": "bag",
    "backpack": "backpack",
}

HEADERS = {
    "User-Agent": (
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
        "AppleWebKit/537.36 (KHTML, like Gecko) "
        "Chrome/123.0.0.0 Safari/537.36"
    )
}

CATEGORIES = [
    "sweaters",
    "hoodies",
    "t-shirts",
    "jackets",
    "shirts",
    "crews",
    "jerseys",
    "tops",
    "polos",
    "tanks",
    "compression",
    "baselayer",
    "jeans",
    "shorts",
    "skirts",
    "tights",
    "parkas",
    "gilets",
    "pants",
    "leggings",
    "trousers",
    "joggers",
    "sweatpants",
    "dresses",
    "rompers",
    "jumpsuits",
    "onesies",
    "overalls",
    "tracksuits",
    "sneakers",
    "slippers",
    "sunglasses",
    "bras",
    "socks",
    "hats",
    "bags",
    "backpacks",
]

SCRAPE_OUTPUT_DIR = Path(__file__).resolve().parent / "scraped_json"


class Recommendation(BaseModel):
    color: str = Field(..., min_length=1)
    category: str = Field(..., min_length=1)
    gender: Optional[str] = Field(default=None, description="men or women")


class ScrapeRequest(BaseModel):
    recommendation: Recommendation
    max_products: int = Field(default=30, ge=1, le=300)


def _ensure_full_url(href: str) -> str:
    if href.startswith("/"):
        return f"{NIKE_BASE_URL}{href}"
    return href


def build_nike_search_url(color: str, category: str, gender: Optional[str] = None) -> str:
    category_normalized = CATEGORY_ALIASES.get(category.lower(), category.lower())
    parts: list[str] = []
    if gender:
        parts.append(gender.lower() + "s")
    parts.append(color.lower())
    parts.append(category_normalized)
    query = " ".join(parts)
    params = urlencode({"q": query, "vst": query})
    return f"{NIKE_BASE_SEARCH}?{params}"


def build_nike_urls_from_recommendation(recommendation: Recommendation) -> list[str]:
    color = recommendation.color
    category = recommendation.category
    gender = recommendation.gender
    if gender:
        return [build_nike_search_url(color, category, gender)]
    return [
        build_nike_search_url(color, category, "men"),
        build_nike_search_url(color, category, "women"),
        build_nike_search_url(color, category),
    ]


def build_search_urls_from_recommendation(recommendation: Recommendation, store: str = "nike") -> list[str]:
    return build_nike_urls_from_recommendation(recommendation)


def build_search_urls_from_query(query: str, store: str = "nike", gender: Optional[str] = None) -> list[str]:
    normalized_query = str(query or "").strip()
    if not normalized_query:
        return []

    def _normalize_prefixed_query(prefix: str, value: str) -> str:
        lowered = value.strip().lower()
        p = prefix.strip().lower()
        if lowered.startswith(f"{p} "):
            return value.strip()
        return f"{prefix} {value}".strip()

    if gender:
        q = _normalize_prefixed_query(gender, normalized_query)
        return [f"{NIKE_BASE_SEARCH}?{urlencode({'q': q, 'vst': q})}"]

    return [
        f"{NIKE_BASE_SEARCH}?{urlencode({'q': f'men {normalized_query}'.strip(), 'vst': f'men {normalized_query}'.strip()})}",
        f"{NIKE_BASE_SEARCH}?{urlencode({'q': f'women {normalized_query}'.strip(), 'vst': f'women {normalized_query}'.strip()})}",
        f"{NIKE_BASE_SEARCH}?{urlencode({'q': normalized_query, 'vst': normalized_query})}",
    ]


def _get_soup(url: str) -> BeautifulSoup:
    response = requests.get(url, headers=HEADERS, timeout=20)
    response.raise_for_status()
    return BeautifulSoup(response.content, "lxml")


def _ensure_store_url(href: str, base_url: str) -> str:
    if not href:
        return ""
    if href.startswith("//"):
        return f"https:{href}"
    if href.startswith("/"):
        return f"{base_url}{href}"
    return href


def extract_product_urls(search_url: str) -> list[str]:
    soup = _get_soup(search_url)
    product_links: list[str] = []

    anchors = soup.find_all("a", {"class": "product-card__link-overlay"})
    for anchor in anchors:
        href = anchor.get("href")
        if href:
            full = _ensure_full_url(href)
            if full not in product_links:
                product_links.append(full)

    if not product_links:
        all_anchors = soup.find_all("a", href=True)
        for anchor in all_anchors:
            href = anchor.get("href")
            if href and "/t/" in href:
                full = _ensure_full_url(href)
                if full not in product_links:
                    product_links.append(full)

    return product_links


def _extract_image_from_container(container: BeautifulSoup) -> str:
    img = container.find("img")
    if not img:
        return ""
    return str(img.get("src") or img.get("data-src") or img.get("srcset") or "").strip()


def extract_product_summaries(search_url: str, store: str = "nike") -> list[dict[str, str]]:
    soup = _get_soup(search_url)
    summaries: list[dict[str, str]] = []
    seen_links: set[str] = set()

    containers = soup.find_all("div", {"class": "product-card__body"})
    for container in containers:
        anchor = container.find("a", {"class": "product-card__link-overlay"})
        if not anchor:
            continue

        href = anchor.get("href")
        if not href:
            continue

        item_link = _ensure_full_url(href)
        if item_link in seen_links:
            continue
        seen_links.add(item_link)

        title = get_title(container)
        current_price, _ = get_prices(container)
        image_url = _extract_image_from_container(container.parent if container.parent else container)

        summaries.append(
            {
                "item_link": item_link,
                "name": title,
                "price": current_price,
                "image_url": image_url,
            }
        )

    if summaries:
        return summaries

    # Fallback path when Nike card markup changes.
    for item_link in extract_product_urls(search_url):
        if item_link in seen_links:
            continue
        seen_links.add(item_link)
        summaries.append(
            {
                "item_link": item_link,
                "name": "N/A",
                "price": "N/A",
                "image_url": "",
            }
        )
    return summaries


def get_title(container: BeautifulSoup) -> str:
    try:
        title = container.find_all("div", {"class": "product-card__title"})[0].text
        subtitle = container.find_all("div", {"class": "product-card__subtitle"})[0].text
        return f"{title} {subtitle}".strip()
    except (IndexError, AttributeError):
        return "N/A"


def get_target_gender(title: str) -> str:
    if "Men's" in title:
        return "Men"
    if "Women's" in title:
        return "Women"
    return "Unisex"


def get_subcategory(title: str) -> str:
    for word in title.split(" "):
        candidate = word.lower().strip(",.")
        if candidate in CATEGORIES or (candidate + "s") in CATEGORIES:
            return word
    return ""


def get_prices(container: BeautifulSoup) -> tuple[str, str]:
    try:
        price_container = container.find_all("div", {"class": "product-price__wrapper"})
        current_price = price_container[0].text
        old_price = "N/A"

        if current_price.count("$") == 2:
            prices = current_price.split("$")
            current_price = "$" + prices[1] if "." in prices[1] else "$" + prices[1] + ".00"
            old_price = "$" + prices[2] if "." in prices[2] else "$" + prices[2] + ".00"
        elif "." not in current_price:
            current_price = current_price + ".00"
    except (IndexError, AttributeError):
        current_price, old_price = "N/A", "N/A"

    return current_price, old_price


def get_item_image_link(item_soup: BeautifulSoup) -> str:
    try:
        img = item_soup.find("img", {"class": "css-viwop1 u-full-width u-full-height css-m5dkrx"})
        return img.get("src") if img else "Click on item link for pictures."
    except (IndexError, AttributeError):
        return "Click on item link for pictures."


def get_colors(item_soup: BeautifulSoup) -> str:
    try:
        current = item_soup.find_all(
            "div",
            {
                "class": "colorway-product-overlay colorway-product-overlay--active "
                "colorway-product-overlay--selected css-sa2cc9"
            },
        )
        if current:
            colors = current[0].find_all("img", alt=True)[0].get("alt")
            for color in item_soup.find_all("div", {"class": "colorway-product-overlay css-sa2cc9"}):
                alt = color.find_all("img", alt=True)[0].get("alt")
                if alt != "Design your own Nike By You product":
                    colors += " || " + alt
        else:
            color_li = item_soup.find_all("li", {"class": "description-preview__color-description ncss-li"})
            colors = str(color_li).split(": ")[1].replace("</li>]", "")
    except (IndexError, AttributeError):
        colors = "Click on item link for available colors."
    return colors


def scrape_products(search_urls: list[str], max_products: int) -> list[dict[str, str]]:
    items: list[dict[str, str]] = []
    seen_links: set[str] = set()

    for link in search_urls:
        soup = _get_soup(link)
        containers = soup.find_all("div", {"class": "product-card__body"})

        for container in containers:
            if len(items) >= max_products:
                return items

            anchor = container.find("a", {"class": "product-card__link-overlay"})
            if not anchor:
                continue
            href = anchor.get("href")
            if not href:
                continue

            item_link = _ensure_full_url(href)
            if item_link in seen_links:
                continue
            seen_links.add(item_link)

            title = get_title(container)
            gender = get_target_gender(title)
            current_price, old_price = get_prices(container)
            subcategory = get_subcategory(title)

            image_link = "Click on item link for pictures."
            colors = "Click on item link for available colors."
            try:
                item_soup = _get_soup(item_link)
                image_link = get_item_image_link(item_soup)
                colors = get_colors(item_soup)
            except requests.RequestException:
                pass

            items.append(
                {
                    "name": title,
                    "gender": gender,
                    "price": current_price,
                    "sale_price": old_price,
                    "colors": colors,
                    "item_link": item_link,
                    "image_link": image_link,
                    "subcategory": subcategory,
                    "brand": "Nike",
                }
            )

    return items


def _build_csv(products: list[dict[str, str]]) -> str:
    output = StringIO()
    writer = csv.DictWriter(
        output,
        fieldnames=[
            "name",
            "gender",
            "price",
            "sale_price",
            "colors",
            "item_link",
            "image_link",
            "subcategory",
            "brand",
        ],
    )
    writer.writeheader()
    writer.writerows(products)
    return output.getvalue()


def _save_json_payload(prefix: str, payload: dict[str, object]) -> str:
    SCRAPE_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
    ts = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
    filename = f"{prefix}_{ts}.json"
    file_path = SCRAPE_OUTPUT_DIR / filename
    with file_path.open("w", encoding="utf-8") as f:
        json.dump(payload, f, ensure_ascii=True, indent=2)
    return str(file_path)


@app.get("/health")
def health() -> dict[str, str]:
    return {"status": "ok"}


@app.get("/")
def root() -> dict[str, str]:
    return {
        "message": "Nike Scraper API is running.",
        "docs": "/docs",
        "health": "/health",
    }


@app.post("/search-urls")
def search_urls(payload: Recommendation) -> dict[str, list[str]]:
    return {"search_urls": build_nike_urls_from_recommendation(payload)}


@app.post("/product-urls")
def product_urls(payload: Recommendation) -> dict[str, object]:
    try:
        urls = build_nike_urls_from_recommendation(payload)
        all_products: list[dict[str, str]] = []
        seen_links: set[str] = set()
        for url in urls:
            for product in extract_product_summaries(url):
                link = product.get("item_link", "")
                if not link or link in seen_links:
                    continue
                seen_links.add(link)
                all_products.append(product)

        response_payload: dict[str, object] = {
            "product_urls": [item["item_link"] for item in all_products],
            "products": all_products,
        }
        response_payload["saved_json_path"] = _save_json_payload("product_urls", response_payload)
        return response_payload
    except requests.RequestException as exc:
        raise HTTPException(status_code=502, detail=f"Failed to fetch Nike pages: {exc}") from exc


@app.post("/scrape")
def scrape(payload: ScrapeRequest) -> dict[str, object]:
    try:
        search_urls = build_nike_urls_from_recommendation(payload.recommendation)
        products = scrape_products(search_urls, max_products=payload.max_products)
    except requests.RequestException as exc:
        raise HTTPException(status_code=502, detail=f"Failed to fetch Nike pages: {exc}") from exc

    response_payload: dict[str, object] = {
        "search_urls": search_urls,
        "count": len(products),
        "products": products,
    }
    return response_payload


@app.post("/scrape.csv")
def scrape_csv(payload: ScrapeRequest) -> StreamingResponse:
    try:
        search_urls = build_nike_urls_from_recommendation(payload.recommendation)
        products = scrape_products(search_urls, max_products=payload.max_products)
    except requests.RequestException as exc:
        raise HTTPException(status_code=502, detail=f"Failed to fetch Nike pages: {exc}") from exc

    csv_content = _build_csv(products)
    filename = (
        f"nike_{payload.recommendation.gender or 'unisex'}_"
        f"{payload.recommendation.color}_{payload.recommendation.category}.csv"
    )
    return StreamingResponse(
        iter([csv_content]),
        media_type="text/csv",
        headers={"Content-Disposition": f"attachment; filename={filename}"},
    )