瀏覽代碼

Merge pull request #57 from pointhi/results

improving publishDate extraction and output of it
Adam Tauber 11 年之前
父節點
當前提交
018a14431b

+ 1
- 0
requirements.txt 查看文件

@@ -3,3 +3,4 @@ flask-babel
3 3
 grequests
4 4
 lxml
5 5
 pyyaml
6
+python-dateutil

+ 4
- 10
searx/engines/google_news.py 查看文件

@@ -2,6 +2,7 @@
2 2
 
3 3
 from urllib import urlencode
4 4
 from json import loads
5
+from dateutil import parser
5 6
 from datetime import datetime
6 7
 
7 8
 categories = ['news']
@@ -32,16 +33,9 @@ def response(resp):
32 33
         return []
33 34
 
34 35
     for result in search_res['responseData']['results']:
35
-# S.149 (159), library.pdf
36
-# datetime.strptime("Mon, 10 Mar 2014 16:26:15 -0700",
37
-#                   "%a, %d %b %Y %H:%M:%S %z")
38
-#        publishedDate = parse(result['publishedDate'])
39
-        publishedDate = datetime.strptime(
40
-            str.join(' ', result['publishedDate'].split(None)[0:5]),
41
-            "%a, %d %b %Y %H:%M:%S")
42
-        #utc_offset = timedelta(result['publishedDate'].split(None)[5])
43
-        # local = utc + offset
44
-        #publishedDate = publishedDate + utc_offset
36
+
37
+# Mon, 10 Mar 2014 16:26:15 -0700
38
+        publishedDate = parser.parse(result['publishedDate'])
45 39
 
46 40
         results.append({'url': result['unescapedUrl'],
47 41
                         'title': result['titleNoFormatting'],

+ 6
- 0
searx/engines/vimeo.py 查看文件

@@ -2,6 +2,8 @@ from urllib import urlencode
2 2
 from HTMLParser import HTMLParser
3 3
 from lxml import html
4 4
 from xpath import extract_text
5
+from datetime import datetime
6
+from dateutil import parser
5 7
 
6 8
 base_url = 'http://vimeo.com'
7 9
 search_url = base_url + '/search?{query}'
@@ -10,6 +12,7 @@ content_xpath = None
10 12
 title_xpath = None
11 13
 results_xpath = ''
12 14
 content_tpl = '<a href="{0}">  <img src="{2}"/> </a>'
15
+publishedDate_xpath = './/p[@class="meta"]//attribute::datetime'
13 16
 
14 17
 # the cookie set by vimeo contains all the following values,
15 18
 # but only __utma seems to be requiered
@@ -40,9 +43,12 @@ def response(resp):
40 43
         url = base_url + result.xpath(url_xpath)[0]
41 44
         title = p.unescape(extract_text(result.xpath(title_xpath)))
42 45
         thumbnail = extract_text(result.xpath(content_xpath)[0])
46
+        publishedDate = parser.parse(extract_text(result.xpath(publishedDate_xpath)[0]))
47
+
43 48
         results.append({'url': url,
44 49
                         'title': title,
45 50
                         'content': content_tpl.format(url, title, thumbnail),
46 51
                         'template': 'videos.html',
52
+                        'publishedDate': publishedDate,
47 53
                         'thumbnail': thumbnail})
48 54
     return results

+ 2
- 3
searx/engines/yahoo_news.py 查看文件

@@ -6,6 +6,7 @@ from searx.engines.xpath import extract_text, extract_url
6 6
 from searx.engines.yahoo import parse_url
7 7
 from datetime import datetime, timedelta
8 8
 import re
9
+from dateutil import parser
9 10
 
10 11
 categories = ['news']
11 12
 search_url = 'http://news.search.yahoo.com/search?{query}&b={offset}'
@@ -52,9 +53,7 @@ def response(resp):
52 53
                     - timedelta(hours=int(timeNumbers[0]))\
53 54
                     - timedelta(minutes=int(timeNumbers[1]))
54 55
             else:
55
-                # TODO year in string possible?
56
-                publishedDate = datetime.strptime(publishedDate,
57
-                                                  "%b %d %H:%M%p")
56
+                publishedDate = parser.parse(publishedDate)
58 57
 
59 58
         if publishedDate.year == 1900:
60 59
             publishedDate = publishedDate.replace(year=datetime.now().year)

+ 7
- 0
searx/engines/youtube.py 查看文件

@@ -1,5 +1,7 @@
1 1
 from json import loads
2 2
 from urllib import urlencode
3
+from dateutil import parser
4
+from datetime import datetime
3 5
 
4 6
 categories = ['videos']
5 7
 
@@ -35,6 +37,10 @@ def response(resp):
35 37
         content = ''
36 38
         thumbnail = ''
37 39
 
40
+#"2013-12-31T15:22:51.000Z"
41
+        pubdate = result['published']['$t']
42
+        publishedDate = parser.parse(pubdate)
43
+
38 44
         if result['media$group']['media$thumbnail']:
39 45
             thumbnail = result['media$group']['media$thumbnail'][0]['url']
40 46
             content += '<a href="{0}" title="{0}" ><img src="{1}" /></a>'.format(url, thumbnail)  # noqa
@@ -48,6 +54,7 @@ def response(resp):
48 54
                         'title': title,
49 55
                         'content': content,
50 56
                         'template': 'videos.html',
57
+                        'publishedDate': publishedDate,
51 58
                         'thumbnail': thumbnail})
