on master help sign in
Merge pull request #50 from pointhi/results

Showing publish Date of articles in search results

commit1467a2e0fc860546ec2490a21db369fa50355c2e authorAdam Tauber <asciimoo@gmail.com> dateSat 15 Mar 2014, 20:07 (13 years ago) parente5d64a5d20a3d6
8 files changed+62−2
searx/engines/google_news.py+9 searx/engines/yahoo_news.py+19−1 searx/static/css/style.css+1 searx/static/less/definitions.less+3 searx/static/less/style.less+6 searx/templates/result_templates/default.html+1 searx/translations/en/LC_MESSAGES/messages.po+8 searx/webapp.py+15−1
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+19−1
@@ -4,6 +4,8 @@ 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, timedelta
8+import re
79
810categories = ['news']
911search_url = 'http://news.search.yahoo.com/search?{query}&b={offset}'
@@ -11,6 +13,7 @@ results_xpath = '//div[@class="res"]'
1113url_xpath = './/h3/a/@href'
1214title_xpath = './/h3/a'
1315content_xpath = './/div[@class="abstr"]'
16+publishedDate_xpath = './/span[@class="timestamp"]'
1417suggestion_xpath = '//div[@id="satat"]//a'
1518
1619paging = True
@@ -37,7 +40,22 @@ def response(resp):
3740 url = parse_url(extract_url(result.xpath(url_xpath), search_url))
3841 title = extract_text(result.xpath(title_xpath)[0])
3942 content = extract_text(result.xpath(content_xpath)[0])
40− results.append({'url': url, 'title': title, 'content': content})
43+ publishedDate = extract_text(result.xpath(publishedDate_xpath)[0])
44+
45+ if re.match("^[0-9]+ minute(s|) ago$", publishedDate):
46+ publishedDate = datetime.now() - timedelta(minutes=int(re.match(r'\d+', publishedDate).group()))
47+ else:
48+ if re.match("^[0-9]+ hour(s|), [0-9]+ minute(s|) ago$", publishedDate):
49+ timeNumbers = re.findall(r'\d+', publishedDate)
50+ publishedDate = datetime.now() - timedelta(hours=int(timeNumbers[0])) - timedelta(minutes=int(timeNumbers[1]))
51+ else:
52+ # TODO year in string possible?
53+ publishedDate = datetime.strptime(publishedDate,"%b %d %H:%M%p")
54+
55+ if publishedDate.year == 1900:
56+ publishedDate = publishedDate.replace(year=datetime.now().year)
57+
58+ results.append({'url': url, 'title': title, 'content': content,'publishedDate':publishedDate})
4159
4260 if not suggestion_xpath:
4361 return results
searx/static/css/style.css+1
@@ -35,6 +35,7 @@ a{text-decoration:none;color:#1a11be}a:visited{color:#8e44ad}
3535.result h3{font-size:1em;word-wrap:break-word;margin:5px 0 1px 0;padding:0}
3636.result .content{font-size:.8em;margin:0;padding:0;max-width:54em;word-wrap:break-word;line-height:1.24}
3737.result .url{font-size:.8em;margin:3px 0 0 0;padding:0;max-width:54em;word-wrap:break-word;color:#c0392b}
38+.result .published_date{font-size:.8em;color:#888;margin:5px 20px}
3839.engines{color:#888}
3940.small_font{font-size:.8em}
4041.small p{margin:2px 0}
searx/static/less/definitions.less+3
@@ -64,6 +64,9 @@
6464// Url to result
6565@color-result-url-font: #C0392B;
6666
67+// Publish Date
68+@color-result-publishdate-font: #888;
69+
6770// Images
6871@color-result-image-span-background-hover: rgba(0, 0, 0, 0.6);
6972@color-result-image-span-font: #FFF;
searx/static/less/style.less+6
@@ -248,6 +248,12 @@ a {
248248 word-wrap:break-word;
249249 color: @color-result-url-font;
250250 }
251+
252+ .published_date {
253+ font-size: 0.8em;
254+ color: @color-result-publishdate-font;
255+ margin: 5px 20px;
256+ }
251257}
252258
253259.engines {
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/translations/en/LC_MESSAGES/messages.po+8
@@ -37,6 +37,14 @@ msgstr ""
3737msgid "Errors"
3838msgstr ""
3939
40+#: searx/webapp.py:167
41+msgid "{minutes} minute(s) ago"
42+msgstr ""
43+
44+#: searx/webapp.py:169
45+msgid "{hours} hour(s), {minutes} minute(s) ago"
46+msgstr ""
47+
4048#: searx/templates/index.html:7
4149msgid "about"
4250msgstr ""
searx/webapp.py+15−1
@@ -26,12 +26,13 @@ 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,
3233 redirect, send_from_directory
3334)
34−from flask.ext.babel import Babel
35+from flask.ext.babel import Babel, gettext, ngettext, format_date
3536from searx import settings, searx_dir
3637from searx.engines import (
3738 search as do_search, categories, engines, get_engines_stats,
@@ -156,6 +157,19 @@ 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'] >= datetime.now() - timedelta(days=1):
163+ timedifference = datetime.now() - result['publishedDate']
164+ minutes = int((timedifference.seconds/60)%60)
165+ hours = int(timedifference.seconds/60/60)
166+ if hours == 0:
167+ result['publishedDate'] = gettext(u'{minutes} minute(s) ago').format(minutes=minutes)
168+ else:
169+ result['publishedDate'] = gettext(u'{hours} hour(s), {minutes} minute(s) ago').format(hours=hours, minutes=minutes)
170+ else:
171+ result['publishedDate'] = format_date(result['publishedDate'])
172+
159173 if search.request_data.get('format') == 'json':
160174 return Response(json.dumps({'query': search.query,
161175 'results': search.results}),