Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py2.7/3 compliant #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions duckduckgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
#
# See LICENSE for terms of usage, modification and redistribution.

import urllib
import urllib2
try:
import urllib.parse as urllib
except ImportError:
import urllib
try:
import urllib.request as urllib2
except ImportError:
import urllib2

import json as j
import sys

Expand Down Expand Up @@ -52,7 +59,7 @@ def query(query, useragent='python-duckduckgo '+str(__version__), safesearch=Tru

request = urllib2.Request(url, headers={'User-Agent': useragent})
response = urllib2.urlopen(request)
json = j.loads(response.read())
json = j.loads(response.read().decode('utf-8'))
response.close()

return Results(json)
Expand Down Expand Up @@ -138,7 +145,7 @@ def get_zci(q, web_fallback=True, priority=['answer', 'abstract', 'related.0', '
'''A helper method to get a single (and hopefully the best) ZCI result.
priority=list can be used to set the order in which fields will be checked for answers.
Use web_fallback=True to fall back to grabbing the first web result.
passed to query. This method will fall back to 'Sorry, no results.'
passed to query. This method will fall back to 'Sorry, no results.'
if it cannot find anything.'''

ddg = query('\\'+q, **kwargs)
Expand All @@ -150,13 +157,13 @@ def get_zci(q, web_fallback=True, priority=['answer', 'abstract', 'related.0', '
index = int(ps[1]) if len(ps) > 1 else None

result = getattr(ddg, type)
if index is not None:
if index is not None:
if not hasattr(result, '__getitem__'): raise TypeError('%s field is not indexable' % type)
result = result[index] if len(result) > index else None
if not result: continue

if result.text: response = result.text
if result.text and hasattr(result,'url') and urls:
if result.text and hasattr(result,'url') and urls:
if result.url: response += ' (%s)' % result.url
if response: break

Expand All @@ -166,7 +173,7 @@ def get_zci(q, web_fallback=True, priority=['answer', 'abstract', 'related.0', '
response = ddg.redirect.url

# final fallback
if not response:
if not response:
response = 'Sorry, no results.'

return response
Expand All @@ -179,7 +186,7 @@ def main():
for key in keys:
sys.stdout.write(key)
if type(q.json[key]) in [str,unicode,int]: print(':', q.json[key])
else:
else:
sys.stdout.write('\n')
for i in q.json[key]: print('\t',i)
else:
Expand Down