From b88146d669b1196ed1efc4ae4108e238cfd7dbca Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Fri, 14 Mar 2014 09:55:04 +0100 Subject: [PATCH 1/8] showing publishedDate for news --- searx/engines/google_news.py | 9 +++++++++ searx/engines/yahoo_news.py | 7 ++++++- searx/templates/result_templates/default.html | 1 + searx/webapp.py | 12 ++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/searx/engines/google_news.py b/searx/engines/google_news.py index 93571860..afda3e75 100644 --- a/searx/engines/google_news.py +++ b/searx/engines/google_news.py @@ -2,6 +2,7 @@ from urllib import urlencode from json import loads +from datetime import datetime, timedelta categories = ['news'] @@ -31,7 +32,15 @@ def response(resp): return [] for result in search_res['responseData']['results']: +# S.149 (159), library.pdf +# datetime.strptime("Mon, 10 Mar 2014 16:26:15 -0700", "%a, %d %b %Y %H:%M:%S %z") +# publishedDate = parse(result['publishedDate']) + publishedDate = datetime.strptime(str.join(' ',result['publishedDate'].split(None)[0:5]), "%a, %d %b %Y %H:%M:%S") + #utc_offset = timedelta(result['publishedDate'].split(None)[5]) # local = utc + offset + #publishedDate = publishedDate + utc_offset + results.append({'url': result['unescapedUrl'], 'title': result['titleNoFormatting'], + 'publishedDate': publishedDate, 'content': result['content']}) return results diff --git a/searx/engines/yahoo_news.py b/searx/engines/yahoo_news.py index 35e32391..c9789240 100644 --- a/searx/engines/yahoo_news.py +++ b/searx/engines/yahoo_news.py @@ -4,6 +4,7 @@ from urllib import urlencode from lxml import html from searx.engines.xpath import extract_text, extract_url from searx.engines.yahoo import parse_url +from datetime import datetime categories = ['news'] search_url = 'http://news.search.yahoo.com/search?{query}&b={offset}' @@ -11,6 +12,7 @@ results_xpath = '//div[@class="res"]' url_xpath = './/h3/a/@href' title_xpath = './/h3/a' content_xpath = './/div[@class="abstr"]' +publishedDate_xpath = './/span[@class="timestamp"]' suggestion_xpath = '//div[@id="satat"]//a' paging = True @@ -37,7 +39,10 @@ def response(resp): url = parse_url(extract_url(result.xpath(url_xpath), search_url)) title = extract_text(result.xpath(title_xpath)[0]) content = extract_text(result.xpath(content_xpath)[0]) - results.append({'url': url, 'title': title, 'content': content}) +# Feb 20 04:02am + publishedDate = datetime.strptime(extract_text(result.xpath(publishedDate_xpath)[0]),"%b %d %H:%M%p") + #publishedDate.replace(year=2014) + results.append({'url': url, 'title': title, 'content': content,'publishedDate':publishedDate}) if not suggestion_xpath: return results diff --git a/searx/templates/result_templates/default.html b/searx/templates/result_templates/default.html index d06a4598..e0711b76 100644 --- a/searx/templates/result_templates/default.html +++ b/searx/templates/result_templates/default.html @@ -6,6 +6,7 @@

{{ result.title|safe }}

+ {% if result.publishedDate %}

{{ result.publishedDate }}

{% endif %}

{% if result.content %}{{ result.content|safe }}
{% endif %}

{{ result.pretty_url }}

