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

Make this project with with python 3 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
55 changes: 39 additions & 16 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
python-duckduckgo
==================

A Python library for querying the Duck Duck Go API.
A Python library for querying the DuckDuckGo API.

Copyright Michael Stephens <[email protected]>, released under a BSD-style license.

Source: http://github.com/mikejs/python-duckduckgo
Source: http://github.com/crazedpsyc/python-duckduckgo
Original source: http://github.com/mikejs/python-duckduckgo (outdated)

This version has been forked from the original to handle some new features of the API, and switch from XML to JSON.

Installation
============
Expand All @@ -19,30 +22,50 @@ Usage
=====

>>> import duckduckgo
>>> r = duckduckgo.query('Duck Duck Go')
>>> r = duckduckgo.query('DuckDuckGo')
>>> r.type
'answer'
u'answer'
>>> r.results[0].text
'Official site'
u'Official site'
>>> r.results[0].url
'http://duckduckgo.com/'
u'http://duckduckgo.com/'
>>> r.abstract.url
'http://en.wikipedia.org/wiki/Duck_Duck_Go'
u'http://en.wikipedia.org/wiki/Duck_Duck_Go'
>>> r.abstract.source
'Wikipedia'
u'Wikipedia'

>>> r = duckduckgo.query('Python')
>>> r.type
'disambiguation'
>>> r.related[6].text
'Python (programming language), a computer programming language'
>>> r.related[6].url
'http://duckduckgo.com/Python_(programming_language)'
u'disambiguation'
>>> r.related[1].text
u'Python (programming language), a computer programming language'
>>> r.related[1].url
u'http://duckduckgo.com/Python_(programming_language)'
>>> r.related[7].topics[0].text # weird, but this is how the DDG API is currently organized
u'Armstrong Siddeley Python, an early turboprop engine'


>>> r = duckduckgo.query('1 + 1')
>>> r.type
'nothing'
u'nothing'
>>> r.answer.text
'1 + 1 = 2'
u'1 + 1 = 2'
>>> r.answer.type
'calc'
u'calc'

>>> print duckduckgo.query('19301', kad='es_ES').answer.text
19301 es un código postal de Paoli, PA
>>> print duckduckgo.query('how to spell test', html=True).answer.text
<b>Test</b> appears to be spelled right!<br/><i>Suggestions: </i>test, testy, teat, tests, rest, yest.

The easiest method of quickly grabbing the best (hopefully) API result is to use duckduckgo.get_zci::
>>> print duckduckgo.get_zci('foo')
The terms foobar, fubar, or foo, bar, baz and qux are sometimes used as placeholder names in computer programming or computer-related documentation. (https://en.wikipedia.org/wiki/Foobar)
>>> print ddg.get_zci('foo fighters site')
http://www.foofighters.com/us/home

Special keyword args for query():
- useragent - string, The useragent used to make API calls. This is somewhat irrelevant, as they are not logged or used on DuckDuckGo, but it is retained for backwards compatibility.
- safesearch - boolean, enable or disable safesearch.
- html - boolean, Allow HTML in responses?

24 changes: 11 additions & 13 deletions duckduckgo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
import urllib
import urllib2
import urllib.request
import urllib.parse
from xml.etree import ElementTree

__version__ = 0.1
Expand All @@ -9,9 +9,7 @@
def query(query, useragent='python-duckduckgo 0.1'):
"""
Query Duck Duck Go, returning a Results object.

Here's a query that's unlikely to change:

>>> result = query('1 + 1')
>>> result.type
'nothing'
Expand All @@ -20,11 +18,11 @@ def query(query, useragent='python-duckduckgo 0.1'):
>>> result.answer.type
'calc'
"""
params = urllib.urlencode({'q': query, 'o': 'x'})
params = urllib.parse.urlencode({'q': query, 'o': 'x'})
url = 'http://duckduckgo.com/?' + params

request = urllib2.Request(url, headers={'User-Agent': useragent})
response = urllib2.urlopen(request)
request = urllib.request.Request(url, headers={'User-Agent': useragent})
response = urllib.request.urlopen(request)
xml = ElementTree.fromstring(response.read())
response.close()

Expand Down Expand Up @@ -131,14 +129,14 @@ def main():
try:
related = results.related[options.d - 1]
except IndexError:
print "Invalid disambiguation number."
print("Invalid disambiguation number.")
sys.exit(1)
results = query(related.url.split("/")[-1].replace("_", " "))

if results.answer and results.answer.text:
print "Answer: %s\n" % results.answer.text
print("Answer: %s\n" % results.answer.text)
elif results.abstract and results.abstract.text:
print "%s\n" % results.abstract.text
print("%s\n" % results.abstract.text)

if results.type == 'disambiguation':
print ("'%s' can mean multiple things. You can re-run your query "
Expand All @@ -150,14 +148,14 @@ def main():
summary = related.text
if len(summary) < len(related.text):
summary += "..."
print '%d. %s: %s\n' % (i + 1, name, summary)
print('%d. %s: %s\n' % (i + 1, name, summary))
else:
for i, result in enumerate(results.results[0:options.n]):
summary = result.text[0:70].replace("&nbsp;", " ")
if len(summary) < len(result.text):
summary += "..."
print "%d. %s" % (i + 1, summary)
print " <%s>\n" % result.url
print("%d. %s" % (i + 1, summary))
print(" <%s>\n" % result.url)


if __name__ == '__main__':
Expand Down