52 59
 
53 60
     return results

+ 1
- 0
searx/templates/opensearch_response_rss.xml 查看文件

@@ -16,6 +16,7 @@
16 16
       <title>{{ r.title }}</title>
17 17
       <link>{{ r.url }}</link>
18 18
       <description>{{ r.content }}</description>
19
+      {% if r.pubdate %}<pubDate>{{ r.pubdate }}</pubDate>{% endif %}
19 20
     </item>
20 21
     {% endfor %}
21 22
   </channel>

+ 1
- 0
searx/templates/result_templates/videos.html 查看文件

@@ -5,6 +5,7 @@
5 5
 
6 6
     <p>
7 7
       <h3 class="result_title"><a href="{{ result.url }}">{{ result.title|safe }}</a></h3>
8
+      {% if result.publishedDate %}<p class="published_date">{{ result.publishedDate }}</p>{% endif %}
8 9
       <a href="{{ result.url }}"><img width="400px" src="{{ result.thumbnail }}" title={{ result.title }} alt=" {{ result.title }}"/></a>
9 10
       <p class="url">{{ result.url }}</p>
10 11
     </p>

+ 3
- 2
searx/webapp.py 查看文件

@@ -159,8 +159,8 @@ def index():
159 159
 
160 160
         # TODO, check if timezone is calculated right
161 161
         if 'publishedDate' in result:
162
-            if result['publishedDate'] >= datetime.now() - timedelta(days=1):
163
-                timedifference = datetime.now() - result['publishedDate']
162
+            if result['publishedDate'].replace(tzinfo=None) >= datetime.now() - timedelta(days=1):
163
+                timedifference = datetime.now() - result['publishedDate'].replace(tzinfo=None)
164 164
                 minutes = int((timedifference.seconds / 60) % 60)
165 165
                 hours = int(timedifference.seconds / 60 / 60)
166 166
                 if hours == 0:
@@ -168,6 +168,7 @@ def index():
168 168
                 else:
169 169
                     result['publishedDate'] = gettext(u'{hours} hour(s), {minutes} minute(s) ago').format(hours=hours, minutes=minutes)  # noqa
170 170
             else:
171
+                result['pubdate'] = result['publishedDate'].strftime('%a, %d %b %Y %H:%M:%S %z')
171 172
                 result['publishedDate'] = format_date(result['publishedDate'])
172 173
 
173 174
     if search.request_data.get('format') == 'json':

+ 1
- 0
setup.py 查看文件

@@ -35,6 +35,7 @@ setup(
35 35
         'lxml',
36 36
         'pyyaml',
37 37
         'setuptools',
38
+        'python-dateutil',
38 39
     ],
39 40
     extras_require={
40 41
         'test': [