.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.engines.duckduckgo Namespace Reference

Functions

 get_cache ()
str get_vqd (str query, str region, bool force_request=False)
 set_vqd (str query, str region, str value)
 get_ddg_lang (EngineTraits eng_traits, sxng_locale, default='en_US')

Variables

dict about
bool send_accept_language_header = True
list categories = ['general', 'web']
bool paging = True
bool time_range_support = True
bool safesearch = True
str url = "https://html.duckduckgo.com/html/"
dict time_range_dict = {'day': 'd', 'week': 'w', 'month': 'm', 'year': 'y'}
dict form_data = {'v': 'l', 'api': 'd.js', 'o': 'json'}
EngineCache _CACHE = None
dict ddg_reg_map

Detailed Description

DuckDuckGo WEB
~~~~~~~~~~~~~~

Function Documentation

◆ get_cache()

searx.engines.duckduckgo.get_cache ( )

Definition at line 61 of file duckduckgo.py.

61def get_cache():
62 global _CACHE # pylint: disable=global-statement
63 if _CACHE is None:
64 _CACHE = EngineCache("duckduckgo") # type:ignore
65 return _CACHE
66
67

Referenced by get_vqd(), and set_vqd().

Here is the caller graph for this function:

◆ get_ddg_lang()

searx.engines.duckduckgo.get_ddg_lang ( EngineTraits eng_traits,
sxng_locale,
default = 'en_US' )
Get DuckDuckGo's language identifier from SearXNG's locale.

DuckDuckGo defines its languages by region codes (see
:py:obj:`fetch_traits`).

To get region and language of a DDG service use:

.. code: python

   eng_region = traits.get_region(params['searxng_locale'], traits.all_locale)
   eng_lang = get_ddg_lang(traits, params['searxng_locale'])

It might confuse, but the ``l`` value of the cookie is what SearXNG calls
the *region*:

.. code:: python

    # !ddi paris :es-AR --> {'ad': 'es_AR', 'ah': 'ar-es', 'l': 'ar-es'}
    params['cookies']['ad'] = eng_lang
    params['cookies']['ah'] = eng_region
    params['cookies']['l'] = eng_region

.. hint::

   `DDG-lite <https://lite.duckduckgo.com/lite>`__ and the *no Javascript*
   page https://html.duckduckgo.com/html do not offer a language selection
   to the user, only a region can be selected by the user (``eng_region``
   from the example above).  DDG-lite and *no Javascript* store the selected
   region in a cookie::

     params['cookies']['kl'] = eng_region  # 'ar-es'

Definition at line 132 of file duckduckgo.py.

132def get_ddg_lang(eng_traits: EngineTraits, sxng_locale, default='en_US'):
133 """Get DuckDuckGo's language identifier from SearXNG's locale.
134
135 DuckDuckGo defines its languages by region codes (see
136 :py:obj:`fetch_traits`).
137
138 To get region and language of a DDG service use:
139
140 .. code: python
141
142 eng_region = traits.get_region(params['searxng_locale'], traits.all_locale)
143 eng_lang = get_ddg_lang(traits, params['searxng_locale'])
144
145 It might confuse, but the ``l`` value of the cookie is what SearXNG calls
146 the *region*:
147
148 .. code:: python
149
150 # !ddi paris :es-AR --> {'ad': 'es_AR', 'ah': 'ar-es', 'l': 'ar-es'}
151 params['cookies']['ad'] = eng_lang
152 params['cookies']['ah'] = eng_region
153 params['cookies']['l'] = eng_region
154
155 .. hint::
156
157 `DDG-lite <https://lite.duckduckgo.com/lite>`__ and the *no Javascript*
158 page https://html.duckduckgo.com/html do not offer a language selection
159 to the user, only a region can be selected by the user (``eng_region``
160 from the example above). DDG-lite and *no Javascript* store the selected
161 region in a cookie::
162
163 params['cookies']['kl'] = eng_region # 'ar-es'
164
165 """
166 return eng_traits.custom['lang_region'].get( # type: ignore
167 sxng_locale, eng_traits.get_language(sxng_locale, default)
168 )
169
170

◆ get_vqd()

str searx.engines.duckduckgo.get_vqd ( str query,
str region,
bool force_request = False )
Returns the ``vqd`` that fits to the *query*.

:param query: The query term
:param region: DDG's region code
:param force_request: force a request to get a vqd value from DDG

TL;DR; the ``vqd`` value is needed to pass DDG's bot protection and is used
by all request to DDG:

- DuckDuckGo Lite: ``https://lite.duckduckgo.com/lite`` (POST form data)
- DuckDuckGo Web: ``https://links.duckduckgo.com/d.js?q=...&vqd=...``
- DuckDuckGo Images: ``https://duckduckgo.com/i.js??q=...&vqd=...``
- DuckDuckGo Videos: ``https://duckduckgo.com/v.js??q=...&vqd=...``
- DuckDuckGo News: ``https://duckduckgo.com/news.js??q=...&vqd=...``

DDG's bot detection is sensitive to the ``vqd`` value.  For some search terms
(such as extremely long search terms that are often sent by bots), no ``vqd``
value can be determined.

If SearXNG cannot determine a ``vqd`` value, then no request should go out
to DDG.

.. attention::

   A request with a wrong ``vqd`` value leads to DDG temporarily putting
   SearXNG's IP on a block list.

Requests from IPs in this block list run into timeouts.  Not sure, but it
seems the block list is a sliding window: to get my IP rid from the bot list
I had to cool down my IP for 1h (send no requests from that IP to DDG).

