Spaces:
Sleeping
Sleeping
File size: 26,207 Bytes
2b5d098 | 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 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | 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 (
CITIES,
DEFAULT_CATEGORY_URL,
DEFAULT_PRODUCT_URL,
MAX_PAGES,
SCRAPE_DO_SIGNUP_URL,
ScrapeDoBlinkitTool,
)
# 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
@spaces.GPU(duration=1)
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 Blinkit request costs 10 credits."
)
CUSTOM = "Custom coordinates"
# --------------------------------------------------------------------- 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[ScrapeDoBlinkitTool]:
if not token or not token.strip():
return None
return ScrapeDoBlinkitTool(api_token=token.strip())
def _location(city: str, coords: str) -> str:
return (coords or "").strip() if city == CUSTOM else city
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 not value:
return '<div class="bk-rating bk-muted">No ratings yet</div>'
count_html = f' <span class="bk-muted">({_fmt_int(count)})</span>' if count not in (None, "") else ""
return f'<div class="bk-rating"><span class="bk-star">★</span> {value:.1f}{count_html}</div>'
def _stock(in_stock: Any, units: Any) -> str:
if not in_stock:
return '<span class="bk-stock bk-out">Out of stock</span>'
n = _num(units)
label = "50+ in stock" if n is not None and n >= 50 else (f"{int(n)} in stock" if n is not None else "In stock")
return f'<span class="bk-stock bk-in">{label}</span>'
BOLT_SVG = (
'<svg viewBox="0 0 24 24" width="{size}" height="{size}" aria-hidden="true">'
'<path fill="#0C831F" d="M13.5 2 5 13.5h6L9.8 22 19 9.5h-6.2z"/></svg>'
)
# ---------------------------------------------------------- category results
def render_category(data: Dict[str, Any]) -> str:
cards: List[str] = []
for item in data.get("products") or []:
link = _safe_link(item.get("url"))
image = _safe_link(item.get("image"))
img_html = (
f'<img class="bk-img" src="{_esc(image)}" alt="" loading="lazy" referrerpolicy="no-referrer" '
"onerror=\"this.style.visibility='hidden'\">"
if image else '<div class="bk-img"></div>'
)
name = _esc(item.get("name"))
title = (
f'<a class="bk-name" href="{_esc(link)}" target="_blank" rel="noopener noreferrer">{name}</a>'
if link else f'<span class="bk-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="bk-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="bk-price bk-muted">No price</div>'
)
cards.append(
f'<div class="bk-card{" bk-card-out" if not item.get("in_stock") else ""}">'
+ (f'<span class="bk-off">{_esc(discount)}% OFF</span>' if discount else "")
+ f"{img_html}"
f'<div class="bk-pack">{_esc(item.get("pack_size"))}</div>'
f"{title}"
f'<div class="bk-brand">{_esc(item.get("brand"))}</div>'
f"{price_html}"
f"{_stars(item.get('rating'), item.get('rating_count'))}"
f'<div class="bk-line">{_stock(item.get("in_stock"), item.get("inventory"))}</div>'
"</div>"
)
if not cards:
cards.append('<div class="bk-empty">No products came back for this category.</div>')
banner = (
f'<div class="bk-store">Prices and stock from Blinkit store <b>{_esc(data.get("store_id"))}</b>, '
f'the dark store serving <b>{_esc(data.get("location"))}</b> ({_esc(data.get("latitude"))}, {_esc(data.get("longitude"))})</div>'
)
return f'<div class="bk-wrap">{banner}<div class="bk-grid">{"".join(cards)}</div></div>'
TABLE_HEADERS = ["Product", "Brand", "Pack", "Price ₹", "MRP ₹", "Off %", "In stock", "Units", "Rating", "Ratings"]
# ------------------------------------------------------------- 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="bk-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="bk-pdp-thumb" src="{_esc(src)}" alt="" loading="lazy" referrerpolicy="no-referrer" '
"onerror=\"this.style.display='none'\">"
for src in images[1:6]
)
price, mrp = _num(p.get("price_inr")), _num(p.get("mrp_inr"))
price_html = (
f'<div class="bk-pdp-price"><b>{_esc(_inr(price))}</b>'
+ (f' <s>MRP {_esc(_inr(mrp))}</s>' if mrp and price is not None and mrp > price else "")
+ (f' <span class="bk-off-inline">{_esc(p.get("discount_pct"))}% OFF</span>' if p.get("discount_pct") else "")
+ "</div>"
if price is not None else '<div class="bk-pdp-price bk-muted">Not sold at this location right now</div>'
)
facts = [
("Stock", "In stock" if p.get("in_stock") else "Out of stock"),
("Units", ("50+" if (_num(p.get("inventory")) or 0) >= 50 else _fmt_int(p.get("inventory"))) if p.get("in_stock") else None),
("Pack size", p.get("pack_size")),
("FSSAI licence", p.get("fssai_license")),
("Shelf life", p.get("shelf_life")),
("Country of origin", p.get("country_of_origin")),
("Store", p.get("store_id")),
]
facts_html = "".join(
f'<div class="bk-spec-name">{_esc(k)}</div><div class="bk-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="bk-btn" href="{_esc(link)}" target="_blank" rel="noopener noreferrer">View on Blinkit</a>'
if link else ""
)
panels = ""
for title, key in (("Ingredients", "ingredients"), ("Manufacturer", "manufacturer"), ("Return policy", "return_policy")):
if p.get(key):
panels += f'<div class="bk-panel"><div class="bk-h">{title}</div><div class="bk-text">{_esc(p[key])}</div></div>'
return (
'<div class="bk-wrap"><div class="bk-pdp">'
f'<div class="bk-pdp-media">{main}<div class="bk-pdp-thumbs">{thumbs}</div></div>'
'<div class="bk-pdp-info">'
f'<div class="bk-brand">{_esc(p.get("brand"))}</div>'
f'<div class="bk-pdp-name">{_esc(p.get("name"))}</div>'
f"{price_html}"
f'<div class="bk-specs">{facts_html}</div>{button}'
"</div></div>"
f"{panels}</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, city: str, coords: str, pages: float
) -> 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 Blinkit category link.", "", [], {}, None, _picker([])
data = tool.category(url, _location(city, coords), int(pages or 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"))
low = sum(1 for r in rows if r.get("in_stock") and (r.get("inventory") or 0) < 5)
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"**{low}** with fewer than 5 units",
f"**{on_sale}** below MRP"]
if deepest:
parts.append(f"deepest discount **{deepest}%**")
summary = " · ".join(parts)
total = data.get("total_count")
summary += (
f"\n\nRead {data.get('pages_read')} page(s)"
+ (f" of a category with {_fmt_int(total)} products at this location" if total else "")
+ f" · {data.get('credits_used')} credits used"
+ (" · more pages available" if data.get("has_more") else "")
)
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("inventory"), r.get("rating"),
r.get("rating_count"),
]
for r in rows
]
choices = [
(f"{str(r.get('name') or '')[:60]} {r.get('pack_size') or ''} — {_inr(r.get('price_inr')) or 'no price'}", r["url"])
for r in rows if r.get("url")
]
return summary, render_category(data), table, data, _write_csv(rows, "blinkit-category"), _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, city: str, coords: 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 Blinkit product link.", "", {}, None
data = tool.product(url, _location(city, coords))
if data.get("status") != "success":
return _error_md(data), "", data, None
product = dict(data.get("product") or {})
product["images"] = " | ".join(product.get("images") or [])
product.pop("attributes", None)
attrs = (data.get("product") or {}).get("attributes") or {}
note = f"{data.get('credits_used')} credits used · {len(attrs)} attributes in the raw JSON below."
return note, render_product(data), data, _write_csv([product], "blinkit-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;
}
.bk-hero { position: relative; overflow: hidden; border-radius: 18px; padding: 30px 32px 28px 32px; margin-bottom: 16px;
background: radial-gradient(circle at 92% 10%, rgba(12,131,31,.85) 0%, rgba(12,131,31,0) 42%),
linear-gradient(135deg, #0a5e17 0%, #0c831f 55%, #2f9e2a 100%); }
.bk-hero, .bk-hero * { color: #ffffff !important; }
.bk-hero-inner { position: relative; z-index: 1; max-width: 640px; }
.bk-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; }
.bk-hero h1 .bk-badge { display: inline-flex; width: 44px; height: 44px; border-radius: 12px; background: #F8CB46;
align-items: center; justify-content: center; }
.bk-hero p { margin: 0 0 8px 0 !important; font-size: 1rem !important; line-height: 1.55 !important; opacity: .97; }
.bk-hero a { text-decoration: underline !important; }
.bk-hero a.bk-cta { display: inline-block; margin-top: 12px; padding: 11px 24px; border-radius: 10px;
background: #F8CB46 !important; color: #1f1f1f !important; font-weight: 800 !important; text-decoration: none !important; }
.bk-hero a.bk-cta:hover { filter: brightness(1.04); }
.bk-hero-card { position: absolute; right: 34px; top: 50%; transform: translateY(-50%); width: 260px; z-index: 1;
background: #ffffff; border-radius: 14px; padding: 14px 16px; box-shadow: 0 10px 28px rgba(0,0,0,.22); }
.bk-hero-card, .bk-hero-card * { color: #1f1f1f !important; }
.bk-hc-title { font-size: .72rem; font-weight: 800; color: #6b6b6b !important; text-transform: uppercase; letter-spacing: .05em; margin-bottom: 8px; }
.bk-hc-row { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; font-size: .82rem; margin: 7px 0; }
.bk-hc-tag { font-size: .68rem; font-weight: 800; border-radius: 6px; padding: 2px 6px; background: #e9f7ef; color: #0c831f !important; }
.bk-hc-tag.low { background: #fff6dc; color: #9a6b00 !important; }
@media (max-width: 900px) { .bk-hero-card { display: none; } }
.bk-note { border-left: 3px solid #0c831f; background: #f1faf3; 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: #0c831f !important; background-image: none !important; border-color: #0c831f !important;
color: #ffffff !important; border-radius: 10px !important; font-weight: 800 !important; }
.gradio-container .primary:hover, .gradio-container button.primary:hover,
.gradio-container button[variant="primary"]:hover { background: #0a6b19 !important; }
.gradio-container .tab-nav button.selected { color: #0c831f !important; border-bottom-color: #0c831f !important; }
.bk-wrap { color: #1f1f1f !important; }
.bk-wrap img { margin: 0 !important; max-width: none; }
.bk-muted { color: #8a8a8a !important; }
.bk-store { background: #fffbea; border: 1px solid #f8e3a0; border-radius: 10px; padding: 9px 14px; margin-bottom: 12px; font-size: .9rem; color: #1f1f1f !important; }
.bk-store b { color: #0a5e17 !important; }
.bk-empty { background: #ffffff; border: 1px solid #ececec; border-radius: 12px; padding: 20px; color: #8a8a8a !important; }
.bk-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(180px, 100%), 1fr)); gap: 12px; }
.bk-card { position: relative; background: #ffffff; border: 1px solid #e8e8e8; border-radius: 12px; padding: 10px 12px 12px 12px; }
.bk-card:hover { box-shadow: 0 3px 12px rgba(12,131,31,.12); }
.bk-card-out { opacity: .7; }
.bk-img { display: block; width: 100%; height: 150px; object-fit: contain; background: #ffffff; }
.bk-off { position: absolute; top: 8px; left: 8px; background: #256fef; color: #ffffff !important; font-size: .66rem;
font-weight: 800; border-radius: 6px; padding: 2px 6px; z-index: 1; }
.bk-pack { color: #8a8a8a !important; font-size: .74rem; margin-top: 6px; }
.bk-name { color: #1f1f1f !important; font-size: .86rem; font-weight: 600; line-height: 1.3; text-decoration: none !important;
display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; margin-top: 2px; }
a.bk-name:hover { color: #0c831f !important; text-decoration: underline !important; }
.bk-brand { color: #0c831f !important; font-size: .76rem; font-weight: 700; margin-top: 3px; }
.bk-price { margin-top: 6px; font-size: 1rem; color: #1f1f1f !important; }
.bk-price b { color: #1f1f1f !important; }
.bk-price s, .bk-pdp-price s { color: #9a9a9a !important; font-size: .78rem; margin-left: 4px; }
.bk-rating { font-size: .76rem; margin-top: 4px; color: #1f1f1f !important; }
.bk-star { color: #e0a800 !important; }
.bk-line { margin-top: 6px; }
.bk-stock { font-size: .74rem; font-weight: 700; border-radius: 6px; padding: 2px 7px; }
.bk-in { background: #e9f7ef; color: #0c831f !important; }
.bk-out { background: #fdecef; color: #d6204e !important; }
.bk-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) { .bk-pdp { grid-template-columns: 1fr; } }
.bk-pdp-media { display: flex; flex-direction: column; gap: 10px; }
.bk-pdp-img { width: 100%; max-height: 360px; object-fit: contain; }
.bk-pdp-thumbs { display: flex; gap: 8px; flex-wrap: wrap; }
.bk-pdp-thumb { width: 60px; height: 60px; object-fit: contain; border: 1px solid #ececec; border-radius: 10px; background: #ffffff; }
.bk-pdp-name { font-size: 1.3rem; font-weight: 700; line-height: 1.3; color: #1f1f1f !important; margin: 4px 0; }
.bk-pdp-price { font-size: 1.25rem; margin-top: 8px; color: #1f1f1f !important; }
.bk-off-inline { background: #256fef; color: #ffffff !important; font-size: .72rem; font-weight: 800; border-radius: 6px; padding: 2px 6px; margin-left: 6px; }
.bk-specs { display: grid; grid-template-columns: minmax(120px, .6fr) 1fr; border-top: 1px solid #ececec; margin-top: 14px; }
.bk-spec-name, .bk-spec-value { padding: 8px 10px; border-bottom: 1px solid #ececec; font-size: .86rem; }
.bk-spec-name { color: #8a8a8a !important; }
.bk-spec-value { color: #1f1f1f !important; }
.bk-btn { display: inline-block; margin-top: 16px; padding: 10px 22px; border-radius: 10px; background: #0c831f !important;
color: #ffffff !important; font-weight: 800; text-decoration: none !important; font-size: .9rem; }
.bk-panel { background: #ffffff; border: 1px solid #ececec; border-radius: 16px; padding: 4px 22px 16px 22px; margin-top: 14px; }
.bk-h { font-size: 1rem; font-weight: 800; color: #1f1f1f !important; margin: 14px 0 6px 0; }
.bk-text { color: #1f1f1f !important; font-size: .88rem; line-height: 1.6; }
footer { display: none !important; }
</style>
"""
HERO = f"""
<div class="bk-hero">
<div class="bk-hero-inner">
<h1><span class="bk-badge">{BOLT_SVG.format(size=28)}</span> Blinkit Scraper</h1>
<p><strong>Prices, MRP and units in stock for every product in a Blinkit category, for the dark store that
serves your address.</strong> Pick a city or enter coordinates: assortment, stock and even the number of
products change from store to store. Open any product for its FSSAI licence, shelf life and ingredients.</p>
<p>Blinkit answers scripts with a Cloudflare 403. This Space fetches the pages through
<a href="https://scrape.do/?utm_source=huggingface&utm_medium=blinkit-scraper" target="_blank" rel="noopener noreferrer">Scrape.do</a>
with residential proxies in India and follows Blinkit's own listing API page by page.</p>
<a class="bk-cta" href="{SCRAPE_DO_SIGNUP_URL}" target="_blank" rel="noopener noreferrer">Get a free API token →</a>
</div>
<div class="bk-hero-card" aria-hidden="true">
<div class="bk-hc-title">Same chips pack · two stores</div>
<div class="bk-hc-row"><span>Gurugram</span><span><b>₹43</b></span><span class="bk-hc-tag">5 left</span></div>
<div class="bk-hc-row"><span>Mumbai</span><span><b>₹43</b></span><span class="bk-hc-tag low">1 left</span></div>
<div class="bk-hc-row"><span>Category size</span><span>673 vs 524</span><span></span></div>
</div>
</div>
"""
CREDIT_NOTE = """
<div class="bk-note">
Every request is 10 Scrape.do credits (residential proxies in India). Page 1 of a category holds 30 products;
every further page is one call to Blinkit's listing API and holds 15. Three pages (60 products) cost 30 credits,
a product page 10. The free 1,000 credits a month cover about 100 requests.
</div>
"""
FOOTER_MD = f"""
---
### Use it as a smolagents Tool
```python
from smolagents import CodeAgent, InferenceClientModel
from tool import ScrapeDoBlinkitTool
agent = CodeAgent(
tools=[ScrapeDoBlinkitTool(api_token="YOUR_SCRAPEDO_TOKEN")],
model=InferenceClientModel(),
)
agent.run("Which chips are low on stock in Mumbai? {DEFAULT_CATEGORY_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 blinkit-scraper`
([GitHub](https://github.com/bugrahansss/blinkit-scraper)) · No-code: [Apify actor](https://apify.com/scrapedo/blinkit-scraper)
[Scrape.do](https://scrape.do/?utm_source=huggingface&utm_medium=blinkit-scraper) · [Documentation](https://scrape.do/documentation/) · [Sign up free]({SCRAPE_DO_SIGNUP_URL})
"""
def build_demo() -> gr.Blocks:
with gr.Blocks(title="Blinkit 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 Blinkit request.",
)
with gr.Row():
city = gr.Dropdown(choices=list(CITIES) + [CUSTOM], value="Gurugram", label="Delivery location",
info="Gurugram is Blinkit's own default.")
coords = gr.Textbox(label="Custom coordinates (lat, lon)", placeholder="12.9352, 77.6245",
info="Used when the location is set to Custom coordinates.")
gr.HTML(CREDIT_NOTE)
with gr.Tabs():
with gr.Tab("Category listings"):
cat_url = gr.Textbox(
label="Blinkit category link",
value=DEFAULT_CATEGORY_URL,
info="Open a category on blinkit.com and copy the link; it contains /cn/ and /cid/.",
)
pages = gr.Slider(minimum=1, maximum=MAX_PAGES, step=1, value=2,
label="Pages (30 products, then 15 per page; 10 credits each)")
cat_btn = gr.Button("Scrape category", variant="primary")
cat_summary = gr.Markdown()
cat_view = gr.HTML()
cat_table = gr.Dataframe(headers=TABLE_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="Blinkit product link", value=DEFAULT_PRODUCT_URL,
info="A link containing /prn/ and /prid/")
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, city, coords, pages]
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_inputs = [api_token, prod_url, city, coords]
prod_outputs = [prod_summary, prod_view, prod_json, prod_csv]
prod_btn.click(fn=run_product, inputs=prod_inputs, outputs=prod_outputs)
prod_url.submit(fn=run_product, inputs=prod_inputs, 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://blinkit.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)
|