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

Functions

 _cache_key (dict data)
 
 cache_vqd (dict data, value)
 
 get_vqd (data)
 
 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'}
 
list __CACHE = []
 
dict ddg_reg_map
 

Detailed Description

DuckDuckGo Lite
~~~~~~~~~~~~~~~

Function Documentation

◆ _cache_key()

searx.engines.duckduckgo._cache_key ( dict data)
protected

Definition at line 63 of file duckduckgo.py.

63def _cache_key(data: dict):
64 return 'SearXNG_ddg_web_vqd' + redislib.secret_hash(f"{data['q']}//{data['kl']}")
65
66

Referenced by searx.engines.duckduckgo.cache_vqd(), and searx.engines.duckduckgo.get_vqd().

+ Here is the caller graph for this function:

◆ cache_vqd()

searx.engines.duckduckgo.cache_vqd ( dict data,
value )
Caches a ``vqd`` value from a query.

Definition at line 67 of file duckduckgo.py.

67def cache_vqd(data: dict, value):
68 """Caches a ``vqd`` value from a query."""
69 c = redisdb.client()
70 if c:
71 logger.debug("cache vqd value: %s", value)
72 c.set(_cache_key(data), value, ex=600)
73
74 else:
75 logger.debug("MEM cache vqd value: %s", value)
76 if len(__CACHE) > 100: # cache vqd from last 100 queries
77 __CACHE.pop(0)
78 __CACHE.append((_cache_key(data), value))
79
80

References searx.engines.duckduckgo._cache_key().

+ Here is the call 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 129 of file duckduckgo.py.

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

◆ get_vqd()

searx.engines.duckduckgo.get_vqd ( data)
Returns the ``vqd`` that fits to the *query* (``data`` from HTTP POST).

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:

    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).

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=...``

Definition at line 81 of file duckduckgo.py.

81def get_vqd(data):
82 """Returns the ``vqd`` that fits to the *query* (``data`` from HTTP POST).
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 A request with a wrong ``vqd`` value leads to DDG temporarily putting
92 SearXNG's IP on a block list.
93
94 Requests from IPs in this block list run into timeouts.
95
96 Not sure, but it seems the block list is a sliding window: to get my IP rid
97 from the bot list I had to cool down my IP for 1h (send no requests from
98 that IP to DDG).
99
100 TL;DR; the ``vqd`` value is needed to pass DDG's bot protection and is used
101 by all request to DDG:
102
103 - DuckDuckGo Lite: ``https://lite.duckduckgo.com/lite`` (POST form data)
104 - DuckDuckGo Web: ``https://links.duckduckgo.com/d.js?q=...&vqd=...``
105 - DuckDuckGo Images: ``https://duckduckgo.com/i.js??q=...&vqd=...``
106 - DuckDuckGo Videos: ``https://duckduckgo.com/v.js??q=...&vqd=...``
107 - DuckDuckGo News: ``https://duckduckgo.com/news.js??q=...&vqd=...``
108
109 """
110
111 key = _cache_key(data)
112 value = None
113 c = redisdb.client()
114 if c:
115 value = c.get(key)
116 if value or value == b'':
117 value = value.decode('utf-8')
118 logger.debug("re-use CACHED vqd value: %s", value)
119 return value
120
121 else:
122 for k, value in __CACHE:
123 if k == key:
124 logger.debug("MEM re-use CACHED vqd value: %s", value)
125 return value
126 return None
127
128

References searx.engines.duckduckgo._cache_key().

+ Here is the call graph for this function:

Variable Documentation

◆ __CACHE

list searx.engines.duckduckgo.__CACHE = []
private

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

◆ categories

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

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

◆ form_data

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

Definition at line 59 of file duckduckgo.py.

◆ logger

logging searx.engines.duckduckgo.logger .Logger

Definition at line 32 of file duckduckgo.py.

◆ paging

bool searx.engines.duckduckgo.paging = True

Definition at line 52 of file duckduckgo.py.

◆ safesearch

bool searx.engines.duckduckgo.safesearch = True

Definition at line 54 of file duckduckgo.py.

◆ send_accept_language_header

bool searx.engines.duckduckgo.send_accept_language_header = True

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

◆ time_range_support

bool searx.engines.duckduckgo.time_range_support = True

Definition at line 53 of file duckduckgo.py.

◆ url

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

Definition at line 56 of file duckduckgo.py.