From bc9041a2b586111d11052350476cf91eabb58e67 Mon Sep 17 00:00:00 2001 From: Miss-Tired-Ghost Date: Mon, 1 Jun 2026 01:56:05 +1000 Subject: [PATCH 1/4] Fix '**kwargs' bug that prevented passing 'max_results' argument to the 'build_payload' function Allow searching multiple pages if one page is not enough Warnings: The '**kwargs' bug appears to affect more than just 'max_results'. This is a bandaid fix and does not address the root problem. The multi-page search uses a while loop which has the potential to cause indefinite freezing if searches fail continuously Due to the calling of the engines multiple times, the chance of encountering the 'DDGSException(err or "No results found.")' error --- ddgs/base.py | 37 ++++++++++++++++++++++--------- ddgs/ddgs.py | 11 ++++++++- ddgs/engines/bing_images.py | 4 ++++ ddgs/engines/duckduckgo_images.py | 4 ++++ test.py | 18 +++++++++++++++ 5 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 test.py diff --git a/ddgs/base.py b/ddgs/base.py index c8a10276..b76c6abb 100644 --- a/ddgs/base.py +++ b/ddgs/base.py @@ -60,6 +60,9 @@ def build_payload( **kwargs: str, ) -> dict[str, Any]: """Build a payload for the search request.""" + + print(kwargs) + raise NotImplementedError def request(self, *args: Any, **kwargs: Any) -> Any: # noqa: ANN401 @@ -110,14 +113,28 @@ def search( **kwargs: str, ) -> list[T] | None: """Search the engine.""" - payload = self.build_payload( - query=query, region=region, safesearch=safesearch, timelimit=timelimit, page=page, **kwargs - ) - if self.search_method == "GET": - html_text = self.request(self.search_method, self.search_url, params=payload) - else: - html_text = self.request(self.search_method, self.search_url, data=payload) - if not html_text: - return None - results = self.extract_results(html_text) + + print(f"The value of 'max_results': {kwargs.get("max_results")}") + print(f"The contents of kwargs: {kwargs}") + + max_results = int(kwargs.get("max_results", 10)) + results = list() + while len(results) <= max_results: + payload = self.build_payload( + query=query, region=region, safesearch=safesearch, timelimit=timelimit, page=page, **kwargs + ) + if self.search_method == "GET": + html_text = self.request(self.search_method, self.search_url, params=payload) + else: + html_text = self.request(self.search_method, self.search_url, data=payload) + if not html_text: + return None + page += 1 + results += self.extract_results(html_text) + + print(type(results)) + print(len(results)) + print(results) + print(type(results[0])) + return self.post_extract_results(results) diff --git a/ddgs/ddgs.py b/ddgs/ddgs.py index 004c7da9..0ceb59ee 100644 --- a/ddgs/ddgs.py +++ b/ddgs/ddgs.py @@ -141,7 +141,7 @@ def _search_sync( # noqa: C901 region: str = "us-en", safesearch: str = "moderate", timelimit: str | None = None, - max_results: int | None = 10, + # max_results: int | None = 10, page: int = 1, backend: str = "auto", **kwargs: str, @@ -164,6 +164,10 @@ def _search_sync( # noqa: C901 A list of dictionaries containing the search results. """ + + print(f"The value of 'max_results': {kwargs.get("max_results")}") + print(f"The contents of kwargs: {kwargs}") + query = keywords or query if not query: msg = "query is mandatory." @@ -175,6 +179,7 @@ def _search_sync( # noqa: C901 # Perform search results_aggregator: ResultsAggregator[set[str]] = ResultsAggregator({"href", "image", "url", "embed_url"}) + max_results = int(kwargs.get("max_results", 0)) max_workers = min(len_unique_providers, ceil(max_results / 10) + 1) if max_results else len_unique_providers if DDGS.threads: max_workers = min(max_workers, DDGS.threads) @@ -228,6 +233,10 @@ def text(self, query: str, **kwargs: Any) -> list[dict[str, Any]]: # noqa: ANN4 def images(self, query: str, **kwargs: Any) -> list[dict[str, Any]]: # noqa: ANN401 """Perform an image search.""" + + print(f"The value of 'max_results': {kwargs.get("max_results")}") + print(f"The contents of kwargs: {kwargs}") + return self._search_sync("images", query, **kwargs) def news(self, query: str, **kwargs: Any) -> list[dict[str, Any]]: # noqa: ANN401 diff --git a/ddgs/engines/bing_images.py b/ddgs/engines/bing_images.py index fe9dc924..b652f50f 100644 --- a/ddgs/engines/bing_images.py +++ b/ddgs/engines/bing_images.py @@ -29,6 +29,10 @@ def build_payload( **kwargs: str, ) -> dict[str, Any]: """Build a payload for the search request.""" + + print(f"The value of 'max_results': {kwargs.get("max_results")}") + print(f"The contents of kwargs: {kwargs}") + count = max(int(kwargs.get("max_results", 10)), 35) payload = { "q": query, diff --git a/ddgs/engines/duckduckgo_images.py b/ddgs/engines/duckduckgo_images.py index 9f45d4b3..50d1c133 100644 --- a/ddgs/engines/duckduckgo_images.py +++ b/ddgs/engines/duckduckgo_images.py @@ -54,6 +54,10 @@ def build_payload( **kwargs: str, ) -> dict[str, Any]: """Build a payload for the search request.""" + + print(f"The value of 'max_results': {kwargs.get("max_results")}") + print(f"The contents of kwargs: {kwargs}") + safesearch_base = {"on": "1", "moderate": "1", "off": "-1"} timelimit_base = {"d": "Day", "w": "Week", "m": "Month", "y": "Year"} timelimit = f"time:{timelimit_base[timelimit]}" if timelimit else "" diff --git a/test.py b/test.py new file mode 100644 index 00000000..4225ec41 --- /dev/null +++ b/test.py @@ -0,0 +1,18 @@ +""" +Testing for **kwargs bug fix +Testing for multiple page searches +""" +from ddgs import DDGS +from fastcore.all import L + +def search_images(term, max_images=30): + "Search via ddgs; return image classes as a fastcore L." + print(f"Searching for '{term}'") + return L(DDGS().images(query=term, max_results=max_images)) + + +results = search_images('grizzly bear', max_images=150) +if len(results) >= 100: + print("HOOOORRRRAAAAYYYY") + print(f"You found {len(results)} images") +print(results) From 616a7606696d1932df8a880229f5162701e10e93 Mon Sep 17 00:00:00 2001 From: Miss-Tired-Ghost Date: Mon, 1 Jun 2026 01:59:07 +1000 Subject: [PATCH 2/4] Remove Debugging flags and scripts --- ddgs/base.py | 4 ---- ddgs/ddgs.py | 8 -------- ddgs/engines/bing_images.py | 4 ---- ddgs/engines/duckduckgo_images.py | 4 ---- test.py | 18 ------------------ 5 files changed, 38 deletions(-) delete mode 100644 test.py diff --git a/ddgs/base.py b/ddgs/base.py index b76c6abb..a04c5ca1 100644 --- a/ddgs/base.py +++ b/ddgs/base.py @@ -113,10 +113,6 @@ def search( **kwargs: str, ) -> list[T] | None: """Search the engine.""" - - print(f"The value of 'max_results': {kwargs.get("max_results")}") - print(f"The contents of kwargs: {kwargs}") - max_results = int(kwargs.get("max_results", 10)) results = list() while len(results) <= max_results: diff --git a/ddgs/ddgs.py b/ddgs/ddgs.py index 0ceb59ee..5ecc79ee 100644 --- a/ddgs/ddgs.py +++ b/ddgs/ddgs.py @@ -164,10 +164,6 @@ def _search_sync( # noqa: C901 A list of dictionaries containing the search results. """ - - print(f"The value of 'max_results': {kwargs.get("max_results")}") - print(f"The contents of kwargs: {kwargs}") - query = keywords or query if not query: msg = "query is mandatory." @@ -233,10 +229,6 @@ def text(self, query: str, **kwargs: Any) -> list[dict[str, Any]]: # noqa: ANN4 def images(self, query: str, **kwargs: Any) -> list[dict[str, Any]]: # noqa: ANN401 """Perform an image search.""" - - print(f"The value of 'max_results': {kwargs.get("max_results")}") - print(f"The contents of kwargs: {kwargs}") - return self._search_sync("images", query, **kwargs) def news(self, query: str, **kwargs: Any) -> list[dict[str, Any]]: # noqa: ANN401 diff --git a/ddgs/engines/bing_images.py b/ddgs/engines/bing_images.py index b652f50f..fe9dc924 100644 --- a/ddgs/engines/bing_images.py +++ b/ddgs/engines/bing_images.py @@ -29,10 +29,6 @@ def build_payload( **kwargs: str, ) -> dict[str, Any]: """Build a payload for the search request.""" - - print(f"The value of 'max_results': {kwargs.get("max_results")}") - print(f"The contents of kwargs: {kwargs}") - count = max(int(kwargs.get("max_results", 10)), 35) payload = { "q": query, diff --git a/ddgs/engines/duckduckgo_images.py b/ddgs/engines/duckduckgo_images.py index 50d1c133..9f45d4b3 100644 --- a/ddgs/engines/duckduckgo_images.py +++ b/ddgs/engines/duckduckgo_images.py @@ -54,10 +54,6 @@ def build_payload( **kwargs: str, ) -> dict[str, Any]: """Build a payload for the search request.""" - - print(f"The value of 'max_results': {kwargs.get("max_results")}") - print(f"The contents of kwargs: {kwargs}") - safesearch_base = {"on": "1", "moderate": "1", "off": "-1"} timelimit_base = {"d": "Day", "w": "Week", "m": "Month", "y": "Year"} timelimit = f"time:{timelimit_base[timelimit]}" if timelimit else "" diff --git a/test.py b/test.py deleted file mode 100644 index 4225ec41..00000000 --- a/test.py +++ /dev/null @@ -1,18 +0,0 @@ -""" -Testing for **kwargs bug fix -Testing for multiple page searches -""" -from ddgs import DDGS -from fastcore.all import L - -def search_images(term, max_images=30): - "Search via ddgs; return image classes as a fastcore L." - print(f"Searching for '{term}'") - return L(DDGS().images(query=term, max_results=max_images)) - - -results = search_images('grizzly bear', max_images=150) -if len(results) >= 100: - print("HOOOORRRRAAAAYYYY") - print(f"You found {len(results)} images") -print(results) From 340b4371814d76b4ad308a0d9adf1ac125e23c77 Mon Sep 17 00:00:00 2001 From: Miss-Tired-Ghost Date: Mon, 1 Jun 2026 01:59:07 +1000 Subject: [PATCH 3/4] Remove Debugging flags and scripts --- ddgs/base.py | 13 ------------- ddgs/ddgs.py | 9 --------- ddgs/engines/bing_images.py | 4 ---- ddgs/engines/duckduckgo_images.py | 4 ---- test.py | 18 ------------------ 5 files changed, 48 deletions(-) delete mode 100644 test.py diff --git a/ddgs/base.py b/ddgs/base.py index b76c6abb..2c465318 100644 --- a/ddgs/base.py +++ b/ddgs/base.py @@ -60,9 +60,6 @@ def build_payload( **kwargs: str, ) -> dict[str, Any]: """Build a payload for the search request.""" - - print(kwargs) - raise NotImplementedError def request(self, *args: Any, **kwargs: Any) -> Any: # noqa: ANN401 @@ -113,10 +110,6 @@ def search( **kwargs: str, ) -> list[T] | None: """Search the engine.""" - - print(f"The value of 'max_results': {kwargs.get("max_results")}") - print(f"The contents of kwargs: {kwargs}") - max_results = int(kwargs.get("max_results", 10)) results = list() while len(results) <= max_results: @@ -131,10 +124,4 @@ def search( return None page += 1 results += self.extract_results(html_text) - - print(type(results)) - print(len(results)) - print(results) - print(type(results[0])) - return self.post_extract_results(results) diff --git a/ddgs/ddgs.py b/ddgs/ddgs.py index 0ceb59ee..287a14cf 100644 --- a/ddgs/ddgs.py +++ b/ddgs/ddgs.py @@ -141,7 +141,6 @@ def _search_sync( # noqa: C901 region: str = "us-en", safesearch: str = "moderate", timelimit: str | None = None, - # max_results: int | None = 10, page: int = 1, backend: str = "auto", **kwargs: str, @@ -164,10 +163,6 @@ def _search_sync( # noqa: C901 A list of dictionaries containing the search results. """ - - print(f"The value of 'max_results': {kwargs.get("max_results")}") - print(f"The contents of kwargs: {kwargs}") - query = keywords or query if not query: msg = "query is mandatory." @@ -233,10 +228,6 @@ def text(self, query: str, **kwargs: Any) -> list[dict[str, Any]]: # noqa: ANN4 def images(self, query: str, **kwargs: Any) -> list[dict[str, Any]]: # noqa: ANN401 """Perform an image search.""" - - print(f"The value of 'max_results': {kwargs.get("max_results")}") - print(f"The contents of kwargs: {kwargs}") - return self._search_sync("images", query, **kwargs) def news(self, query: str, **kwargs: Any) -> list[dict[str, Any]]: # noqa: ANN401 diff --git a/ddgs/engines/bing_images.py b/ddgs/engines/bing_images.py index b652f50f..fe9dc924 100644 --- a/ddgs/engines/bing_images.py +++ b/ddgs/engines/bing_images.py @@ -29,10 +29,6 @@ def build_payload( **kwargs: str, ) -> dict[str, Any]: """Build a payload for the search request.""" - - print(f"The value of 'max_results': {kwargs.get("max_results")}") - print(f"The contents of kwargs: {kwargs}") - count = max(int(kwargs.get("max_results", 10)), 35) payload = { "q": query, diff --git a/ddgs/engines/duckduckgo_images.py b/ddgs/engines/duckduckgo_images.py index 50d1c133..9f45d4b3 100644 --- a/ddgs/engines/duckduckgo_images.py +++ b/ddgs/engines/duckduckgo_images.py @@ -54,10 +54,6 @@ def build_payload( **kwargs: str, ) -> dict[str, Any]: """Build a payload for the search request.""" - - print(f"The value of 'max_results': {kwargs.get("max_results")}") - print(f"The contents of kwargs: {kwargs}") - safesearch_base = {"on": "1", "moderate": "1", "off": "-1"} timelimit_base = {"d": "Day", "w": "Week", "m": "Month", "y": "Year"} timelimit = f"time:{timelimit_base[timelimit]}" if timelimit else "" diff --git a/test.py b/test.py deleted file mode 100644 index 4225ec41..00000000 --- a/test.py +++ /dev/null @@ -1,18 +0,0 @@ -""" -Testing for **kwargs bug fix -Testing for multiple page searches -""" -from ddgs import DDGS -from fastcore.all import L - -def search_images(term, max_images=30): - "Search via ddgs; return image classes as a fastcore L." - print(f"Searching for '{term}'") - return L(DDGS().images(query=term, max_results=max_images)) - - -results = search_images('grizzly bear', max_images=150) -if len(results) >= 100: - print("HOOOORRRRAAAAYYYY") - print(f"You found {len(results)} images") -print(results) From 7dd03fcd638e3e3a06a0b799b5c2e9cec3094db4 Mon Sep 17 00:00:00 2001 From: Miss-Tired-Ghost Date: Mon, 1 Jun 2026 03:24:32 +1000 Subject: [PATCH 4/4] Fix '**kwargs' bug that prevented changing backend --- ddgs/ddgs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddgs/ddgs.py b/ddgs/ddgs.py index 287a14cf..e5fb1fa2 100644 --- a/ddgs/ddgs.py +++ b/ddgs/ddgs.py @@ -142,7 +142,6 @@ def _search_sync( # noqa: C901 safesearch: str = "moderate", timelimit: str | None = None, page: int = 1, - backend: str = "auto", **kwargs: str, ) -> list[dict[str, Any]]: """Perform a search across engines in the given category. @@ -168,6 +167,7 @@ def _search_sync( # noqa: C901 msg = "query is mandatory." raise DDGSException(msg) + backend = kwargs.get("backend", "auto") engines = self._get_engines(category, backend) len_unique_providers = len({engine.provider for engine in engines}) seen_providers: set[str] = set()