summaryrefslogtreecommitdiff
path: root/searx/engines/twitter.py
blob: c05c20fc2ea66f17512da8787d8f782878c8d399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from urlparse import urljoin
from urllib import urlencode
from lxml import html
from cgi import escape

categories = ['social media']

base_url = 'https://twitter.com/'
search_url = base_url+'search?'
title_xpath = './/span[@class="username js-action-profile-name"]//text()'
content_xpath = './/p[@class="js-tweet-text tweet-text"]//text()'


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'))
        title = ''.join(tweet.xpath(title_xpath))
        content = escape(''.join(tweet.xpath(content_xpath)))
        results.append({'url': url,
                        'title': title,
                        'content': content})
    return results