Update app.py
Browse files123import g4ffrom webscout import DDGSfrom time import time as t1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000import asyncioimport loggingimport warningsfrom typing import Dict, Generator, Optionalimport nest_asyncioimport jsonimport loggingimport sysfrom collections import dequefrom datetime import datetime, timezonefrom decimal import Decimalfrom itertools import cyclefrom typing import AsyncGenerator, Deque, Dict, Optional, Set, Tuplefrom curl_cffi import requestsfrom docstring_inheritance import GoogleDocstringInheritanceMetafrom lxml import htmlimport jsonimport refrom html import unescapefrom typing import Optionalfrom urllib.parse import unquotefrom dataclasses import dataclassfrom typing import Dict, Optionalfrom random import randintclass DuckDuckGoSearchException(Exception):"""Base exception class for webscout."""@dataclassclass MapsResult:"""Represents a result from the maps search."""title: Optional[str] = Noneaddress: Optional[str] = Nonecountry_code: Optional[str] = Nonelatitude: Optional[str] = Nonelongitude: Optional[str] = Noneurl: Optional[str] = Nonedesc: Optional[str] = Nonephone: Optional[str] = Noneimage: Optional[str] = Nonesource: Optional[str] = Nonehours: Optional[Dict[str, str]] = Nonecategory: Optional[str] = Nonefacebook: Optional[str] = Noneinstagram: Optional[str] = Nonetwitter: Optional[str] = NoneREGEX_500_IN_URL = re.compile(r"(?:\d{3}-\d{2}\.js)")REGEX_STRIP_TAGS = re.compile("<.*?>")REGEX_VQD = re.compile(rb"""vqd=['"]?([^&"']+)""")def _extract_vqd(html_bytes: bytes, keywords: str) -> Optional[str]:"""Extract vqd from html using a regular expression."""try:match = REGEX_VQD.search(html_bytes)if match:return match.group(1).decode()except Exception:passraise DuckDuckGoSearchException(f"_extract_vqd() {keywords=} Could not extract vqd.")def _text_extract_json(html_bytes: bytes, keywords: str) -> Optional[str]:"""text(backend="api") -> extract json from html."""try:start = html_bytes.index(b"DDG.pageLayout.load('d',") + 24end = html_bytes.index(b");DDG.duckbar.load(", start)data = html_bytes[start:end]return json.loads(data)except Exception as ex:raise DuckDuckGoSearchException(f"_text_extract_json() {keywords=} {type(ex).__name__}: {ex}") from exdef _is_500_in_url(url: str) -> bool:"""Something like '506-00.js' inside the url."""return bool(REGEX_500_IN_URL.search(url))def _normalize(raw_html: str) -> str:"""Strip HTML tags from the raw_html string."""return unescape(REGEX_STRIP_TAGS.sub("", raw_html)) if raw_html else ""def _normalize_url(url: str) -> str:"""Unquote URL and replace spaces with '+'."""return unquote(url.replace(" ", "+")) if url else ""logger = logging.getLogger("duckduckgo_search.AsyncDDGS")# Not working on Windows, NotImplementedError (https://curl-cffi.readthedocs.io/en/latest/faq/)if sys.platform.lower().startswith("win"):asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())class AsyncDDGS(metaclass=GoogleDocstringInheritanceMeta):"""webscout_search async class to get search results from duckduckgo.com."""def __init__(self, headers=None, proxies=None, timeout=10) -> None:"""Initialize the AsyncDDGS object.Args:headers (dict, optional): Dictionary of headers for the HTTP client. Defaults to None.proxies (Union[dict, str], optional): Proxies for the HTTP client (can be dict or str). Defaults to None.timeout (int, optional): Timeout value for the HTTP client. Defaults to 10."""useragent=f'{randint(0, 1000000)}'headers = {'User-Agent': useragent}self.proxies = proxies if proxies and isinstance(proxies, dict) else {"http": proxies, "https": proxies}self._asession = requests.AsyncSession(headers=headers, proxies=self.proxies, timeout=timeout, impersonate="chrome")self._asession.headers["Referer"] = "https://duckduckgo.com/"async def __aenter__(self) -> "AsyncDDGS":"""A context manager method that is called when entering the 'with' statement."""return selfasync def __aexit__(self, exc_type, exc_val, exc_tb) -> None:"""Closes the session."""return self._asession.close()async def _aget_url(self, method: str, url: str, **kwargs) -> Optional[requests.Response]:try:useragent=f'{randint(0, 1000000)}'headers = {'User-Agent': useragent}resp = await self._asession.request(method, url, stream=True, **kwargs,headers=headers)resp.raise_for_status()resp_content = await resp.acontent()logger.debug(f"_aget_url() {url} {resp.status_code} {resp.http_version} {resp.elapsed} {len(resp_content)}")if _is_500_in_url(str(resp.url)) or resp.status_code == 202:raise DuckDuckGoSearchException("Ratelimit")if resp.status_code == 200:return resp_contentexcept Exception as ex:raise DuckDuckGoSearchException(f"_aget_url() {url} {type(ex).__name__}: {ex}") from exasync def _aget_vqd(self, keywords: str) -> Optional[str]:"""Get vqd value for a search query."""resp_content = await self._aget_url("POST", "https://duckduckgo.com", data={"q": keywords})if resp_content:return _extract_vqd(resp_content, keywords)async def text(self,keywords: str,region: str = "wt-wt",safesearch: str = "moderate",timelimit: Optional[str] = None,backend: str = "api",max_results: Optional[int] = None,) -> AsyncGenerator[Dict[str, Optional[str]], None]:"""DuckDuckGo text search generator. Query params: https://duckduckgo.com/params.Args:keywords: keywords for query.region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".safesearch: on, moderate, off. Defaults to "moderate".timelimit: d, w, m, y. Defaults to None.backend: api, html, lite. Defaults to api.api - collect data from https://duckduckgo.com,html - collect data from https://html.duckduckgo.com,lite - collect data from https://lite.duckduckgo.com.max_results: max number of results. If None, returns results only from the first response. Defaults to None.Yields:dict with search results."""if backend == "api":results = self._text_api(keywords, region, safesearch, timelimit, max_results)elif backend == "html":results = self._text_html(keywords, region, safesearch, timelimit, max_results)elif backend == "lite":results = self._text_lite(keywords, region, timelimit, max_results)async for result in results:yield resultasync def _text_api(self,keywords: str,region: str = "wt-wt",safesearch: str = "moderate",timelimit: Optional[str] = None,max_results: Optional[int] = None,) -> AsyncGenerator[Dict[str, Optional[str]], None]:"""webscout text search generator. Query params: https://duckduckgo.com/params.Args:keywords: keywords for query.region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".safesearch: on, moderate, off. Defaults to "moderate".timelimit: d, w, m, y. Defaults to None.max_results: max number of results. If None, returns results only from the first response. Defaults to None.Yields:dict with search results."""assert keywords, "keywords is mandatory"vqd = await self._aget_vqd(keywords)payload = {"q": keywords,"kl": region,"l": region,"bing_market": region,"s": "0","df": timelimit,"vqd": vqd,# "o": "json","sp": "0",}safesearch = safesearch.lower()if safesearch == "moderate":payload["ex"] = "-1"elif safesearch == "off":payload["ex"] = "-2"elif safesearch == "on": # strictpayload["p"] = "1"cache = set()for _ in range(11):resp_content = await self._aget_url("GET", "https://links.duckduckgo.com/d.js", params=payload)if resp_content is None:returnpage_data = _text_extract_json(resp_content, keywords)if page_data is None:returnresult_exists, next_page_url = False, Nonefor row in page_data:href = row.get("u", None)if href and href not in cache and href != f"http://www.google.com/search?q={keywords}":cache.add(href)body = _normalize(row["a"])if body:result_exists = Trueyield {"title": _normalize(row["t"]),"href": _normalize_url(href),"body": body,}if max_results and len(cache) >= max_results:returnelse:next_page_url = row.get("n", None)if max_results is None or result_exists is False or next_page_url is None:returnpayload["s"] = next_page_url.split("s=")[1].split("&")[0]async def _text_html(self,keywords: str,region: str = "wt-wt",safesearch: str = "moderate",timelimit: Optional[str] = None,max_results: Optional[int] = None,) -> AsyncGenerator[Dict[str, Optional[str]], None]:"""webscout text search generator. Query params: https://duckduckgo.com/params.Args:keywords: keywords for query.region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".safesearch: on, moderate, off. Defaults to "moderate".timelimit: d, w, m, y. Defaults to None.max_results: max number of results. If None, returns results only from the first response. Defaults to None.Yields:dict with search results."""assert keywords, "keywords is mandatory"self._asession.headers["Referer"] = "https://html.duckduckgo.com/"safesearch_base = {"on": 1, "moderate": -1, "off": -2}payload = {"q": keywords,"s": "0","kl": region,"p": safesearch_base[safesearch.lower()],"df": timelimit,}cache: Set[str] = set()for _ in range(11):resp_content = await self._aget_url("POST", "https://html.duckduckgo.com/html", data=payload)if resp_content is None:returntree = html.fromstring(resp_content)if tree.xpath('//div[@class="no-results"]/text()'):returnresult_exists = Falsefor e in tree.xpath('//div[contains(@class, "results_links")]'):href = e.xpath('.//a[contains(@class, "result__a")]/@href')href = href[0] if href else Noneif (hrefand href not in cacheand href != f"http://www.google.com/search?q={keywords}"and not href.startswith("https://duckduckgo.com/y.js?ad_domain")):cache.add(href)title = e.xpath('.//a[contains(@class, "result__a")]/text()')body = e.xpath('.//a[contains(@class, "result__snippet")]//text()')result_exists = Trueyield {"title": _normalize(title[0]) if title else None,"href": _normalize_url(href),"body": _normalize("".join(body)) if body else None,}if max_results and len(cache) >= max_results:returnif max_results is None or result_exists is False:returnnext_page = tree.xpath('.//div[@class="nav-link"]')next_page = next_page[-1] if next_page else Noneif next_page is None:returnnames = next_page.xpath('.//input[@type="hidden"]/@name')values = next_page.xpath('.//input[@type="hidden"]/@value')payload = {n: v for n, v in zip(names, values)}async def _text_lite(self,keywords: str,region: str = "wt-wt",timelimit: Optional[str] = None,max_results: Optional[int] = None,) -> AsyncGenerator[Dict[str, Optional[str]], None]:"""webscout text search generator. Query params: https://duckduckgo.com/params.Args:keywords: keywords for query.region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".timelimit: d, w, m, y. Defaults to None.max_results: max number of results. If None, returns results only from the first response. Defaults to None.Yields:dict with search results."""assert keywords, "keywords is mandatory"self._asession.headers["Referer"] = "https://lite.duckduckgo.com/"payload = {"q": keywords,"s": "0","o": "json","api": "d.js","kl": region,"df": timelimit,}cache: Set[str] = set()for _ in range(11):resp_content = await self._aget_url("POST", "https://lite.duckduckgo.com/lite/", data=payload)if resp_content is None:returnif b"No more results." in resp_content:returntree = html.fromstring(resp_content)result_exists = Falsedata = zip(cycle(range(1, 5)), tree.xpath("//table[last()]//tr"))for i, e in data:if i == 1:href = e.xpath(".//a//@href")href = href[0] if href else Noneif (href is Noneor href in cacheor href == f"http://www.google.com/search?q={keywords}"or href.startswith("https://duckduckgo.com/y.js?ad_domain")):[next(data, None) for _ in range(3)] # skip block(i=1,2,3,4)else:cache.add(href)title = e.xpath(".//a//text()")[0]elif i == 2:body = e.xpath(".//td[@class='result-snippet']//text()")body = "".join(body).strip()elif i == 3:result_exists = Trueyield {"title": _normalize(title),"href": _normalize_url(href),"body": _normalize(body),}if max_results and len(cache) >= max_results:returnif max_results is None or result_exists is False:returnnext_page_s = tree.xpath("//form[./input[contains(@value, 'ext')]]/input[@name='s']/@value")if not next_page_s:returnpayload["s"] = next_page_s[0]payload["vqd"] = _extract_vqd(resp_content, keywords)async def images(self,keywords: str,region: str = "wt-wt",safesearch: str = "moderate",timelimit: Optional[str] = None,size: Optional[str] = None,color: Optional[str] = None,type_image: Optional[str] = None,layout: Optional[str] = None,license_image: Optional[str] = None,max_results: Optional[int] = None,) -> AsyncGenerator[Dict[str, Optional[str]], None]:"""webscout images search. Query params: https://duckduckgo.com/params.Args:keywords: keywords for query.region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".safesearch: on, moderate, off. Defaults to "moderate".timelimit: Day, Week, Month, Year. Defaults to None.size: Small, Medium, Large, Wallpaper. Defaults to None.color: color, Monochrome, Red, Orange, Yellow, Green, Blue,Purple, Pink, Brown, Black, Gray, Teal, White. Defaults to None.type_image: photo, clipart, gif, transparent, line.Defaults to None.layout: Square, Tall, Wide. Defaults to None.license_image: any (All Creative Commons), Public (PublicDomain),Share (Free to Share and Use), ShareCommercially (Free to Share and Use Commercially),Modify (Free to Modify, Share, and Use), ModifyCommercially (Free to Modify, Share, andUse Commercially). Defaults to None.max_results: max number of results. If None, returns results only from the first response. Defaults to None.Yields:dict with image search results."""assert keywords, "keywords is mandatory"vqd = await self._aget_vqd(keywords)safesearch_base = {"on": 1, "moderate": 1, "off": -1}timelimit = f"time:{timelimit}" if timelimit else ""size = f"size:{size}" if size else ""color = f"color:{color}" if color else ""type_image = f"type:{type_image}" if type_image else ""layout = f"layout:{layout}" if layout else ""license_image = f"license:{license_image}" if license_image else ""payload = {"l": region,"o": "json","q": keywords,"vqd": vqd,"f": f"{timelimit},{size},{color},{type_image},{layout},{license_image}","p": safesearch_base[safesearch.lower()],}cache = set()for _ in range(10):resp_content = await self._aget_url("GET", "https://duckduckgo.com/i.js", params=payload)if resp_content is None:returntry:resp_json = json.loads(resp_content)except Exception:returnpage_data = resp_json.get("results", None)if page_data is None:returnresult_exists = Falsefor row in page_data:image_url = row.get("image", None)if image_url and image_url not in cache:cache.add(image_url)result_exists = Trueyield {"title": row["title"],"image": _normalize_url(image_url),"thumbnail": _normalize_url(row["thumbnail"]),"url": _normalize_url(row["url"]),"height": row["height"],"width": row["width"],"source": row["source"],}if max_results and len(cache) >= max_results:returnif max_results is None or result_exists is False:returnnext = resp_json.get("next", None)if next is None:returnpayload["s"] = next.split("s=")[-1].split("&")[0]async def videos(self,keywords: str,region: str = "wt-wt",safesearch: str = "moderate",timelimit: Optional[str] = None,resolution: Optional[str] = None,duration: Optional[str] = None,license_videos: Optional[str] = None,max_results: Optional[int] = None,) -> AsyncGenerator[Dict[str, Optional[str]], None]:"""webscout videos search. Query params: https://duckduckgo.com/params.Args:keywords: keywords for query.region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".safesearch: on, moderate, off. Defaults to "moderate".timelimit: d, w, m. Defaults to None.resolution: high, standart. Defaults to None.duration: short, medium, long. Defaults to None.license_videos: creativeCommon, youtube. Defaults to None.max_results: max number of results. If None, returns results only from the first response. Defaults to None.Yields:dict with videos search results"""assert keywords, "keywords is mandatory"vqd = await self._aget_vqd(keywords)safesearch_base = {"on": 1, "moderate": -1, "off": -2}timelimit = f"publishedAfter:{timelimit}" if timelimit else ""resolution = f"videoDefinition:{resolution}" if resolution else ""duration = f"videoDuration:{duration}" if duration else ""license_videos = f"videoLicense:{license_videos}" if license_videos else ""payload = {"l": region,"o": "json","s": 0,"q": keywords,"vqd": vqd,"f": f"{timelimit},{resolution},{duration},{license_videos}","p": safesearch_base[safesearch.lower()],}cache = set()for _ in range(10):resp_content = await self._aget_url("GET", "https://duckduckgo.com/v.js", params=payload)if resp_content is None:returntry:resp_json = json.loads(resp_content)except Exception:returnpage_data = resp_json.get("results", None)if page_data is None:returnresult_exists = Falsefor row in page_data:if row["content"] not in cache:cache.add(row["content"])result_exists = Trueyield rowif max_results and len(cache) >= max_results:returnif max_results is None or result_exists is False:returnnext = resp_json.get("next", None)if next is None:returnpayload["s"] = next.split("s=")[-1].split("&")[0]async def news(self,keywords: str,region: str = "wt-wt",safesearch: str = "moderate",timelimit: Optional[str] = None,max_results: Optional[int] = None,) -> AsyncGenerator[Dict[str, Optional[str]], None]:"""webscout news search. Query params: https://duckduckgo.com/params.Args:keywords: keywords for query.region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".safesearch: on, moderate, off. Defaults to "moderate".timelimit: d, w, m. Defaults to None.max_results: max number of results. If None, returns results only from the first response. Defaults to None.Yields:dict with news search results."""assert keywords, "keywords is mandatory"vqd = await self._aget_vqd(keywords)safesearch_base = {"on": 1, "moderate": -1, "off": -2}payload = {"l": region,"o": "json","noamp": "1","q": keywords,"vqd": vqd,"p": safesearch_base[safesearch.lower()],"df": timelimit,"s": 0,}cache = set()for _ in range(10):resp_content = await self._aget_url("GET", "https://duckduckgo.com/news.js", params=payload)if resp_content is None:returntry:resp_json = json.loads(resp_content)except Exception:returnpage_data = resp_json.get("results", None)if page_data is None:returnresult_exists = Falsefor row in page_data:if row["url"] not in cache:cache.add(row["url"])image_url = row.get("image", None)result_exists = Trueyield {"date": datetime.fromtimestamp(row["date"], timezone.utc).isoformat(),"title": row["title"],"body": _normalize(row["excerpt"]),"url": _normalize_url(row["url"]),"image": _normalize_url(image_url) if image_url else None,"source": row["source"],}if max_results and len(cache) >= max_results:returnif max_results is None or result_exists is False:returnnext = resp_json.get("next", None)if next is None:returnpayload["s"] = next.split("s=")[-1].split("&")[0]async def answers(self, keywords: str) -> AsyncGenerator[Dict[str, Optional[str]], None]:"""webscout instant answers. Query params: https://duckduckgo.com/params.Args:keywords: keywords for query.Yields:dict with instant answers results."""assert keywords, "keywords is mandatory"payload = {"q": f"what is {keywords}","format": "json",}resp_content = await self._aget_url("GET", "https://api.duckduckgo.com/", params=payload)if resp_content is None:yield Nonetry:page_data = json.loads(resp_content)except Exception:page_data = Noneif page_data:answer = page_data.get("AbstractText", None)url = page_data.get("AbstractURL", None)if answer:yield {"icon": None,"text": answer,"topic": None,"url": url,}# related:payload = {"q": f"{keywords}","format": "json",}resp_content = await self._aget_url("GET", "https://api.duckduckgo.com/", params=payload)if resp_content is None:yield Nonetry:page_data = json.loads(resp_content).get("RelatedTopics", None)except Exception:page_data = Noneif page_data:for row in page_data:topic = row.get("Name", None)if not topic:icon = row["Icon"].get("URL", None)yield {"icon": f"https://duckduckgo.com{icon}" if icon else None,"text": row["Text"],"topic": None,"url": row["FirstURL"],}else:for subrow in row["Topics"]:icon = subrow["Icon"].get("URL", None)yield {"icon": f"https://duckduckgo.com{icon}" if icon else None,"text": subrow["Text"],"topic": topic,"url": subrow["FirstURL"],}async def suggestions(self, keywords: str, region: str = "wt-wt") -> AsyncGenerator[Dict[str, Optional[str]], None]:"""webscout suggestions. Query params: https://duckduckgo.com/params.Args:keywords: keywords for query.region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".Yields:dict with suggestions results."""assert keywords, "keywords is mandatory"payload = {"q": keywords,"kl": region,}resp_content = await self._aget_url("GET", "https://duckduckgo.com/ac", params=payload)if resp_content is None:yield Nonetry:page_data = json.loads(resp_content)for r in page_data:yield rexcept Exception:passasync def maps(self,keywords: str,place: Optional[str] = None,street: Optional[str] = None,city: Optional[str] = None,county: Optional[str] = None,state: Optional[str] = None,country: Optional[str] = None,postalcode: Optional[str] = None,latitude: Optional[str] = None,longitude: Optional[str] = None,radius: int = 0,max_results: Optional[int] = None,) -> AsyncGenerator[Dict[str, Optional[str]], None]:"""webscout maps search. Query params: https://duckduckgo.com/params.Args:keywords: keywords for queryplace: if set, the other parameters are not used. Defaults to None.street: house number/street. Defaults to None.city: city of search. Defaults to None.county: county of search. Defaults to None.state: state of search. Defaults to None.country: country of search. Defaults to None.postalcode: postalcode of search. Defaults to None.latitude: geographic coordinate (north-south position). Defaults to None.longitude: geographic coordinate (east-west position); if latitude andlongitude are set, the other parameters are not used. Defaults to None.radius: expand the search square by the distance in kilometers. Defaults to 0.max_results: max number of results. If None, returns results only from the first response. Defaults to None.Yields:dict with maps search results"""assert keywords, "keywords is mandatory"vqd = await self._aget_vqd(keywords)# if longitude and latitude are specified, skip the request about bbox to the nominatim apiif latitude and longitude:lat_t = Decimal(latitude.replace(",", "."))lat_b = Decimal(latitude.replace(",", "."))lon_l = Decimal(longitude.replace(",", "."))lon_r = Decimal(longitude.replace(",", "."))if radius == 0:radius = 1# otherwise request about bbox to nominatim apielse:if place:params: Dict[str, Optional[str]] = {"q": place,"polygon_geojson": "0","format": "jsonv2",}else:params = {"street": street,"city": city,"county": county,"state": state,"country": country,"postalcode": postalcode,"polygon_geojson": "0","format": "jsonv2",}try:resp_content = await self._aget_url("GET","https://nominatim.openstreetmap.org/search.php",params=params,)if resp_content is None:yield Nonecoordinates = json.loads(resp_content)[0]["boundingbox"]lat_t, lon_l = Decimal(coordinates[1]), Decimal(coordinates[2])lat_b, lon_r = Decimal(coordinates[0]), Decimal(coordinates[3])except Exception as ex:logger.debug(f"ddg_maps() keywords={keywords} {type(ex).__name__} {ex}")return# if a radius is specified, expand the search squarelat_t += Decimal(radius) * Decimal(0.008983)lat_b -= Decimal(radius) * Decimal(0.008983)lon_l -= Decimal(radius) * Decimal(0.008983)lon_r += Decimal(radius) * Decimal(0.008983)logger.debug(f"bbox coordinates\n{lat_t} {lon_l}\n{lat_b} {lon_r}")# сreate a queue of search squares (bboxes)work_bboxes: Deque[Tuple[Decimal, Decimal, Decimal, Decimal]] = deque()work_bboxes.append((lat_t, lon_l, lat_b, lon_r))# bbox iteratecache = set()while work_bboxes:lat_t, lon_l, lat_b, lon_r = work_bboxes.pop()params = {"q": keywords,"vqd": vqd,"tg": "maps_places","rt": "D","mkexp": "b","wiki_info": "1","is_requery": "1","bbox_tl": f"{lat_t},{lon_l}","bbox_br": f"{lat_b},{lon_r}","strict_bbox": "1",}resp_content = await self._aget_url("GET", "https://duckduckgo.com/local.js", params=params)if resp_content is None:returntry:page_data = json.loads(resp_content).get("results", [])except Exception:returnif page_data is None:returnfor res in page_data:result = MapsResult()result.title = res["name"]result.address = res["address"]if f"{result.title} {result.address}" in cache:continueelse:cache.add(f"{result.title} {result.address}")result.country_code = res["country_code"]result.url = _normalize_url(res["website"])result.phone = res["phone"]result.latitude = res["coordinates"]["latitude"]result.longitude = res["coordinates"]["longitude"]result.source = _normalize_url(res["url"])if res["embed"]:result.image = res["embed"].get("image", "")result.desc = res["embed"].get("description", "")result.hours = res["hours"]result.category = res["ddg_category"]result.facebook = f"www.facebook.com/profile.php?id={x}" if (x := res["facebook_id"]) else Noneresult.instagram = f"https://www.instagram.com/{x}" if (x := res["instagram_id"]) else Noneresult.twitter = f"https://twitter.com/{x}" if (x := res["twitter_id"]) else Noneyield result.__dict__if max_results and len(cache) >= max_results:returnif max_results is None:return# divide the square into 4 parts and add to the queueif len(page_data) >= 15:lat_middle = (lat_t + lat_b) / 2lon_middle = (lon_l + lon_r) / 2bbox1 = (lat_t, lon_l, lat_middle, lon_middle)bbox2 = (lat_t, lon_middle, lat_middle, lon_r)bbox3 = (lat_middle, lon_l, lat_b, lon_middle)bbox4 = (lat_middle, lon_middle, lat_b, lon_r)work_bboxes.extendleft([bbox1, bbox2, bbox3, bbox4])async def translate(self, keywords: str, from_: Optional[str] = None, to: str = "en") -> Optional[Dict[str, Optional[str]]]:"""webscout translate.Args:keywords: string or a list of strings to translatefrom_: translate from (defaults automatically). Defaults to None.to: what language to translate. Defaults to "en".Returns:dict with translated keywords."""assert keywords, "keywords is mandatory"vqd = await self._aget_vqd("translate")payload = {"vqd": vqd,"query": "translate","to": to,}if from_:payload["from"] = from_resp_content = await self._aget_url("POST","https://duckduckgo.com/translation.js",params=payload,data=keywords.encode(),)if resp_content is None:return Nonetry:page_data = json.loads(resp_content)page_data["original"] = keywordsexcept Exception:page_data = Nonereturn page_datalogger = logging.getLogger("duckduckgo_search.DDGS")nest_asyncio.apply()class DDGS(AsyncDDGS):def __init__(self, headers=None, proxies=None, timeout=10):if asyncio.get_event_loop().is_running():warnings.warn("DDGS running in an async loop. This may cause errors. Use AsyncDDGS instead.", stacklevel=2)super().__init__(headers, proxies, timeout)self._loop = asyncio.get_event_loop()def __enter__(self) -> "DDGS":return selfdef __exit__(self, exc_type, exc_val, exc_tb) -> None:self._loop.create_task(self.__aexit__(exc_type, exc_val, exc_tb))def _iter_over_async(self, async_gen):"""Iterate over an async generator."""while True:try:yield self._loop.run_until_complete(async_gen.__anext__())except StopAsyncIteration:breakdef text(self, *args, **kwargs) -> Generator[Dict[str, Optional[str]], None, None]:async_gen = super().text(*args, **kwargs)return self._iter_over_async(async_gen)def images(self, *args, **kwargs) -> Generator[Dict[str, Optional[str]], None, None]:async_gen = super().images(*args, **kwargs)return self._iter_over_async(async_gen)def videos(self, *args, **kwargs) -> Generator[Dict[str, Optional[str]], None, None]:async_gen = super().videos(*args, **kwargs)return self._iter_over_async(async_gen)def news(self, *args, **kwargs) -> Generator[Dict[str, Optional[str]], None, None]:async_gen = super().news(*args, **kwargs)return self._iter_over_async(async_gen)def answers(self, *args, **kwargs) -> Generator[Dict[str, Optional[str]], None, None]:async_gen = super().answers(*args, **kwargs)return self._iter_over_async(async_gen)def suggestions(self, *args, **kwargs) -> Generator[Dict[str, Optional[str]], None, None]:async_gen = super().suggestions(*args, **kwargs)return self._iter_over_async(async_gen)def maps(self, *args, **kwargs) -> Generator[Dict[str, Optional[str]], None, None]:async_gen = super().maps(*args, **kwargs)return self._iter_over_async(async_gen)def translate(self, *args, **kwargs) -> Optional[Dict[str, Optional[str]]]:async_coro = super().translate(*args, **kwargs)return self._loop.run_until_complete(async_coro)import g4ffrom webscout import DDGSfrom time import time as t
app.py+997