瀏覽代碼

improve publishDate extraction from yahoo

Thomas Pointhuber 11 年之前
父節點
當前提交
5538c6771a
共有 1 個文件被更改,包括 17 次插入4 次删除
  1. 17
    4
      searx/engines/yahoo_news.py

+ 17
- 4
searx/engines/yahoo_news.py 查看文件

@@ -4,7 +4,8 @@ from urllib import urlencode
4 4
 from lxml import html
5 5
 from searx.engines.xpath import extract_text, extract_url
6 6
 from searx.engines.yahoo import parse_url
7
-from datetime import datetime
7
+from datetime import datetime, timedelta
8
+import re
8 9
 
9 10
 categories = ['news']
10 11
 search_url = 'http://news.search.yahoo.com/search?{query}&b={offset}'
@@ -39,9 +40,21 @@ def response(resp):
39 40
         url = parse_url(extract_url(result.xpath(url_xpath), search_url))
40 41
         title = extract_text(result.xpath(title_xpath)[0])
41 42
         content = extract_text(result.xpath(content_xpath)[0])
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)
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
+
45 58
         results.append({'url': url, 'title': title, 'content': content,'publishedDate':publishedDate})
46 59
 
47 60
     if not suggestion_xpath: