Spaces:
Sleeping
Sleeping
Download app.py from bugrahans/zepto-scraper: direct link, hf CLI and curl.
- Browser
- Download file 24.9 kB
-
https://huggingface.co/spaces/bugrahans/zepto-scraper/resolve/main/app.py
- Command line
-
hf download hf://spaces/bugrahans/zepto-scraper/app.py
-
curl -L -o app.py https://huggingface.co/spaces/bugrahans/zepto-scraper/resolve/main/app.py
24.9 kB
| from __future__ import annotations | |
| import csv | |
| import html | |
| import tempfile | |
| from typing import Any, Dict, List, Optional, Tuple | |
| import gradio as gr | |
| from tool import ( | |
| DEFAULT_PRODUCT_URL, | |
| DEFAULT_SUBCATEGORY_URL, | |
| MAX_SUBCATEGORIES, | |
| SCRAPE_DO_SIGNUP_URL, | |
| ScrapeDoZeptoTool, | |
| ) | |
| # ZeroGPU hardware refuses to keep a Space alive unless it finds at least one | |
| # @spaces.GPU function at startup. This app is pure HTTP and never touches a GPU, | |
| # so the probe below exists only to satisfy that check. It is never called. | |
| try: | |
| import spaces | |
| def _zerogpu_probe() -> str: | |
| return "ok" | |
| except ImportError: | |
| pass | |
| NO_TOKEN_MD = ( | |
| "### Paste your Scrape.do API token above\n\n" | |
| f"No token yet? [Create a free account]({SCRAPE_DO_SIGNUP_URL}) — " | |
| "1,000 free credits a month, no credit card. Every Zepto page costs 10 credits." | |
| ) | |
| # --------------------------------------------------------------------- helpers | |
| def _esc(value: Any) -> str: | |
| return html.escape("" if value is None else str(value), quote=True) | |
| def _safe_link(url: Any) -> str: | |
| url = str(url or "") | |
| return url if url.startswith(("http://", "https://")) else "" | |
| def _num(value: Any) -> Optional[float]: | |
| if isinstance(value, bool): | |
| return None | |
| try: | |
| return float(value) | |
| except (TypeError, ValueError): | |
| return None | |
| def _inr(value: Any) -> str: | |
| number = _num(value) | |
| if number is None: | |
| return "" | |
| return f"₹{number:,.0f}" if number == int(number) else f"₹{number:,.2f}" | |
| def _fmt_int(value: Any) -> str: | |
| number = _num(value) | |
| return f"{int(number):,}" if number is not None else _esc(value) | |
| def _tool_or_none(token: str) -> Optional[ScrapeDoZeptoTool]: | |
| if not token or not token.strip(): | |
| return None | |
| return ScrapeDoZeptoTool(api_token=token.strip()) | |
| def _write_csv(rows: List[Dict[str, Any]], prefix: str) -> Optional[str]: | |
| if not rows: | |
| return None | |
| fieldnames: List[str] = [] | |
| for row in rows: | |
| for key in row: | |
| if key not in fieldnames: | |
| fieldnames.append(key) | |
| handle = tempfile.NamedTemporaryFile( | |
| mode="w", suffix=".csv", prefix=f"{prefix}-", delete=False, newline="", encoding="utf-8-sig", | |
| ) | |
| writer = csv.DictWriter(handle, fieldnames=fieldnames) | |
| writer.writeheader() | |
| for row in rows: | |
| writer.writerow({key: row.get(key, "") for key in fieldnames}) | |
| handle.close() | |
| return handle.name | |
| def _error_md(data: Dict[str, Any]) -> str: | |
| out = f"**That didn't go through.**\n\n{data.get('errorMessage') or 'Unknown error'}" | |
| if data.get("hint"): | |
| out += f"\n\n{data['hint']}" | |
| return out | |
| def _stars(rating: Any, count: Any) -> str: | |
| value = _num(rating) | |
| if value is None: | |
| return '<div class="zp-rating zp-muted">No ratings yet</div>' | |
| count_html = f' <span class="zp-muted">({_fmt_int(count)})</span>' if count not in (None, "") else "" | |
| return f'<div class="zp-rating"><span class="zp-star">★</span> {value:.1f}{count_html}</div>' | |
| def _stock_html(in_stock: Any, qty: Any) -> str: | |
| if not in_stock: | |
| return '<span class="zp-stock zp-out">Out of stock</span>' | |
| units = _num(qty) | |
| if units is None: | |
| return '<span class="zp-stock zp-in">In stock</span>' | |
| return f'<span class="zp-stock zp-in">{int(units)} in stock</span>' | |
| BOLT_SVG = ( | |
| '<svg viewBox="0 0 24 24" width="{size}" height="{size}" aria-hidden="true">' | |
| '<path fill="#ffffff" d="M5 8h14l-1.6 11.2A2 2 0 0 1 15.4 21H8.6a2 2 0 0 1-2-1.8z"/>' | |
| '<path fill="none" stroke="#ffffff" stroke-width="1.8" stroke-linecap="round" d="M9 8l2.5-4.5M15 8l-2.5-4.5"/>' | |
| '<path fill="#ff3269" d="M12.8 10.5l-3.2 4.6h2.4l-.9 3.8 3.6-5h-2.5z"/></svg>' | |
| ) | |
| # ---------------------------------------------------------- category results | |
| def render_listing(data: Dict[str, Any], hide_out_of_stock: bool) -> str: | |
| rows = data.get("products") or [] | |
| shown = [r for r in rows if not (hide_out_of_stock and not r.get("in_stock"))] | |
| cards: List[str] = [] | |
| for item in shown: | |
| link = _safe_link(item.get("url")) | |
| name = _esc(item.get("name")) | |
| title = ( | |
| f'<a class="zp-name" href="{_esc(link)}" target="_blank" rel="noopener noreferrer">{name}</a>' | |
| if link else f'<span class="zp-name">{name}</span>' | |
| ) | |
| price, mrp = _num(item.get("price_inr")), _num(item.get("mrp_inr")) | |
| discount = item.get("discount_pct") or 0 | |
| price_html = ( | |
| f'<div class="zp-price"><b>{_esc(_inr(price))}</b>' | |
| + (f' <s>{_esc(_inr(mrp))}</s>' if mrp and price is not None and mrp > price else "") | |
| + "</div>" | |
| if price is not None else '<div class="zp-price zp-muted">No price</div>' | |
| ) | |
| cards.append( | |
| f'<div class="zp-card{" zp-card-out" if not item.get("in_stock") else ""}">' | |
| + (f'<span class="zp-off">{_esc(discount)}% OFF</span>' if discount else "") | |
| + f'<div class="zp-pack">{_esc(item.get("pack_size"))}</div>' | |
| f"{title}" | |
| f'<div class="zp-brand">{_esc(item.get("brand"))}</div>' | |
| f"{price_html}" | |
| f"{_stars(item.get('rating'), item.get('rating_count'))}" | |
| f'<div class="zp-line">{_stock_html(item.get("in_stock"), item.get("available_qty"))}</div>' | |
| f'<div class="zp-sub">{_esc(item.get("subcategory"))}</div>' | |
| "</div>" | |
| ) | |
| if not cards: | |
| cards.append('<div class="zp-empty">No products came back for this subcategory.</div>') | |
| store = next((r.get("store_id") for r in rows if r.get("store_id")), None) | |
| banner = ( | |
| f'<div class="zp-store">Prices and stock from Zepto dark store <b>{_esc(store)}</b> ' | |
| "(Zepto's default store, east Bengaluru)</div>" | |
| if store else "" | |
| ) | |
| return f'<div class="zp-wrap">{banner}<div class="zp-grid">{"".join(cards)}</div></div>' | |
| LISTING_HEADERS = ["Product", "Brand", "Pack", "Price ₹", "MRP ₹", "Off %", "In stock", "Units", "Rating", "Subcategory"] | |
| # ------------------------------------------------------------- product page | |
| def render_product(data: Dict[str, Any]) -> str: | |
| p = data.get("product") or {} | |
| images = [src for src in p.get("images") or [] if _safe_link(src)] | |
| main = ( | |
| f'<img class="zp-pdp-img" src="{_esc(images[0])}" alt="" loading="lazy" referrerpolicy="no-referrer" ' | |
| "onerror=\"this.style.visibility='hidden'\">" | |
| if images else "" | |
| ) | |
| thumbs = "".join( | |
| f'<img class="zp-pdp-thumb" src="{_esc(src)}" alt="" loading="lazy" referrerpolicy="no-referrer" ' | |
| "onerror=\"this.style.display='none'\">" | |
| for src in images[1:6] | |
| ) | |
| facts = [ | |
| ("Seller", p.get("seller")), | |
| ("Shelf life", p.get("shelf_life")), | |
| ("FSSAI licence", p.get("fssai_licence")), | |
| ("Country of origin", p.get("country_of_origin")), | |
| ("Pack size", p.get("pack_size")), | |
| ("MRP", _inr(p.get("mrp_inr"))), | |
| ("Max per order", p.get("max_per_order")), | |
| ("Category", p.get("category")), | |
| ] | |
| facts_html = "".join( | |
| f'<div class="zp-spec-name">{_esc(k)}</div><div class="zp-spec-value">{_esc(v)}</div>' | |
| for k, v in facts if v not in (None, "") | |
| ) | |
| link = _safe_link(p.get("url")) | |
| button = ( | |
| f'<a class="zp-btn" href="{_esc(link)}" target="_blank" rel="noopener noreferrer">View on Zepto</a>' | |
| if link else "" | |
| ) | |
| desc = _esc(p.get("description") or "") | |
| return ( | |
| '<div class="zp-wrap"><div class="zp-pdp">' | |
| f'<div class="zp-pdp-media">{main}<div class="zp-pdp-thumbs">{thumbs}</div></div>' | |
| '<div class="zp-pdp-info">' | |
| f'<div class="zp-brand">{_esc(p.get("brand"))}</div>' | |
| f'<div class="zp-pdp-name">{_esc(p.get("name"))}</div>' | |
| f"{_stars(p.get('rating'), p.get('rating_count'))}" | |
| f'<div class="zp-specs">{facts_html}</div>{button}' | |
| "</div></div>" | |
| + (f'<div class="zp-panel"><div class="zp-h">Description</div><div class="zp-text">{desc}</div></div>' if desc else "") | |
| + "</div>" | |
| ) | |
| # ---------------------------------------------------------------- handlers | |
| def _picker(choices: List[Tuple[str, str]]) -> Any: | |
| return gr.Dropdown(choices=choices, value=None, visible=bool(choices)) | |
| def run_category( | |
| token: str, url: str, walk: bool, max_subs: float, hide_oos: bool | |
| ) -> Tuple[str, str, List[List[Any]], Dict[str, Any], Optional[str], Any]: | |
| tool = _tool_or_none(token) | |
| if tool is None: | |
| return NO_TOKEN_MD, "", [], {}, None, _picker([]) | |
| if not url or not url.strip(): | |
| return "Paste a Zepto subcategory link.", "", [], {}, None, _picker([]) | |
| data = tool.category(url, int(max_subs or 1) if walk else 1) | |
| if data.get("status") != "success": | |
| return _error_md(data), "", [], data, None, _picker([]) | |
| rows = data.get("products") or [] | |
| oos = sum(1 for r in rows if not r.get("in_stock")) | |
| on_sale = sum(1 for r in rows if (r.get("discount_pct") or 0) > 0) | |
| deepest = max((r.get("discount_pct") or 0 for r in rows), default=0) | |
| parts = [f"**{len(rows)}** products", f"**{oos}** out of stock", f"**{on_sale}** below MRP"] | |
| if deepest: | |
| parts.append(f"deepest discount **{deepest}%**") | |
| summary = " · ".join(parts) | |
| pages = data.get("subcategories_read") or [] | |
| page_bits = [ | |
| f"{p.get('subcategory')}: {p.get('products')}" if "error" not in p else f"{p.get('url')}: failed" | |
| for p in pages | |
| ] | |
| summary += ( | |
| f"\n\nRead {len(pages)} of {data.get('subcategories_found')} subcategories ({', '.join(page_bits)}) · " | |
| f"{data.get('credits_used')} credits used · Zepto shows at most 32 products per subcategory." | |
| ) | |
| table = [ | |
| [ | |
| (r.get("name") or "")[:80], r.get("brand"), r.get("pack_size"), r.get("price_inr"), r.get("mrp_inr"), | |
| r.get("discount_pct"), "yes" if r.get("in_stock") else "no", r.get("available_qty"), | |
| r.get("rating"), r.get("subcategory"), | |
| ] | |
| for r in rows | |
| ] | |
| choices = [ | |
| (f"{str(r.get('name') or '')[:70]} — {_inr(r.get('price_inr')) or 'no price'}", r["url"]) | |
| for r in rows if r.get("url") | |
| ] | |
| csv_path = _write_csv(rows, "zepto-category") | |
| return summary, render_listing(data, bool(hide_oos)), table, data, csv_path, _picker(choices) | |
| def send_to_product(picked: Optional[str], current: str) -> Tuple[str, str]: | |
| if not picked: | |
| return current, "" | |
| return picked, "Sent. Open the **Product details** tab and press the button." | |
| def run_product(token: str, url: str) -> Tuple[str, str, Dict[str, Any], Optional[str]]: | |
| tool = _tool_or_none(token) | |
| if tool is None: | |
| return NO_TOKEN_MD, "", {}, None | |
| if not url or not url.strip(): | |
| return "Paste a Zepto product link.", "", {}, None | |
| data = tool.product(url) | |
| if data.get("status") != "success": | |
| return _error_md(data), "", data, None | |
| product = dict(data.get("product") or {}) | |
| product["images"] = " | ".join(product.get("images") or []) | |
| note = ( | |
| f"{data.get('credits_used')} credits used. Price and stock are left out here on purpose: the product " | |
| "page caps the stock count. Use the **Category listings** tab for those." | |
| ) | |
| return note, render_product(data), data, _write_csv([product], "zepto-product") | |
| # ----------------------------------------------------------------------- UI | |
| STYLE = """ | |
| <style> | |
| .gradio-container, .gradio-container button, .gradio-container input, | |
| .gradio-container textarea, .gradio-container select { | |
| font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif; | |
| } | |
| .zp-hero { position: relative; overflow: hidden; border-radius: 18px; padding: 30px 32px 28px 32px; margin-bottom: 16px; | |
| background: radial-gradient(circle at 88% 15%, rgba(255,50,105,.55) 0%, rgba(255,50,105,0) 42%), | |
| linear-gradient(135deg, #3b0a7a 0%, #5b14b8 55%, #7a1fd6 100%); } | |
| .zp-hero, .zp-hero * { color: #ffffff !important; } | |
| .zp-hero-inner { position: relative; z-index: 1; max-width: 640px; } | |
| .zp-hero h1 { font-size: 2.05rem !important; font-weight: 800 !important; margin: 0 0 10px 0 !important; | |
| display: flex; align-items: center; gap: 10px; letter-spacing: -0.01em; } | |
| .zp-hero p { margin: 0 0 8px 0 !important; font-size: 1rem !important; line-height: 1.55 !important; opacity: .96; } | |
| .zp-hero a { text-decoration: underline !important; } | |
| .zp-hero a.zp-cta { display: inline-block; margin-top: 12px; padding: 11px 24px; border-radius: 12px; | |
| background: linear-gradient(90deg, #ff3269, #ff6a3d) !important; color: #ffffff !important; font-weight: 800 !important; | |
| text-decoration: none !important; } | |
| .zp-hero a.zp-cta:hover { filter: brightness(1.06); } | |
| .zp-hero-card { position: absolute; right: 34px; top: 50%; transform: translateY(-50%); width: 250px; z-index: 1; | |
| background: #ffffff; border-radius: 14px; padding: 14px 16px; box-shadow: 0 10px 28px rgba(0,0,0,.25); } | |
| .zp-hero-card, .zp-hero-card * { color: #1f1f1f !important; } | |
| .zp-hc-title { font-size: .75rem; font-weight: 800; color: #6b6b6b !important; text-transform: uppercase; letter-spacing: .05em; margin-bottom: 8px; } | |
| .zp-hc-row { display: flex; justify-content: space-between; align-items: center; font-size: .84rem; margin: 7px 0; } | |
| .zp-hc-row b { color: #1f1f1f !important; } | |
| .zp-hc-row s { color: #9a9a9a !important; font-size: .75rem; } | |
| .zp-hc-tag { font-size: .7rem; font-weight: 800; border-radius: 6px; padding: 2px 6px; background: #e9f7ef; color: #0c831f !important; } | |
| .zp-hc-tag.out { background: #fdecef; color: #d6204e !important; } | |
| @media (max-width: 900px) { .zp-hero-card { display: none; } } | |
| .zp-note { border-left: 3px solid #ff3269; background: #fff3f6; padding: 12px 16px; border-radius: 0 10px 10px 0; | |
| font-size: .9rem; margin-bottom: 12px; color: #1f1f1f !important; } | |
| .gradio-container .primary, .gradio-container button.primary, .gradio-container button[variant="primary"] { | |
| background: #5b14b8 !important; background-image: none !important; border-color: #5b14b8 !important; | |
| color: #ffffff !important; border-radius: 12px !important; font-weight: 800 !important; } | |
| .gradio-container .primary:hover, .gradio-container button.primary:hover, | |
| .gradio-container button[variant="primary"]:hover { background: #3b0a7a !important; } | |
| .gradio-container .tab-nav button.selected { color: #5b14b8 !important; border-bottom-color: #5b14b8 !important; } | |
| .zp-wrap { color: #1f1f1f !important; } | |
| .zp-wrap img { margin: 0 !important; max-width: none; } | |
| .zp-muted { color: #8a8a8a !important; } | |
| .zp-store { background: #f6f0ff; border: 1px solid #e4d6fb; border-radius: 10px; padding: 9px 14px; margin-bottom: 12px; font-size: .9rem; color: #1f1f1f !important; } | |
| .zp-store b { color: #3b0a7a !important; } | |
| .zp-empty { background: #ffffff; border: 1px solid #ececec; border-radius: 12px; padding: 20px; color: #8a8a8a !important; } | |
| .zp-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(200px, 100%), 1fr)); gap: 12px; } | |
| .zp-card { position: relative; background: #ffffff; border: 1px solid #ececec; border-radius: 14px; padding: 14px 14px 12px 14px; } | |
| .zp-card:hover { box-shadow: 0 3px 12px rgba(91,20,184,.12); } | |
| .zp-card-out { opacity: .72; } | |
| .zp-off { position: absolute; top: 10px; right: 10px; background: #5b14b8; color: #ffffff !important; font-size: .68rem; | |
| font-weight: 800; border-radius: 6px; padding: 2px 6px; } | |
| .zp-pack { color: #8a8a8a !important; font-size: .75rem; margin-bottom: 4px; padding-right: 60px; } | |
| .zp-name { color: #1f1f1f !important; font-size: .9rem; font-weight: 600; line-height: 1.35; text-decoration: none !important; | |
| display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; } | |
| a.zp-name:hover { color: #5b14b8 !important; text-decoration: underline !important; } | |
| .zp-brand { color: #5b14b8 !important; font-size: .78rem; font-weight: 700; margin-top: 3px; } | |
| .zp-price { margin-top: 8px; font-size: 1.05rem; color: #1f1f1f !important; } | |
| .zp-price b { color: #1f1f1f !important; } | |
| .zp-price s { color: #9a9a9a !important; font-size: .8rem; margin-left: 4px; } | |
| .zp-rating { font-size: .8rem; margin-top: 4px; color: #1f1f1f !important; } | |
| .zp-star { color: #0c831f !important; } | |
| .zp-line { margin-top: 6px; } | |
| .zp-stock { font-size: .78rem; font-weight: 700; border-radius: 6px; padding: 2px 7px; } | |
| .zp-in { background: #e9f7ef; color: #0c831f !important; } | |
| .zp-out { background: #fdecef; color: #d6204e !important; } | |
| .zp-sub { color: #a0a0a0 !important; font-size: .72rem; margin-top: 8px; } | |
| .zp-pdp { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr); gap: 28px; background: #ffffff; | |
| border: 1px solid #ececec; border-radius: 16px; padding: 22px; } | |
| @media (max-width: 760px) { .zp-pdp { grid-template-columns: 1fr; } } | |
| .zp-pdp-media { display: flex; flex-direction: column; gap: 10px; } | |
| .zp-pdp-img { width: 100%; max-height: 360px; object-fit: contain; } | |
| .zp-pdp-thumbs { display: flex; gap: 8px; flex-wrap: wrap; } | |
| .zp-pdp-thumb { width: 60px; height: 60px; object-fit: contain; border: 1px solid #ececec; border-radius: 10px; background: #ffffff; } | |
| .zp-pdp-name { font-size: 1.35rem; font-weight: 700; line-height: 1.3; color: #1f1f1f !important; margin: 4px 0; } | |
| .zp-specs { display: grid; grid-template-columns: minmax(120px, .6fr) 1fr; border-top: 1px solid #ececec; margin-top: 14px; } | |
| .zp-spec-name, .zp-spec-value { padding: 9px 10px; border-bottom: 1px solid #ececec; font-size: .88rem; } | |
| .zp-spec-name { color: #8a8a8a !important; } | |
| .zp-spec-value { color: #1f1f1f !important; } | |
| .zp-btn { display: inline-block; margin-top: 16px; padding: 10px 22px; border-radius: 12px; background: #5b14b8 !important; | |
| color: #ffffff !important; font-weight: 800; text-decoration: none !important; font-size: .9rem; } | |
| .zp-panel { background: #ffffff; border: 1px solid #ececec; border-radius: 16px; padding: 4px 22px 18px 22px; margin-top: 14px; } | |
| .zp-h { font-size: 1.05rem; font-weight: 800; color: #1f1f1f !important; margin: 16px 0 8px 0; } | |
| .zp-text { color: #1f1f1f !important; font-size: .9rem; line-height: 1.6; } | |
| footer { display: none !important; } | |
| </style> | |
| """ | |
| HERO = f""" | |
| <div class="zp-hero"> | |
| <div class="zp-hero-inner"> | |
| <h1>{BOLT_SVG.format(size=36)} Zepto Scraper</h1> | |
| <p><strong>Prices, MRP and live stock for every product in a Zepto category.</strong> Paste a subcategory link | |
| and get each card's selling price, printed MRP, discount and the units the dark store holds, out-of-stock items | |
| included. Open any product for its seller, shelf life and FSSAI licence.</p> | |
| <p>Zepto answers scripts with an empty AWS WAF challenge. This Space fetches the pages through | |
| <a href="https://scrape.do/?utm_source=huggingface&utm_medium=zepto-scraper" target="_blank" rel="noopener noreferrer">Scrape.do</a> | |
| through residential proxies in India (10 credits per page) and reads the product data Zepto streams into the HTML.</p> | |
| <a class="zp-cta" href="{SCRAPE_DO_SIGNUP_URL}" target="_blank" rel="noopener noreferrer">Get a free API token →</a> | |
| </div> | |
| <div class="zp-hero-card" aria-hidden="true"> | |
| <div class="zp-hc-title">One subcategory · one request</div> | |
| <div class="zp-hc-row"><span>Chips 41 g</span><span><b>₹19</b> <s>₹20</s></span><span class="zp-hc-tag">30 left</span></div> | |
| <div class="zp-hc-row"><span>Nachos 60 g</span><span><b>₹45</b> <s>₹50</s></span><span class="zp-hc-tag">12 left</span></div> | |
| <div class="zp-hc-row"><span>Namkeen 200 g</span><span><b>₹52</b></span><span class="zp-hc-tag out">Out</span></div> | |
| </div> | |
| </div> | |
| """ | |
| CREDIT_NOTE = """ | |
| <div class="zp-note"> | |
| Every Zepto page costs 10 credits: one subcategory (up to 32 products) or one product page, fetched through | |
| Scrape.do's residential proxies in India. Walking a whole category of 9 subcategories costs 90 credits. The free | |
| 1,000 credits a month cover about 100 pages. | |
| </div> | |
| """ | |
| FOOTER_MD = f""" | |
| --- | |
| ### Use it as a smolagents Tool | |
| ```python | |
| from smolagents import CodeAgent, InferenceClientModel | |
| from tool import ScrapeDoZeptoTool | |
| agent = CodeAgent( | |
| tools=[ScrapeDoZeptoTool(api_token="YOUR_SCRAPEDO_TOKEN")], | |
| model=InferenceClientModel(), | |
| ) | |
| agent.run("List every out-of-stock product in Zepto's Chips & Crisps subcategory: {DEFAULT_SUBCATEGORY_URL}") | |
| ``` | |
| Or set `SCRAPE_DO_API_TOKEN` in the environment and instantiate with no arguments. | |
| Prefer plain Python? The same scraper is open source: `pip install zepto-scraper` | |
| ([GitHub](https://github.com/bugrahansss/zepto-scraper)). | |
| [Scrape.do](https://scrape.do/?utm_source=huggingface&utm_medium=zepto-scraper) · [Documentation](https://scrape.do/documentation/) · [Sign up free]({SCRAPE_DO_SIGNUP_URL}) | |
| """ | |
| def build_demo() -> gr.Blocks: | |
| with gr.Blocks(title="Zepto Scraper — Scrape.do") as demo: | |
| gr.HTML(STYLE) | |
| gr.HTML(HERO) | |
| api_token = gr.Textbox( | |
| label="Scrape.do API token", | |
| placeholder="Paste your token here", | |
| type="password", | |
| info="Free tier: 1,000 credits a month. 10 credits per Zepto page.", | |
| ) | |
| gr.HTML(CREDIT_NOTE) | |
| with gr.Tabs(): | |
| with gr.Tab("Category listings"): | |
| cat_url = gr.Textbox( | |
| label="Zepto subcategory link", | |
| value=DEFAULT_SUBCATEGORY_URL, | |
| info="Open a category on zepto.com and click a subcategory in the left sidebar. " | |
| "The link contains /cid/ and /scid/.", | |
| ) | |
| with gr.Row(): | |
| walk = gr.Checkbox(label="Also read the sibling subcategories", value=False) | |
| max_subs = gr.Slider( | |
| minimum=1, maximum=MAX_SUBCATEGORIES, step=1, value=9, | |
| label="Max subcategories (10 credits each)", | |
| ) | |
| hide_oos = gr.Checkbox(label="Hide out-of-stock cards", value=False) | |
| cat_btn = gr.Button("Scrape category", variant="primary") | |
| cat_summary = gr.Markdown() | |
| cat_view = gr.HTML() | |
| cat_table = gr.Dataframe(headers=LISTING_HEADERS, wrap=True, label="Products") | |
| cat_csv = gr.File(label="Download products as CSV") | |
| pick = gr.Dropdown( | |
| label="Open one of these products in the Product details tab", | |
| choices=[], value=None, visible=False, | |
| ) | |
| pick_note = gr.Markdown() | |
| with gr.Accordion("Raw JSON response", open=False): | |
| cat_json = gr.JSON() | |
| with gr.Tab("Product details"): | |
| prod_url = gr.Textbox(label="Zepto product link", value=DEFAULT_PRODUCT_URL) | |
| prod_btn = gr.Button("Get product details", variant="primary") | |
| prod_summary = gr.Markdown() | |
| prod_view = gr.HTML() | |
| prod_csv = gr.File(label="Download as CSV") | |
| with gr.Accordion("Raw JSON response", open=False): | |
| prod_json = gr.JSON() | |
| cat_inputs = [api_token, cat_url, walk, max_subs, hide_oos] | |
| cat_outputs = [cat_summary, cat_view, cat_table, cat_json, cat_csv, pick] | |
| cat_btn.click(fn=run_category, inputs=cat_inputs, outputs=cat_outputs) | |
| cat_url.submit(fn=run_category, inputs=cat_inputs, outputs=cat_outputs) | |
| pick.change(fn=send_to_product, inputs=[pick, prod_url], outputs=[prod_url, pick_note]) | |
| prod_outputs = [prod_summary, prod_view, prod_json, prod_csv] | |
| prod_btn.click(fn=run_product, inputs=[api_token, prod_url], outputs=prod_outputs) | |
| prod_url.submit(fn=run_product, inputs=[api_token, prod_url], outputs=prod_outputs) | |
| gr.Markdown(FOOTER_MD) | |
| return demo | |
| # Hugging Face Spaces launches the module-level `demo` object rather than | |
| # calling launch() itself, so build it at import time. | |
| demo = build_demo() | |
| def _network_probe() -> None: | |
| """Log once at startup whether api.scrape.do is reachable from this Space (a bad token costs nothing).""" | |
| import time as _t | |
| import requests as _r | |
| started = _t.time() | |
| try: | |
| resp = _r.get("https://api.scrape.do/", params={"token": "probe", "url": "https://www.zepto.com/"}, timeout=(10, 30)) | |
| print(f"[probe] api.scrape.do HTTP {resp.status_code} in {_t.time() - started:.1f}s", flush=True) | |
| except Exception as exc: # noqa: BLE001 | |
| print(f"[probe] api.scrape.do unreachable after {_t.time() - started:.1f}s: {exc}", flush=True) | |
| if __name__ == "__main__": | |
| import threading | |
| threading.Thread(target=_network_probe, daemon=True).start() | |
| demo.queue(default_concurrency_limit=4).launch(ssr_mode=False) | |