1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/searx_ynh.git synced 2024-09-03 20:16:30 +02:00
searx_ynh/sources/searx/engines/twitter.py

84 lines
2.3 KiB
Python
Raw Normal View History

2015-09-08 23:05:37 +02:00
"""
Twitter (Social media)
@website https://twitter.com/
@provide-api yes (https://dev.twitter.com/docs/using-search)
@using-api no
@results HTML (using search portal)
@stable no (HTML can change)
@parse url, title, content
@todo publishedDate
"""
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
from urlparse import urljoin
from urllib import urlencode
from lxml import html
2015-01-13 17:13:08 +01:00
from datetime import datetime
2015-02-17 12:45:54 +01:00
from searx.engines.xpath import extract_text
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# engine dependent config
2014-05-10 11:56:19 +02:00
categories = ['social media']
2014-12-01 12:26:38 +01:00
language_support = True
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# search-url
2014-05-10 11:56:19 +02:00
base_url = 'https://twitter.com/'
2015-02-17 12:45:54 +01:00
search_url = base_url + 'search?'
2014-12-01 12:26:38 +01:00
# specific xpath variables
results_xpath = '//li[@data-item-type="tweet"]'
link_xpath = './/small[@class="time"]//a'
2015-09-08 23:05:37 +02:00
title_xpath = './/span[contains(@class, "username")]'
content_xpath = './/p[contains(@class, "tweet-text")]'
2015-01-13 17:13:08 +01:00
timestamp_xpath = './/span[contains(@class,"_timestamp")]'
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# do search-request
2014-05-10 11:56:19 +02:00
def request(query, params):
params['url'] = search_url + urlencode({'q': query})
2014-12-01 12:26:38 +01:00
# set language if specified
if params['language'] != 'all':
params['cookies']['lang'] = params['language'].split('_')[0]
2015-02-17 12:45:54 +01:00
else:
params['cookies']['lang'] = 'en'
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
return params
2014-12-01 12:26:38 +01:00
# get response from search-request
2014-05-10 11:56:19 +02:00
def response(resp):
results = []
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
dom = html.fromstring(resp.text)
2014-12-01 12:26:38 +01:00
# parse results
for tweet in dom.xpath(results_xpath):
2015-09-08 23:05:37 +02:00
try:
link = tweet.xpath(link_xpath)[0]
content = extract_text(tweet.xpath(content_xpath)[0])
except Exception:
continue
2014-05-10 11:56:19 +02:00
url = urljoin(base_url, link.attrib.get('href'))
2015-02-17 12:45:54 +01:00
title = extract_text(tweet.xpath(title_xpath))
2015-01-13 17:13:08 +01:00
pubdate = tweet.xpath(timestamp_xpath)
if len(pubdate) > 0:
timestamp = float(pubdate[0].attrib.get('data-time'))
publishedDate = datetime.fromtimestamp(timestamp, 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-12-01 12:26:38 +01:00
# return results
2014-05-10 11:56:19 +02:00
return results