2010-07-07 23:25:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
import re
|
2023-07-27 11:07:13 +00:00
|
|
|
import urllib
|
2010-07-07 23:25:57 +00:00
|
|
|
|
|
|
|
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):
|
2014-09-30 19:04:46 +00:00
|
|
|
if not isinstance(s, bytes):
|
2010-07-28 13:04:43 +00:00
|
|
|
s = s.encode('utf-8')
|
2014-09-30 19:04:46 +00:00
|
|
|
return urllib.parse.quote_plus(s)
|
2010-07-07 23:25:57 +00:00
|
|
|
|
2021-08-29 11:43:33 +00:00
|
|
|
|
|
|
|
def infobox(query, timeout=DEFAULT_TIMEOUT):
|
|
|
|
import lxml.html
|
|
|
|
data = read_url(url, timeout=timeout)
|
|
|
|
doc = lxml.html.document_fromstring(data)
|
|
|
|
k = 'kp-wholepage'
|
|
|
|
wholepage = doc.cssselect('.' + k)
|
|
|
|
infobox = {}
|
|
|
|
if wholepage:
|
|
|
|
page = wholepage[0]
|
|
|
|
for a in page.cssselect('a'):
|
|
|
|
if a.attrib.get('href', '').startswith('http'):
|
|
|
|
domain = '.'.join(a.attrib['href'].split('/')[2].split('.')[-2:])
|
|
|
|
infobox[domain] = a.attrib['href']
|
|
|
|
for e in page.cssselect('*[data-attrid]'):
|
|
|
|
key = e.attrib['data-attrid']
|
|
|
|
value = e.text_content()
|
|
|
|
if value and key not in (
|
|
|
|
'kc:/film/film:media_actions_wholepage',
|
|
|
|
'action:watch_film'
|
|
|
|
):
|
|
|
|
infobox[key] = value
|
|
|
|
return infobox
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-05-21 13:19:25 +00:00
|
|
|
>>> str(find("The Matrix site:imdb.com", 1)[0][0])
|
|
|
|
'The Matrix (1999) - IMDb'
|
2010-07-28 13:04:43 +00:00
|
|
|
|
2016-05-21 13:19:25 +00:00
|
|
|
>>> str(find("The Matrix site:imdb.com", 1)[0][1])
|
|
|
|
'http://www.imdb.com/title/tt0133093/'
|
2010-07-28 13:04:43 +00:00
|
|
|
"""
|
2010-07-07 23:25:57 +00:00
|
|
|
results = []
|
2013-09-08 13:56:57 +00:00
|
|
|
offset = 0
|
|
|
|
while len(results) < max_results:
|
|
|
|
url = 'http://google.com/search?q=%s' % quote_plus(query)
|
|
|
|
if offset:
|
|
|
|
url += '&start=%d' % offset
|
|
|
|
data = read_url(url, timeout=timeout)
|
|
|
|
data = re.sub('<span class="f">(.*?)</span>', '\\1', data)
|
2024-08-30 11:30:47 +00:00
|
|
|
for a in re.compile(r'<a href="(htt\S+?)".*?>(.*?)</a>.*?<span class="st">(.*?)<\/span>').findall(data):
|
2013-09-08 13:56:57 +00:00
|
|
|
results.append((strip_tags(decode_html(a[1])), a[0], strip_tags(decode_html(a[2]))))
|
|
|
|
if len(results) >= max_results:
|
|
|
|
break
|
|
|
|
offset += 10
|
2010-07-07 23:25:57 +00:00
|
|
|
return results
|
|
|
|
|