@@ -1,30 +1,63 @@
1+## Twitter (Social media)
3+# @website https://www.bing.com/news
4+# @provide-api yes (https://dev.twitter.com/docs/using-search)
7+# @results HTML (using search portal)
8+# @stable no (HTML can change)
9+# @parse url, title, content
1from urlparse import urljoin
13from urlparse import urljoin
2from urllib import urlencode
14from urllib import urlencode
3from lxml import html
15from lxml import html
4from cgi import escape
16from cgi import escape
18+# engine dependent config
6categories = ['social media']
19categories = ['social media']
20+language_support = True
8base_url = 'https://twitter.com/'
23base_url = 'https://twitter.com/'
9search_url = base_url+'search?'
24search_url = base_url+'search?'
26+# specific xpath variables
27+results_xpath = '//li[@data-item-type="tweet"]'
28+link_xpath = './/small[@class="time"]//a'
10title_xpath = './/span[@class="username js-action-profile-name"]//text()'
29title_xpath = './/span[@class="username js-action-profile-name"]//text()'
11content_xpath = './/p[@class="js-tweet-text tweet-text"]//text()'
30content_xpath = './/p[@class="js-tweet-text tweet-text"]//text()'
14def request(query, params):
34def request(query, params):
15 params['url'] = search_url + urlencode({'q': query})
35 params['url'] = search_url + urlencode({'q': query})
37+ # set language if specified
38+ if params['language'] != 'all':
39+ params['cookies']['lang'] = params['language'].split('_')[0]
16 return params
41 return params
44+# get response from search-request
19def response(resp):
45def response(resp):
20 results = []
46 results = []
21 dom = html.fromstring(resp.text)
48 dom = html.fromstring(resp.text)
22− for tweet in dom.xpath('//li[@data-item-type="tweet"]'):
51+ for tweet in dom.xpath(results_xpath):
23− link = tweet.xpath('.//small[@class="time"]//a')[0]
52+ link = tweet.xpath(link_xpath)[0]
24 url = urljoin(base_url, link.attrib.get('href'))
53 url = urljoin(base_url, link.attrib.get('href'))
25 title = ''.join(tweet.xpath(title_xpath))
54 title = ''.join(tweet.xpath(title_xpath))
26 content = escape(''.join(tweet.xpath(content_xpath)))
55 content = escape(''.join(tweet.xpath(content_xpath)))
27 results.append({'url': url,
58 results.append({'url': url,
28 'title': title,
59 'title': title,
29 'content': content})
60 'content': content})
30 return results
63 return results