on master help sign in
Digg + Twitter corrections

Digg engines, with thumbnails Add pubdate for twitter

commite7e298153678fc0e77e24a3ae3b333b1230136b2 authorCqoicebordel <Cqoicebordel@users.noreply.github.com> dateSun 28 Dec 2014, 22:57 (12 years ago) parent011c43b
3 files changed+86−6
searx/engines/digg.py+66 searx/engines/twitter.py+16−6 searx/settings.yml+4
searx/engines/digg.pynew file+66
@@ -0,0 +1,66 @@
1+## Digg (News, Social media)
2+#
3+# @website https://digg.com/
4+# @provide-api no
5+#
6+# @using-api no
7+# @results HTML (using search portal)
8+# @stable no (HTML can change)
9+# @parse url, title, content, publishedDate, thumbnail
10+
11+from urllib import quote_plus
12+from json import loads
13+from lxml import html
14+from cgi import escape
15+from dateutil import parser
16+
17+# engine dependent config
18+categories = ['news', 'social media']
19+paging = True
20+
21+# search-url
22+base_url = 'https://digg.com/'
23+search_url = base_url+'api/search/{query}.json?position={position}&format=html'
24+
25+# specific xpath variables
26+results_xpath = '//article'
27+link_xpath = './/small[@class="time"]//a'
28+title_xpath = './/h2//a//text()'
29+content_xpath = './/p//text()'
30+pubdate_xpath = './/time'
31+
32+
33+# do search-request
34+def request(query, params):
35+ offset = (params['pageno'] - 1) * 10
36+ params['url'] = search_url.format(position=offset,
37+ query=quote_plus(query))
38+ return params
39+
40+
41+# get response from search-request
42+def response(resp):
43+ results = []
44+
45+ search_result = loads(resp.text)
46+
47+ dom = html.fromstring(search_result['html'])
48+
49+ # parse results
50+ for result in dom.xpath(results_xpath):
51+ url = result.attrib.get('data-contenturl')
52+ thumbnail = result.xpath('.//img')[0].attrib.get('src')
53+ title = ''.join(result.xpath(title_xpath))
54+ content = escape(''.join(result.xpath(content_xpath)))
55+ publishedDate = parser.parse(result.xpath(pubdate_xpath)[0].attrib.get('datetime'))
56+
57+ # append result
58+ results.append({'url': url,
59+ 'title': title,
60+ 'content': content,
61+ 'template': 'videos.html',
62+ 'publishedDate': publishedDate,
63+ 'thumbnail': thumbnail})
64+
65+ # return results
66+ return results
searx/engines/twitter.py+16−6
@@ -1,6 +1,6 @@
11## Twitter (Social media)
22#
3−# @website https://www.bing.com/news
3+# @website https://twitter.com/
44# @provide-api yes (https://dev.twitter.com/docs/using-search)
55#
66# @using-api no
@@ -14,6 +14,7 @@ from urlparse import urljoin
1414from urllib import urlencode
1515from lxml import html
1616from cgi import escape
17+from datetime import datetime
1718
1819# engine dependent config
1920categories = ['social media']
@@ -28,6 +29,7 @@ results_xpath = '//li[@data-item-type="tweet"]'
2829link_xpath = './/small[@class="time"]//a'
2930title_xpath = './/span[@class="username js-action-profile-name"]//text()'
3031content_xpath = './/p[@class="js-tweet-text tweet-text"]//text()'
32+timestamp_xpath = './/span[contains(@class,"_timestamp")]'
3133
3234
3335# do search-request
@@ -53,11 +55,19 @@ def response(resp):
5355 url = urljoin(base_url, link.attrib.get('href'))
5456 title = ''.join(tweet.xpath(title_xpath))
5557 content = escape(''.join(tweet.xpath(content_xpath)))
56−
57− # append result
58− results.append({'url': url,
59− 'title': title,
60− 'content': content})
58+ pubdate = tweet.xpath(timestamp_xpath)
59+ if len(pubdate) > 0:
60+ publishedDate = datetime.fromtimestamp(float(pubdate[0].attrib.get('data-time')), None)
61+ # append result
62+ results.append({'url': url,
63+ 'title': title,
64+ 'content': content,
65+ 'publishedDate': publishedDate})
66+ else:
67+ # append result
68+ results.append({'url': url,
69+ 'title': title,
70+ 'content': content})
6171
6272 # return results
6373 return results
searx/settings.yml+4
@@ -44,6 +44,10 @@ engines:
4444 - name : ddg definitions
4545 engine : duckduckgo_definitions
4646 shortcut : ddd
47+
48+ - name : digg
49+ engine : digg
50+ shortcut : dg
4751
4852 - name : wikidata
4953 engine : wikidata