From 119e50075ef8aba837cb2e550bbc01f2fdeb0c80 Mon Sep 17 00:00:00 2001 From: Mizarka Date: Wed, 20 May 2026 12:57:30 +0000 Subject: [PATCH] refactor(ddgs): improve extract performance The previous map-based approach, while prettier-looking, caused Python to eagerly calculate all possible return formats to insert on the map, only to discard most and keep only one. The new, slightly longer if-based approach only accesses a single format attribute, calculating only one. --- ddgs/ddgs.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/ddgs/ddgs.py b/ddgs/ddgs.py index 40f853a..004c7da 100644 --- a/ddgs/ddgs.py +++ b/ddgs/ddgs.py @@ -259,11 +259,14 @@ def extract(self, url: str, fmt: str = "text_markdown") -> dict[str, str | bytes msg = f"Failed to fetch {url}: HTTP {resp.status_code}" raise DDGSException(msg) - content_map: dict[str, str | bytes] = { - "text_markdown": resp.text_markdown, - "text_plain": resp.text_plain, - "text_rich": resp.text_rich, - "text": resp.text, - "content": resp.content, - } - return {"url": url, "content": content_map.get(fmt, resp.text_markdown)} + if fmt == "text_plain": + content = resp.text_plain + elif fmt == "text_rich": + content = resp.text_rich + elif fmt == "text": + content = resp.text + elif fmt == "content": + content = resp.content + else: + content = resp.text_markdown + return {"url": url, "content": content}