on master help sign in
showing publishedDate for news
commitb88146d669b1196ed1efc4ae4108e238cfd7dbca authorThomas Pointhuber <thomas.pointhuber@gmx.at> dateFri 14 Mar 2014, 09:55 (13 years ago) parenta559bad
4 files changed+28−1
searx/engines/google_news.py+9 searx/engines/yahoo_news.py+6−1 searx/templates/result_templates/default.html+1 searx/webapp.py+12
searx/engines/google_news.py+9
@@ -2,6 +2,7 @@
22
33from urllib import urlencode
44from json import loads
5+from datetime import datetime, timedelta
56
67categories = ['news']
78
@@ -31,7 +32,15 @@ def response(resp):
3132 return []
3233
3334 for result in search_res['responseData']['results']:
35+# S.149 (159), library.pdf
36+# datetime.strptime("Mon, 10 Mar 2014 16:26:15 -0700", "%a, %d %b %Y %H:%M:%S %z")
37+# publishedDate = parse(result['publishedDate'])
38+ publishedDate = datetime.strptime(str.join(' ',result['publishedDate'].split(None)[0:5]), "%a, %d %b %Y %H:%M:%S")
39+ #utc_offset = timedelta(result['publishedDate'].split(None)[5]) # local = utc + offset
40+ #publishedDate = publishedDate + utc_offset
41+
3442 results.append({'url': result['unescapedUrl'],
3543 'title': result['titleNoFormatting'],
44+ 'publishedDate': publishedDate,
3645 'content': result['content']})
3746 return results
searx/engines/yahoo_news.py+6−1
@@ -4,6 +4,7 @@ from urllib import urlencode
44from lxml import html
55from searx.engines.xpath import extract_text, extract_url
66from searx.engines.yahoo import parse_url
7+from datetime import datetime
78
89categories = ['news']
910search_url = 'http://news.search.yahoo.com/search?{query}&b={offset}'
@@ -11,6 +12,7 @@ results_xpath = '//div[@class="res"]'
1112url_xpath = './/h3/a/@href'
1213title_xpath = './/h3/a'
1314content_xpath = './/div[@class="abstr"]'
15+publishedDate_xpath = './/span[@class="timestamp"]'
1416suggestion_xpath = '//div[@id="satat"]//a'
1517
1618paging = True
@@ -37,7 +39,10 @@ def response(resp):
3739 url = parse_url(extract_url(result.xpath(url_xpath), search_url))
3840 title = extract_text(result.xpath(title_xpath)[0])
3941 content = extract_text(result.xpath(content_xpath)[0])
40− results.append({'url': url, 'title': title, 'content': content})
42+# Feb 20 04:02am
43+ publishedDate = datetime.strptime(extract_text(result.xpath(publishedDate_xpath)[0]),"%b %d %H:%M%p")
44+ #publishedDate.replace(year=2014)
45+ results.append({'url': url, 'title': title, 'content': content,'publishedDate':publishedDate})
4146
4247 if not suggestion_xpath:
4348 return results
searx/templates/result_templates/default.html+1
@@ -6,6 +6,7 @@
66
77 <div>
88 <h3 class="result_title"><a href="{{ result.url }}">{{ result.title|safe }}</a></h3>
9+ {% if result.publishedDate %}<p class="published_date">{{ result.publishedDate }}</p>{% endif %}
910 <p class="content">{% if result.content %}{{ result.content|safe }}<br />{% endif %}</p>
1011 <p class="url">{{ result.pretty_url }}</p>
1112 </div>
searx/webapp.py+12
@@ -26,6 +26,7 @@ import json
2626import cStringIO
2727import os
2828
29+from datetime import datetime, timedelta
2930from itertools import chain
3031from flask import (
3132 Flask, request, render_template, url_for, Response, make_response,
@@ -156,6 +157,17 @@ def index():
156157 if engine in favicons:
157158 result['favicon'] = engine
158159
160+ # TODO, check if timezone is calculated right
161+ if 'publishedDate' in result:
162+ if result['publishedDate'].date() == datetime.now().date():
163+ timedifference = datetime.now()-result['publishedDate']
164+ if timedifference.seconds < 60*60:
165+ result['publishedDate'] = '{0:d} minutes ago'.format(timedifference.seconds/60)
166+ else:
167+ result['publishedDate'] = '{0:d} hours ago'.format(timedifference.seconds/60/60)
168+ else:
169+ result['publishedDate'] = result['publishedDate'].strftime('%d.%m.%Y')
170+
159171 if search.request_data.get('format') == 'json':
160172 return Response(json.dumps({'query': search.query,
161173 'results': search.results}),