.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

logging logger .Logger
 
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 71 of file duckduckgo.py.

71def get_cache():
72 global _CACHE # pylint: disable=global-statement
73 if _CACHE is None:
74 _CACHE = EngineCache("duckduckgo") # type:ignore
75 return _CACHE
76
77

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 142 of file duckduckgo.py.

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

◆ 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 78 of file duckduckgo.py.

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

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 136 of file duckduckgo.py.

136def set_vqd(query: str, region: str, value: str):
137 cache = get_cache()
138 key = cache.secret_hash(f"{query}//{region}")
139 cache.set(key=key, value=value, expire=3600)
140
141

References get_cache().

+ Here is the call graph for this function:

Variable Documentation

◆ _CACHE

EngineCache searx.engines.duckduckgo._CACHE = None
protected

Definition at line 66 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 41 of file duckduckgo.py.

◆ categories

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

Definition at line 56 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 181 of file duckduckgo.py.

◆ form_data

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

Definition at line 64 of file duckduckgo.py.

◆ logger

logging searx.engines.duckduckgo.logger .Logger

Definition at line 37 of file duckduckgo.py.

◆ paging

bool searx.engines.duckduckgo.paging = True

Definition at line 57 of file duckduckgo.py.

◆ safesearch

bool searx.engines.duckduckgo.safesearch = True

Definition at line 59 of file duckduckgo.py.

◆ send_accept_language_header

bool searx.engines.duckduckgo.send_accept_language_header = True

Definition at line 49 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 63 of file duckduckgo.py.

◆ time_range_support

bool searx.engines.duckduckgo.time_range_support = True

Definition at line 58 of file duckduckgo.py.

◆ url

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

Definition at line 61 of file duckduckgo.py.