diff --git a/searx/webapp.py b/searx/webapp.py index fdb6568b..2f7f131c 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -26,6 +26,7 @@ import json import cStringIO import os +from datetime import datetime, timedelta from itertools import chain from flask import ( Flask, request, render_template, url_for, Response, make_response, @@ -156,6 +157,17 @@ def index(): if engine in favicons: result['favicon'] = engine + # TODO, check if timezone is calculated right + if 'publishedDate' in result: + if result['publishedDate'].date() == datetime.now().date(): + timedifference = datetime.now()-result['publishedDate'] + if timedifference.seconds < 60*60: + result['publishedDate'] = '{0:d} minutes ago'.format(timedifference.seconds/60) + else: + result['publishedDate'] = '{0:d} hours ago'.format(timedifference.seconds/60/60) + else: + result['publishedDate'] = result['publishedDate'].strftime('%d.%m.%Y') + if search.request_data.get('format') == 'json': return Response(json.dumps({'query': search.query, 'results': search.results}), From c54baf63015e884c060e10bc7a73a2c1503564c1 Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Fri, 14 Mar 2014 10:04:36 +0100 Subject: [PATCH 2/8] Adding style for publish Date --- searx/static/css/style.css | 1 + searx/static/less/definitions.less | 3 +++ searx/static/less/style.less | 6 ++++++ 3 files changed, 10 insertions(+) diff --git a/searx/static/css/style.css b/searx/static/css/style.css index bc4c034d..767d7b05 100644 --- a/searx/static/css/style.css +++ b/searx/static/css/style.css @@ -35,6 +35,7 @@ a{text-decoration:none;color:#1a11be}a:visited{color:#8e44ad} .result h3{font-size:1em;word-wrap:break-word;margin:5px 0 1px 0;padding:0} .result .content{font-size:.8em;margin:0;padding:0;max-width:54em;word-wrap:break-word;line-height:1.24} .result .url{font-size:.8em;margin:3px 0 0 0;padding:0;max-width:54em;word-wrap:break-word;color:#c0392b} +.result .published_date{font-size:.8em;color:#888;margin:5px 20px} .engines{color:#888} .small_font{font-size:.8em} .small p{margin:2px 0} diff --git a/searx/static/less/definitions.less b/searx/static/less/definitions.less index f71f5578..2d85f781 100644 --- a/searx/static/less/definitions.less +++ b/searx/static/less/definitions.less @@ -64,6 +64,9 @@ // Url to result @color-result-url-font: #C0392B; +// Publish Date +@color-result-publishdate-font: #888; + // Images @color-result-image-span-background-hover: rgba(0, 0, 0, 0.6); @color-result-image-span-font: #FFF; diff --git a/searx/static/less/style.less b/searx/static/less/style.less index 556ecd61..817f6b31 100644 --- a/searx/static/less/style.less +++ b/searx/static/less/style.less @@ -248,6 +248,12 @@ a { word-wrap:break-word; color: @color-result-url-font; } + + .published_date { + font-size: 0.8em; + color: @color-result-publishdate-font; + margin: 5px 20px; + } } .engines { From 7a922b2ab8464f32ebed03d4c2b652159891fdc7 Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Fri, 14 Mar 2014 10:55:52 +0100 Subject: [PATCH 3/8] adding translations support for publish-date --- searx/webapp.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/searx/webapp.py b/searx/webapp.py index 2f7f131c..9d1221fa 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -32,7 +32,7 @@ from flask import ( Flask, request, render_template, url_for, Response, make_response, redirect, send_from_directory ) -from flask.ext.babel import Babel +from flask.ext.babel import Babel, gettext, ngettext, format_date from searx import settings, searx_dir from searx.engines import ( search as do_search, categories, engines, get_engines_stats, @@ -162,11 +162,11 @@ def index(): if result['publishedDate'].date() == datetime.now().date(): timedifference = datetime.now()-result['publishedDate'] if timedifference.seconds < 60*60: - result['publishedDate'] = '{0:d} minutes ago'.format(timedifference.seconds/60) + result['publishedDate'] = gettext(u'{0:d} minutes ago').format(timedifference.seconds/60) else: - result['publishedDate'] = '{0:d} hours ago'.format(timedifference.seconds/60/60) + result['publishedDate'] = gettext(u'{0:d} hours ago').format(timedifference.seconds/60/60) else: - result['publishedDate'] = result['publishedDate'].strftime('%d.%m.%Y') + result['publishedDate'] = format_date(result['publishedDate']) if search.request_data.get('format') == 'json': return Response(json.dumps({'query': search.query, From 794165d19cbf91b62d45e93c9a2f387346b89c7b Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Sat, 15 Mar 2014 19:13:57 +0100 Subject: [PATCH 4/8] improve published Date output --- searx/webapp.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/searx/webapp.py b/searx/webapp.py index 9d1221fa..5cc05904 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -161,10 +161,24 @@ def index(): if 'publishedDate' in result: if result['publishedDate'].date() == datetime.now().date(): timedifference = datetime.now()-result['publishedDate'] - if timedifference.seconds < 60*60: - result['publishedDate'] = gettext(u'{0:d} minutes ago').format(timedifference.seconds/60) + minutes = int((timedifference.seconds/60)%60) + hours = int(timedifference.seconds/60/60) + if hours == 0: + if minutes == 1: + result['publishedDate'] = gettext(u'1 minute ago') + else: + result['publishedDate'] = gettext(u'{minutes} minutes ago').format(minutes=minutes) else: - result['publishedDate'] = gettext(u'{0:d} hours ago').format(timedifference.seconds/60/60) + if hours == 1: + if minutes == 1: + result['publishedDate'] = gettext(u'1 hour, 1 minute ago') + else: + result['publishedDate'] = gettext(u'1 hour, {minutes} minutes ago').format(minutes=minutes) + else: + if minutes == 1: + result['publishedDate'] = gettext(u'{hours} hours, 1 minutes ago').format(hours=hours) + else: + result['publishedDate'] = gettext(u'{hours} hours, {minutes} minutes ago').format(hours=hours, minutes=minutes) else: result['publishedDate'] = format_date(result['publishedDate']) From 5538c6771abacb9dab504fa7da0052c443b14f13 Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Sat, 15 Mar 2014 19:20:29 +0100 Subject: [PATCH 5/8] improve publishDate extraction from yahoo --- searx/engines/yahoo_news.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/searx/engines/yahoo_news.py b/searx/engines/yahoo_news.py index c9789240..13a8a602 100644 --- a/searx/engines/yahoo_news.py +++ b/searx/engines/yahoo_news.py @@ -4,7 +4,8 @@ from urllib import urlencode from lxml import html from searx.engines.xpath import extract_text, extract_url from searx.engines.yahoo import parse_url -from datetime import datetime +from datetime import datetime, timedelta +import re categories = ['news'] search_url = 'http://news.search.yahoo.com/search?{query}&b={offset}' @@ -39,9 +40,21 @@ def response(resp): url = parse_url(extract_url(result.xpath(url_xpath), search_url)) title = extract_text(result.xpath(title_xpath)[0]) content = extract_text(result.xpath(content_xpath)[0]) -# Feb 20 04:02am - publishedDate = datetime.strptime(extract_text(result.xpath(publishedDate_xpath)[0]),"%b %d %H:%M%p") - #publishedDate.replace(year=2014) + publishedDate = extract_text(result.xpath(publishedDate_xpath)[0]) + + if re.match("^[0-9]+ minute(s|) ago$", publishedDate): + publishedDate = datetime.now() - timedelta(minutes=int(re.match(r'\d+', publishedDate).group())) + else: + if re.match("^[0-9]+ hour(s|), [0-9]+ minute(s|) ago$", publishedDate): + timeNumbers = re.findall(r'\d+', publishedDate) + publishedDate = datetime.now() - timedelta(hours=int(timeNumbers[0])) - timedelta(minutes=int(timeNumbers[1])) + else: + # TODO year in string possible? + publishedDate = datetime.strptime(publishedDate,"%b %d %H:%M%p") + + if publishedDate.year == 1900: + publishedDate = publishedDate.replace(year=datetime.now().year) + results.append({'url': url, 'title': title, 'content': content,'publishedDate':publishedDate}) if not suggestion_xpath: From ef2b1b7515fbc029d566ab1afb80429007983fbb Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Sat, 15 Mar 2014 19:31:34 +0100 Subject: [PATCH 6/8] fix little bug --- searx/webapp.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/searx/webapp.py b/searx/webapp.py index 5cc05904..54061a46 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -159,8 +159,8 @@ def index(): # TODO, check if timezone is calculated right if 'publishedDate' in result: - if result['publishedDate'].date() == datetime.now().date(): - timedifference = datetime.now()-result['publishedDate'] + if result['publishedDate'] >= datetime.now() - timedelta(days=1): + timedifference = datetime.now() - result['publishedDate'] minutes = int((timedifference.seconds/60)%60) hours = int(timedifference.seconds/60/60) if hours == 0: From d54ef01898a1bd289560ae757593df9f43fba7bc Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Sat, 15 Mar 2014 19:41:54 +0100 Subject: [PATCH 7/8] simplify publish Date formating --- searx/webapp.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/searx/webapp.py b/searx/webapp.py index 54061a46..251d5672 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -164,21 +164,9 @@ def index(): minutes = int((timedifference.seconds/60)%60) hours = int(timedifference.seconds/60/60) if hours == 0: - if minutes == 1: - result['publishedDate'] = gettext(u'1 minute ago') - else: - result['publishedDate'] = gettext(u'{minutes} minutes ago').format(minutes=minutes) + result['publishedDate'] = gettext(u'{minutes} minute(s) ago').format(minutes=minutes) else: - if hours == 1: - if minutes == 1: - result['publishedDate'] = gettext(u'1 hour, 1 minute ago') - else: - result['publishedDate'] = gettext(u'1 hour, {minutes} minutes ago').format(minutes=minutes) - else: - if minutes == 1: - result['publishedDate'] = gettext(u'{hours} hours, 1 minutes ago').format(hours=hours) - else: - result['publishedDate'] = gettext(u'{hours} hours, {minutes} minutes ago').format(hours=hours, minutes=minutes) + result['publishedDate'] = gettext(u'{hours} hour(s), {minutes} minute(s) ago').format(hours=hours, minutes=minutes) else: result['publishedDate'] = format_date(result['publishedDate']) From d20a3d61648e528d8d7ea0d823c2c36139dd397f Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Sat, 15 Mar 2014 19:58:40 +0100 Subject: [PATCH 8/8] update english translation --- searx/translations/en/LC_MESSAGES/messages.po | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/searx/translations/en/LC_MESSAGES/messages.po b/searx/translations/en/LC_MESSAGES/messages.po index b8f6d38f..2b5641f6 100644 --- a/searx/translations/en/LC_MESSAGES/messages.po +++ b/searx/translations/en/LC_MESSAGES/messages.po @@ -37,6 +37,14 @@ msgstr "" msgid "Errors" msgstr "" +#: searx/webapp.py:167 +msgid "{minutes} minute(s) ago" +msgstr "" + +#: searx/webapp.py:169 +msgid "{hours} hour(s), {minutes} minute(s) ago" +msgstr "" + #: searx/templates/index.html:7 msgid "about" msgstr ""