searxng/searx/engines/twitter.py

31 lines
952 B
Python
Raw Normal View History

2013-10-20 08:40:02 +00:00
from urlparse import urljoin
from urllib import urlencode
from lxml import html
2013-11-09 17:39:20 +00:00
from cgi import escape
2013-10-20 08:40:02 +00:00
categories = ['social media']
base_url = 'https://twitter.com/'
search_url = base_url+'search?'
2014-01-20 01:31:20 +00:00
title_xpath = './/span[@class="username js-action-profile-name"]//text()'
content_xpath = './/p[@class="js-tweet-text tweet-text"]//text()'
2013-10-20 08:40:02 +00:00
def request(query, params):
params['url'] = search_url + urlencode({'q': query})
return params
def response(resp):
results = []
dom = html.fromstring(resp.text)
for tweet in dom.xpath('//li[@data-item-type="tweet"]'):
link = tweet.xpath('.//small[@class="time"]//a')[0]
url = urljoin(base_url, link.attrib.get('href'))
2014-01-20 01:31:20 +00:00
title = ''.join(tweet.xpath(title_xpath))
content = escape(''.join(tweet.xpath(content_xpath)))
results.append({'url': url,
'title': title,
'content': content})
2013-10-20 08:40:02 +00:00
return results