2010-07-07 23:25:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
import re
|
|
|
|
import urllib
|
|
|
|
|
|
|
|
import ox
|
2012-08-14 14:12:43 +00:00
|
|
|
from ox import strip_tags, decode_html
|
2010-07-07 23:25:57 +00:00
|
|
|
|
|
|
|
DEFAULT_MAX_RESULTS = 10
|
|
|
|
DEFAULT_TIMEOUT = 24*60*60
|
|
|
|
|
2012-08-14 13:58:05 +00:00
|
|
|
def read_url(url, data=None, headers=ox.net.DEFAULT_HEADERS, timeout=DEFAULT_TIMEOUT):
|
|
|
|
return ox.cache.read_url(url, data, headers, timeout, unicode=True)
|
2010-07-07 23:25:57 +00:00
|
|
|
|
|
|
|
def quote_plus(s):
|
2010-07-28 13:04:43 +00:00
|
|
|
if not isinstance(s, str):
|
|
|
|
s = s.encode('utf-8')
|
|
|
|
return urllib.quote_plus(s)
|
2010-07-07 23:25:57 +00:00
|
|
|
|
|
|
|
def find(query, max_results=DEFAULT_MAX_RESULTS, timeout=DEFAULT_TIMEOUT):
|
2010-07-28 13:04:43 +00:00
|
|
|
"""
|
|
|
|
Return max_results tuples with title, url, description
|
|
|
|
|
|
|
|
>>> find("The Matrix site:imdb.com", 1)[0][0]
|
2011-11-28 13:24:21 +00:00
|
|
|
u'The Matrix (1999) - IMDb'
|
2010-07-28 13:04:43 +00:00
|
|
|
|
|
|
|
>>> find("The Matrix site:imdb.com", 1)[0][1]
|
2011-11-28 13:24:21 +00:00
|
|
|
u'http://www.imdb.com/title/tt0133093/'
|
2010-07-28 13:04:43 +00:00
|
|
|
"""
|
2011-11-28 13:24:21 +00:00
|
|
|
url = 'http://google.com/search?q=%s' % quote_plus(query)
|
2012-08-14 13:58:05 +00:00
|
|
|
data = read_url(url, timeout=timeout)
|
2010-07-07 23:25:57 +00:00
|
|
|
results = []
|
2012-03-18 14:38:51 +00:00
|
|
|
data = re.sub('<span class="f">(.*?)</span>', '\\1', data)
|
2013-05-31 20:05:25 +00:00
|
|
|
for a in re.compile('<a href="(htt\S+?)".*?>(.*?)</a>.*?<span class="st">(.*?)<\/span>').findall(data):
|
2012-08-14 14:12:43 +00:00
|
|
|
results.append((strip_tags(decode_html(a[1])), a[0], strip_tags(decode_html(a[2]))))
|
2010-07-28 13:04:43 +00:00
|
|
|
if len(results) >= max_results:
|
|
|
|
break
|
2010-07-07 23:25:57 +00:00
|
|
|
return results
|
|
|
|
|