Skip to content

Commit

Permalink
use env var DDGS_PROXY
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Dec 18, 2024
1 parent 763fc93 commit 237fc12
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -212,6 +212,10 @@ results = ddgs.text("something you need", max_results=50)
ddgs = DDGS(proxy="socks5h://user:[email protected]: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:[email protected]:32325"
```

[Go To TOP](#TOP)

Expand All @@ -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
Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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".
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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".
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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.
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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.
"""
Expand Down
12 changes: 7 additions & 5 deletions duckduckgo_search/duckduckgo_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import logging
import os
import warnings
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 237fc12

Please sign in to comment.