From b88146d669b1196ed1efc4ae4108e238cfd7dbca Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Fri, 14 Mar 2014 09:55:04 +0100 Subject: showing publishedDate for news --- searx/engines/google_news.py | 9 +++++++++ searx/engines/yahoo_news.py | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) (limited to 'searx/engines') diff --git a/searx/engines/google_news.py b/searx/engines/google_news.py index 935718609..afda3e756 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 35e323917..c9789240d 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 -- cgit v1.2.3 From 5538c6771abacb9dab504fa7da0052c443b14f13 Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Sat, 15 Mar 2014 19:20:29 +0100 Subject: improve publishDate extraction from yahoo --- searx/engines/yahoo_news.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'searx/engines') diff --git a/searx/engines/yahoo_news.py b/searx/engines/yahoo_news.py index c9789240d..13a8a6024 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: -- cgit v1.2.3