diff --git a/README.rst b/README.rst index 690c1ac..b48227e 100644 --- a/README.rst +++ b/README.rst @@ -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 , 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 ============ @@ -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 + Test appears to be spelled right!
Suggestions: 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? + diff --git a/duckduckgo.py b/duckduckgo.py index 7ff2209..241e296 100755 --- a/duckduckgo.py +++ b/duckduckgo.py @@ -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 @@ -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' @@ -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() @@ -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 " @@ -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(" ", " ") 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__':