Definition at line 68 of file duckduckgo.py.

68def get_vqd(query: str, region: str, force_request: bool = False) -> str:
69 """Returns the ``vqd`` that fits to the *query*.
70
71 :param query: The query term
72 :param region: DDG's region code
73 :param force_request: force a request to get a vqd value from DDG
74
75 TL;DR; the ``vqd`` value is needed to pass DDG's bot protection and is used
76 by all request to DDG:
77
78 - DuckDuckGo Lite: ``https://lite.duckduckgo.com/lite`` (POST form data)
79 - DuckDuckGo Web: ``https://links.duckduckgo.com/d.js?q=...&vqd=...``
80 - DuckDuckGo Images: ``https://duckduckgo.com/i.js??q=...&vqd=...``
81 - DuckDuckGo Videos: ``https://duckduckgo.com/v.js??q=...&vqd=...``
82 - DuckDuckGo News: ``https://duckduckgo.com/news.js??q=...&vqd=...``
83
84 DDG's bot detection is sensitive to the ``vqd`` value. For some search terms
85 (such as extremely long search terms that are often sent by bots), no ``vqd``
86 value can be determined.
87
88 If SearXNG cannot determine a ``vqd`` value, then no request should go out
89 to DDG.
90
91 .. attention::
92
93 A request with a wrong ``vqd`` value leads to DDG temporarily putting
94 SearXNG's IP on a block list.
95
96 Requests from IPs in this block list run into timeouts. Not sure, but it
97 seems the block list is a sliding window: to get my IP rid from the bot list
98 I had to cool down my IP for 1h (send no requests from that IP to DDG).
99 """
100 cache = get_cache()
101 key = cache.secret_hash(f"{query}//{region}")
102 value = cache.get(key=key)
103 if value is not None and not force_request:
104 logger.debug("vqd: re-use cached value: %s", value)
105 return value
106
107 logger.debug("vqd: request value from from duckduckgo.com")
108 resp = get(f'https://duckduckgo.com/?q={quote_plus(query)}')
109 if resp.status_code == 200: # type: ignore
110 value = extr(resp.text, 'vqd="', '"') # type: ignore
111 if value:
112 logger.debug("vqd value from duckduckgo.com request: '%s'", value)
113 else:
114 logger.error("vqd: can't parse value from ddg response (return empty string)")
115 return ""
116 else:
117 logger.error("vqd: got HTTP %s from duckduckgo.com", resp.status_code)
118
119 if value:
120 cache.set(key=key, value=value)
121 else:
122 logger.error("none vqd value from duckduckgo.com: HTTP %s", resp.status_code)
123 return value
124
125

References get_cache().

Here is the call graph for this function:

◆ set_vqd()

searx.engines.duckduckgo.set_vqd ( str query,
str region,
str value )

Definition at line 126 of file duckduckgo.py.

126def set_vqd(query: str, region: str, value: str):
127 cache = get_cache()
128 key = cache.secret_hash(f"{query}//{region}")
129 cache.set(key=key, value=value, expire=3600)
130
131

References get_cache().

Here is the call graph for this function:

Variable Documentation

◆ _CACHE

EngineCache searx.engines.duckduckgo._CACHE = None
protected

Definition at line 56 of file duckduckgo.py.

◆ about

dict searx.engines.duckduckgo.about
Initial value:
1= {
2 "website": 'https://lite.duckduckgo.com/lite/',
3 "wikidata_id": 'Q12805',
4 "use_official_api": False,
5 "require_api_key": False,
6 "results": 'HTML',
7}

Definition at line 31 of file duckduckgo.py.

◆ categories

list searx.engines.duckduckgo.categories = ['general', 'web']

Definition at line 46 of file duckduckgo.py.

◆ ddg_reg_map

dict searx.engines.duckduckgo.ddg_reg_map
Initial value:
1= {
2 'tw-tzh': 'zh_TW',
3 'hk-tzh': 'zh_HK',
4 'ct-ca': 'skip', # ct-ca and es-ca both map to ca_ES
5 'es-ca': 'ca_ES',
6 'id-en': 'id_ID',
7 'no-no': 'nb_NO',
8 'jp-jp': 'ja_JP',
9 'kr-kr': 'ko_KR',
10 'xa-ar': 'ar_SA',
11 'sl-sl': 'sl_SI',
12 'th-en': 'th_TH',
13 'vn-en': 'vi_VN',
14}

Definition at line 171 of file duckduckgo.py.

◆ form_data

dict searx.engines.duckduckgo.form_data = {'v': 'l', 'api': 'd.js', 'o': 'json'}

Definition at line 54 of file duckduckgo.py.

◆ paging

bool searx.engines.duckduckgo.paging = True

Definition at line 47 of file duckduckgo.py.

◆ safesearch

bool searx.engines.duckduckgo.safesearch = True

Definition at line 49 of file duckduckgo.py.

◆ send_accept_language_header

bool searx.engines.duckduckgo.send_accept_language_header = True

Definition at line 39 of file duckduckgo.py.

◆ time_range_dict

dict searx.engines.duckduckgo.time_range_dict = {'day': 'd', 'week': 'w', 'month': 'm', 'year': 'y'}

Definition at line 53 of file duckduckgo.py.

◆ time_range_support

bool searx.engines.duckduckgo.time_range_support = True

Definition at line 48 of file duckduckgo.py.

◆ url

str searx.engines.duckduckgo.url = "https://html.duckduckgo.com/html/"

Definition at line 51 of file duckduckgo.py.