Skip to content

Commit ddf1d92

Browse files
committed
Cleanup logs and change ret type
1 parent a59c3cb commit ddf1d92

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

duckduckgo/query.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ async def query(q: str,
6464
encparams = urllib.parse.urlencode(params)
6565
url = f'http://api.duckduckgo.com/?{encparams}'
6666

67+
logger.debug(f"Full request url: {url}")
6768
async with aiohttp.ClientSession() as cs:
6869
async with cs.get(url, headers={'User-Agent': useragent}) as req:
6970
response = await req.json(content_type='application/x-javascript')
@@ -117,14 +118,14 @@ async def zci(q: str,
117118
break
118119

119120
# If there still isn't anything, try to get the first web result
120-
logger.debug("Trying web fallback...")
121121
if not response and web_fallback:
122+
logger.debug("Trying web fallback...")
122123
if ddg.redirect.url:
123124
response = ddg.redirect.url
124125

125126
# Final fallback
126-
logger.info("No results!")
127127
if not response:
128+
logger.info("No results!")
128129
response = 'Sorry, no results.'
129130

130131
logger.debug(f"Final response: {response!r}")
@@ -134,12 +135,12 @@ async def zci_extra(q: str,
134135
web_fallback: bool = True,
135136
priority: Tuple[str] = DEFAULT_PRIORITIES,
136137
urls: bool = True,
137-
**kwargs) -> Tuple[str, bool, bool]:
138+
**kwargs) -> Tuple[str, bool, str]:
138139
'''A helper method to get a single (and hopefully the best) ZCI result.
139140
priority=list can be used to set the order in which fields will be checked for answers.
140141
Use web_fallback=True to fall back to grabbing the first web result.
141142
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
143144
allows to determine how the result was retrieved.'''
144145

145146
logger.info(f"Performing DDG ZCI: '{q}'")
@@ -149,7 +150,7 @@ async def zci_extra(q: str,
149150
ddg = await query(f'\\{q}', **kwargs)
150151
response = ''
151152
found = True
152-
is_fallback = False
153+
result_type = ''
153154

154155
for p in priority:
155156
ps = p.split('.')
@@ -177,19 +178,18 @@ async def zci_extra(q: str,
177178
break
178179

179180
# If there still isn't anything, try to get the first web result
180-
logger.debug("Trying web fallback...")
181181
if not response and web_fallback:
182+
logger.debug("Trying web fallback...")
182183
if ddg.redirect.url:
183-
is_fallback = True
184184
found = True
185+
result_type = ddg.type
185186
response = ddg.redirect.url
186187

187188
# Final fallback
188-
logger.info("No results!")
189189
if not response:
190+
logger.info("No results!")
190191
found = False
191-
is_fallback = True
192192
response = 'Sorry, no results.'
193193

194194
logger.debug(f"Final response: {response!r}")
195-
return (response, found, is_fallback)
195+
return (response, found, result_type)

0 commit comments

Comments
 (0)