extsearch 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
extsearch/__init__.py ADDED
@@ -0,0 +1,38 @@
1
+ """extsearch — product search across marketplaces of many countries.
2
+
3
+ Quick start:
4
+
5
+ import asyncio
6
+ from extsearch import search, select, set_proxies
7
+
8
+ set_proxies("socks5://1.2.3.4:1080") # optional
9
+ res = asyncio.run(search("fifine k669", where="ua", limit=5))
10
+ card = asyncio.run(select(res["items"][0]["id"]))
11
+
12
+ Entry points (see pyproject): ``extsearch`` (CLI), ``extsearch-mcp``
13
+ (MCP server), ``extsearch-api`` (HTTP API).
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ from .config import __version__
19
+ from .errors import ExtSearchError, OlxError
20
+ from .marketplaces import REGISTRY, catalog, by_country, resolve
21
+ from .media import FREELANCE_BOARDS, freelance_search, mod_search, video_search
22
+ from .proxies import POOL, get_pool, parse_proxies, set_proxies
23
+ from .search import search, select, sources_catalog
24
+ from .web import web_get, web_search
25
+
26
+ __all__ = [
27
+ "__version__",
28
+ # search
29
+ "search", "select", "sources_catalog",
30
+ "REGISTRY", "catalog", "by_country", "resolve",
31
+ # content
32
+ "web_search", "web_get", "video_search", "mod_search", "freelance_search",
33
+ "FREELANCE_BOARDS",
34
+ # proxies
35
+ "set_proxies", "parse_proxies", "get_pool", "POOL",
36
+ # errors
37
+ "ExtSearchError", "OlxError",
38
+ ]
extsearch/api.py ADDED
@@ -0,0 +1,202 @@
1
+ """HTTP API on FastAPI: search, product cards, catalog, content tools.
2
+
3
+ Run:
4
+
5
+ extsearch-api # 127.0.0.1:8000
6
+ extsearch-api --host 0.0.0.0 --port 8000
7
+
8
+ Interactive docs: /docs (OpenAPI). Every error keeps the same shape:
9
+
10
+ {"error": "blocked", "message": "...", "hint": "...", "retryable": true}
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ from typing import Any, Literal
16
+
17
+ from fastapi import FastAPI, HTTPException, Query
18
+ from pydantic import BaseModel, Field
19
+
20
+ from .config import __version__
21
+ from .errors import ExtSearchError
22
+ from .media import freelance_search, mod_search, video_search
23
+ from .proxies import get_pool, set_proxies
24
+ from .search import search, select, sources_catalog
25
+ from .web import web_get, web_search
26
+
27
+ app = FastAPI(
28
+ title="extsearch",
29
+ version=__version__,
30
+ summary="Product search across marketplaces of many countries",
31
+ )
32
+
33
+ STATUS_BY_CODE = {
34
+ "bad_argument": 400,
35
+ "not_found": 404,
36
+ "no_url": 409,
37
+ "unavailable": 501,
38
+ "browser_unavailable": 501,
39
+ "blocked": 502,
40
+ "needs_proxy": 502,
41
+ "rate_limited": 429,
42
+ "timeout": 504,
43
+ "network": 502,
44
+ "server_error": 502,
45
+ "http_error": 502,
46
+ "bad_response": 502,
47
+ "no_results": 404,
48
+ }
49
+
50
+
51
+ def _fail(exc: ExtSearchError) -> HTTPException:
52
+ return HTTPException(status_code=STATUS_BY_CODE.get(exc.code, 500),
53
+ detail=exc.as_dict())
54
+
55
+
56
+ class SearchBody(BaseModel):
57
+ query: str = Field(..., description="search phrase")
58
+ where: str = Field("all", description='countries/marketplaces: "ua", "ua,ru", "olx,ozon", "all,-ru"')
59
+ limit: int = Field(10, ge=1, le=50)
60
+ page: int = Field(1, ge=1)
61
+ price_min: float | None = None
62
+ price_max: float | None = None
63
+ min_rating: float | None = None
64
+ sort: Literal["relevance", "newest", "cheapest", "expensive", "rating",
65
+ "rating_asc", "reviews", "reviews_asc", "discount"] = "relevance"
66
+ strict: bool = False
67
+ junk_filter: bool | None = None
68
+ include_urls: bool = True
69
+ details: int = Field(0, ge=0, le=20)
70
+ per_source_limit: int | None = Field(None, ge=1, le=50)
71
+ free_only: bool = False
72
+ proxies: list[str] | None = Field(None, description='["socks5://IP:PORT"]')
73
+
74
+
75
+ class ProxyBody(BaseModel):
76
+ proxies: list[str] | str | None = None
77
+
78
+
79
+ @app.get("/health")
80
+ def health() -> dict[str, Any]:
81
+ return {"status": "ok", "version": __version__, "proxies": len(get_pool())}
82
+
83
+
84
+ @app.get("/sources")
85
+ def sources() -> dict[str, Any]:
86
+ return sources_catalog()
87
+
88
+
89
+ @app.get("/search")
90
+ async def search_get(
91
+ q: str = Query(..., min_length=1),
92
+ where: str = "all",
93
+ limit: int = Query(10, ge=1, le=50),
94
+ page: int = Query(1, ge=1),
95
+ sort: str = "relevance",
96
+ price_min: float | None = None,
97
+ price_max: float | None = None,
98
+ min_rating: float | None = None,
99
+ strict: bool = False,
100
+ ) -> dict[str, Any]:
101
+ """Convenience GET for quick checks and curl."""
102
+ try:
103
+ return await search(q, where=where, limit=limit, page=page, sort=sort,
104
+ price_min=price_min, price_max=price_max,
105
+ min_rating=min_rating, strict=strict)
106
+ except ExtSearchError as exc:
107
+ raise _fail(exc) from None
108
+
109
+
110
+ @app.post("/search")
111
+ async def search_post(body: SearchBody) -> dict[str, Any]:
112
+ try:
113
+ return await search(
114
+ body.query, where=body.where, limit=body.limit, page=body.page,
115
+ price_min=body.price_min, price_max=body.price_max,
116
+ min_rating=body.min_rating, sort=body.sort, strict=body.strict,
117
+ junk_filter=body.junk_filter, include_urls=body.include_urls,
118
+ details=body.details, per_source_limit=body.per_source_limit,
119
+ free_only=body.free_only, proxies=body.proxies,
120
+ )
121
+ except ExtSearchError as exc:
122
+ raise _fail(exc) from None
123
+
124
+
125
+ @app.get("/item/{item_id:path}")
126
+ async def item(item_id: str, max_description_chars: int = Query(600, ge=0, le=20000)) -> dict[str, Any]:
127
+ try:
128
+ return await select(item_id, max_description_chars)
129
+ except ExtSearchError as exc:
130
+ raise _fail(exc) from None
131
+
132
+
133
+ @app.post("/proxies")
134
+ def proxies_set(body: ProxyBody) -> dict[str, Any]:
135
+ try:
136
+ pool = set_proxies(body.proxies)
137
+ except ValueError as exc:
138
+ raise HTTPException(status_code=400,
139
+ detail=ExtSearchError("bad_argument", str(exc)).as_dict()) from None
140
+ return {"count": len(pool), "proxies": pool.stats()}
141
+
142
+
143
+ @app.get("/proxies")
144
+ def proxies_get() -> dict[str, Any]:
145
+ pool = get_pool()
146
+ return {"count": len(pool), "proxies": pool.stats()}
147
+
148
+
149
+ @app.get("/web/search")
150
+ async def web_search_ep(q: str, limit: int = Query(8, ge=1, le=30),
151
+ fetch_top: int = Query(0, ge=0, le=10)) -> dict[str, Any]:
152
+ try:
153
+ return await web_search(q, limit=limit, fetch_top=fetch_top)
154
+ except ExtSearchError as exc:
155
+ raise _fail(exc) from None
156
+
157
+
158
+ @app.get("/web/get")
159
+ async def web_get_ep(url: str, max_chars: int = Query(4000, ge=1, le=20000),
160
+ offset: int = Query(0, ge=0)) -> dict[str, Any]:
161
+ try:
162
+ return await web_get(url, max_chars=max_chars, offset=offset)
163
+ except ExtSearchError as exc:
164
+ raise _fail(exc) from None
165
+
166
+
167
+ @app.get("/video")
168
+ async def video_ep(q: str, limit: int = Query(8, ge=1, le=30)) -> dict[str, Any]:
169
+ try:
170
+ return await video_search(q, limit=limit)
171
+ except ExtSearchError as exc:
172
+ raise _fail(exc) from None
173
+
174
+
175
+ @app.get("/mods")
176
+ async def mods_ep(q: str, where: str = "all", limit: int = Query(10, ge=1, le=30)) -> dict[str, Any]:
177
+ try:
178
+ return await mod_search(q, where=where, limit=limit)
179
+ except ExtSearchError as exc:
180
+ raise _fail(exc) from None
181
+
182
+
183
+ @app.get("/freelance")
184
+ async def freelance_ep(q: str, boards: str = "all",
185
+ limit: int = Query(10, ge=1, le=30)) -> dict[str, Any]:
186
+ try:
187
+ return await freelance_search(q, boards=boards, limit=limit)
188
+ except ExtSearchError as exc:
189
+ raise _fail(exc) from None
190
+
191
+
192
+ def main() -> None: # pragma: no cover - entry point
193
+ import argparse
194
+
195
+ import uvicorn
196
+
197
+ parser = argparse.ArgumentParser(description="extsearch HTTP API")
198
+ parser.add_argument("--host", default="127.0.0.1")
199
+ parser.add_argument("--port", type=int, default=8000)
200
+ parser.add_argument("--reload", action="store_true")
201
+ args = parser.parse_args()
202
+ uvicorn.run("extsearch.api:app", host=args.host, port=args.port, reload=args.reload)