2014-09-02 18:14:52 +00:00
|
|
|
## Twitter (Social media)
|
2014-12-07 15:37:56 +00:00
|
|
|
#
|
2014-12-28 21:57:59 +00:00
|
|
|
# @website https://twitter.com/
|
2014-09-02 18:14:52 +00:00
|
|
|
# @provide-api yes (https://dev.twitter.com/docs/using-search)
|
2014-12-07 15:37:56 +00:00
|
|
|
#
|
2014-09-02 18:14:52 +00:00
|
|
|
# @using-api no
|
|
|
|
# @results HTML (using search portal)
|
|
|
|
# @stable no (HTML can change)
|
|
|
|
# @parse url, title, content
|
|
|
|
#
|
|
|
|
# @todo publishedDate
|
|
|
|
|
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
|
2014-12-28 21:57:59 +00:00
|
|
|
from datetime import datetime
|
2013-10-20 08:40:02 +00:00
|
|
|
|
2014-09-02 18:14:52 +00:00
|
|
|
# engine dependent config
|
2013-10-20 08:40:02 +00:00
|
|
|
categories = ['social media']
|
2014-09-02 18:14:52 +00:00
|
|
|
language_support = True
|
2013-10-20 08:40:02 +00:00
|
|
|
|
2014-09-02 18:14:52 +00:00
|
|
|
# search-url
|
2013-10-20 08:40:02 +00:00
|
|
|
base_url = 'https://twitter.com/'
|
|
|
|
search_url = base_url+'search?'
|
2014-09-02 18:14:52 +00:00
|
|
|
|
|
|
|
# specific xpath variables
|
|
|
|
results_xpath = '//li[@data-item-type="tweet"]'
|
|
|
|
link_xpath = './/small[@class="time"]//a'
|
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()'
|
2014-12-28 21:57:59 +00:00
|
|
|
timestamp_xpath = './/span[contains(@class,"_timestamp")]'
|
2014-01-20 01:31:20 +00:00
|
|
|
|
2013-10-20 08:40:02 +00:00
|
|
|
|
2014-09-02 18:14:52 +00:00
|
|
|
# do search-request
|
2013-10-20 08:40:02 +00:00
|
|
|
def request(query, params):
|
|
|
|
params['url'] = search_url + urlencode({'q': query})
|
2014-09-02 18:14:52 +00:00
|
|
|
|
|
|
|
# set language if specified
|
|
|
|
if params['language'] != 'all':
|
|
|
|
params['cookies']['lang'] = params['language'].split('_')[0]
|
|
|
|
|
2013-10-20 08:40:02 +00:00
|
|
|
return params
|
|
|
|
|
|
|
|
|
2014-09-02 18:14:52 +00:00
|
|
|
# get response from search-request
|
2013-10-20 08:40:02 +00:00
|
|
|
def response(resp):
|
|
|
|
results = []
|
2014-09-02 18:14:52 +00:00
|
|
|
|
2013-10-20 08:40:02 +00:00
|
|
|
dom = html.fromstring(resp.text)
|
2014-09-02 18:14:52 +00:00
|
|
|
|
|
|
|
# parse results
|
|
|
|
for tweet in dom.xpath(results_xpath):
|
|
|
|
link = tweet.xpath(link_xpath)[0]
|
2013-10-20 08:40:02 +00:00
|
|
|
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)))
|
2014-12-28 21:57:59 +00:00
|
|
|
pubdate = tweet.xpath(timestamp_xpath)
|
|
|
|
if len(pubdate) > 0:
|
|
|
|
publishedDate = datetime.fromtimestamp(float(pubdate[0].attrib.get('data-time')), None)
|
|
|
|
# append result
|
|
|
|
results.append({'url': url,
|
|
|
|
'title': title,
|
|
|
|
'content': content,
|
|
|
|
'publishedDate': publishedDate})
|
|
|
|
else:
|
|
|
|
# append result
|
|
|
|
results.append({'url': url,
|
|
|
|
'title': title,
|
|
|
|
'content': content})
|
2014-09-02 18:14:52 +00:00
|
|
|
|
|
|
|
# return results
|
2013-10-20 08:40:02 +00:00
|
|
|
return results
|