From 237fc12ca80747723396179f6b8b8195d893e852 Mon Sep 17 00:00:00 2001 From: deedy5 <65482418+deedy5@users.noreply.github.com> Date: Sat, 7 Dec 2024 12:50:28 +0300 Subject: [PATCH] use env var DDGS_PROXY --- README.md | 36 ++++++++++++++------------ duckduckgo_search/duckduckgo_search.py | 12 +++++---- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 2b53383..560e0af 100755 --- a/README.md +++ b/README.md @@ -168,7 +168,7 @@ class DDGS: """ ``` -Here is an example of initializing the DDGS class. +Here is an example of initializing the DDGS class. ```python3 from duckduckgo_search import DDGS @@ -212,6 +212,10 @@ results = ddgs.text("something you need", max_results=50) ddgs = DDGS(proxy="socks5h://user:password@geo.iproyal.com:32325", timeout=20) results = ddgs.text("something you need", max_results=50) ``` +*3. The proxy can also be set using the `DDGS_PROXY` environment variable.* +```python3 +export DDGS_PROXY="socks5h://user:password@geo.iproyal.com:32325" +``` [Go To TOP](#TOP) @@ -222,7 +226,7 @@ Exceptions: - `RatelimitException`: Inherits from DuckDuckGoSearchException, raised for exceeding API request rate limits. - `TimeoutException`: Inherits from DuckDuckGoSearchException, raised for API request timeouts. - + [Go To TOP](#TOP) ## 1. chat() - AI chat @@ -304,10 +308,10 @@ print(results) ```python def answers(keywords: str) -> list[dict[str, str]]: """DuckDuckGo instant answers. Query params: https://duckduckgo.com/params. - + Args: keywords: keywords for query, - + Returns: List of dictionaries with instant answers results. """ @@ -347,7 +351,7 @@ def images( max_results: int | None = None, ) -> list[dict[str, str]]: """DuckDuckGo 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". @@ -364,7 +368,7 @@ def images( Modify (Free to Modify, Share, and Use), ModifyCommercially (Free to Modify, Share, and Use Commercially). Defaults to None. max_results: max number of results. If None, returns results only from the first response. Defaults to None. - + Returns: List of dictionaries with images search results. """ @@ -415,7 +419,7 @@ def videos( max_results: int | None = None, ) -> list[dict[str, str]]: """DuckDuckGo 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". @@ -425,7 +429,7 @@ def videos( 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. - + Returns: List of dictionaries with videos search results. """ @@ -482,14 +486,14 @@ def news( max_results: int | None = None, ) -> list[dict[str, str]]: """DuckDuckGo 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. - + Returns: List of dictionaries with news search results. """ @@ -533,7 +537,7 @@ def maps( max_results: int | None = None, ) -> list[dict[str, str]]: """DuckDuckGo maps search. Query params: https://duckduckgo.com/params. - + Args: keywords: keywords for query place: if set, the other parameters are not used. Defaults to None. @@ -548,7 +552,7 @@ def maps( longitude 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. - + Returns: List of dictionaries with maps search results. """ @@ -605,12 +609,12 @@ def translate( to: str = "en", ) -> list[dict[str, str]]: """DuckDuckGo translate. - + Args: keywords: string or list of strings to translate. from_: translate from (defaults automatically). Defaults to None. to: what language to translate. Defaults to "en". - + Returns: List od dictionaries with translated keywords. """ @@ -638,11 +642,11 @@ def suggestions( region: str = "wt-wt", ) -> list[dict[str, str]]: """DuckDuckGo 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". - + Returns: List of dictionaries with suggestions results. """ diff --git a/duckduckgo_search/duckduckgo_search.py b/duckduckgo_search/duckduckgo_search.py index 15c0469..ed868e4 100644 --- a/duckduckgo_search/duckduckgo_search.py +++ b/duckduckgo_search/duckduckgo_search.py @@ -1,6 +1,7 @@ from __future__ import annotations import logging +import os import warnings from concurrent.futures import ThreadPoolExecutor from datetime import datetime, timezone @@ -42,13 +43,13 @@ class DDGS: _executor: ThreadPoolExecutor = ThreadPoolExecutor() _impersonates = ( - "chrome_100", "chrome_101", "chrome_104", "chrome_105", "chrome_106", "chrome_107", "chrome_108", - "chrome_109", "chrome_114", "chrome_116", "chrome_117", "chrome_118", "chrome_119", "chrome_120", + "chrome_100", "chrome_101", "chrome_104", "chrome_105", "chrome_106", "chrome_107", "chrome_108", + "chrome_109", "chrome_114", "chrome_116", "chrome_117", "chrome_118", "chrome_119", "chrome_120", "chrome_123", "chrome_124", "chrome_126", "chrome_127", "chrome_128", "chrome_129", "chrome_130", "chrome_131", "safari_ios_16.5", "safari_ios_17.2", "safari_ios_17.4.1", - "safari_15.3", "safari_15.5", "safari_15.6.1", "safari_16", "safari_16.5", - "safari_17.0", "safari_17.2.1", "safari_17.4.1", "safari_17.5", "safari_18", + "safari_15.3", "safari_15.5", "safari_15.6.1", "safari_16", "safari_16.5", + "safari_17.0", "safari_17.2.1", "safari_17.4.1", "safari_17.5", "safari_18", "safari_ipad_18", "edge_101", "edge_122", "edge_127", ) # fmt: skip @@ -70,7 +71,8 @@ def __init__( timeout (int, optional): Timeout value for the HTTP client. Defaults to 10. verify (bool): SSL verification when making the request. Defaults to True. """ - self.proxy: str | None = _expand_proxy_tb_alias(proxy) # replaces "tb" with "socks5://127.0.0.1:9150" + ddgs_proxy: str | None = os.environ.get("DDGS_PROXY") + self.proxy: str | None = ddgs_proxy if ddgs_proxy else _expand_proxy_tb_alias(proxy) assert self.proxy is None or isinstance(self.proxy, str), "proxy must be a str" if not proxy and proxies: warnings.warn("'proxies' is deprecated, use 'proxy' instead.", stacklevel=1)