2010-12-29 12:06:14 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
import re
|
|
|
|
import urllib
|
|
|
|
import ox
|
|
|
|
from ox import stripTags, decodeHtml
|
|
|
|
from ox.utils import json
|
2010-12-31 07:23:24 +00:00
|
|
|
from ox.cache import readUrlUnicode
|
2010-12-29 12:06:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def find(query, timeout=ox.cache.cache_timeout):
|
2010-12-31 07:23:24 +00:00
|
|
|
if isinstance(query, unicode):
|
|
|
|
query = query.encode('utf-8')
|
2010-12-31 07:32:22 +00:00
|
|
|
params = urllib.urlencode({'q': query})
|
2010-12-29 12:06:14 +00:00
|
|
|
url = 'http://duckduckgo.com/html/?' + params
|
2010-12-31 07:23:24 +00:00
|
|
|
data = readUrlUnicode(url, timeout=timeout)
|
2010-12-29 12:06:14 +00:00
|
|
|
results = []
|
|
|
|
regex = '<a .*?class="l le" href="(.+?)">(.*?)</a>.*?<div class="cra">(.*?)</div>'
|
|
|
|
for r in re.compile(regex, re.DOTALL).findall(data):
|
|
|
|
results.append((stripTags(decodeHtml(r[1])), r[0], stripTags(decodeHtml(r[2]))))
|
|
|
|
return results
|
|
|
|
|