@@ -64,6 +64,7 @@ async def query(q: str,
64
64
encparams = urllib .parse .urlencode (params )
65
65
url = f'http://api.duckduckgo.com/?{ encparams } '
66
66
67
+ logger .debug (f"Full request url: { url } " )
67
68
async with aiohttp .ClientSession () as cs :
68
69
async with cs .get (url , headers = {'User-Agent' : useragent }) as req :
69
70
response = await req .json (content_type = 'application/x-javascript' )
@@ -117,14 +118,14 @@ async def zci(q: str,
117
118
break
118
119
119
120
# If there still isn't anything, try to get the first web result
120
- logger .debug ("Trying web fallback..." )
121
121
if not response and web_fallback :
122
+ logger .debug ("Trying web fallback..." )
122
123
if ddg .redirect .url :
123
124
response = ddg .redirect .url
124
125
125
126
# Final fallback
126
- logger .info ("No results!" )
127
127
if not response :
128
+ logger .info ("No results!" )
128
129
response = 'Sorry, no results.'
129
130
130
131
logger .debug (f"Final response: { response !r} " )
@@ -134,12 +135,12 @@ async def zci_extra(q: str,
134
135
web_fallback : bool = True ,
135
136
priority : Tuple [str ] = DEFAULT_PRIORITIES ,
136
137
urls : bool = True ,
137
- ** kwargs ) -> Tuple [str , bool , bool ]:
138
+ ** kwargs ) -> Tuple [str , bool , str ]:
138
139
'''A helper method to get a single (and hopefully the best) ZCI result.
139
140
priority=list can be used to set the order in which fields will be checked for answers.
140
141
Use web_fallback=True to fall back to grabbing the first web result.
141
142
passed to query. This method will fall back to 'Sorry, no results.'
142
- if it cannot find anything. Returns a tuple with [result, result_found, is_fallback ] which
143
+ if it cannot find anything. Returns a tuple with [result, result_found, result_type ] which
143
144
allows to determine how the result was retrieved.'''
144
145
145
146
logger .info (f"Performing DDG ZCI: '{ q } '" )
@@ -149,7 +150,7 @@ async def zci_extra(q: str,
149
150
ddg = await query (f'\\ { q } ' , ** kwargs )
150
151
response = ''
151
152
found = True
152
- is_fallback = False
153
+ result_type = ''
153
154
154
155
for p in priority :
155
156
ps = p .split ('.' )
@@ -177,19 +178,18 @@ async def zci_extra(q: str,
177
178
break
178
179
179
180
# If there still isn't anything, try to get the first web result
180
- logger .debug ("Trying web fallback..." )
181
181
if not response and web_fallback :
182
+ logger .debug ("Trying web fallback..." )
182
183
if ddg .redirect .url :
183
- is_fallback = True
184
184
found = True
185
+ result_type = ddg .type
185
186
response = ddg .redirect .url
186
187
187
188
# Final fallback
188
- logger .info ("No results!" )
189
189
if not response :
190
+ logger .info ("No results!" )
190
191
found = False
191
- is_fallback = True
192
192
response = 'Sorry, no results.'
193
193
194
194
logger .debug (f"Final response: { response !r} " )
195
- return (response , found , is_fallback )
195
+ return (response , found , result_type )
0 commit comments