Selaa lähdekoodia

simplify datetime extraction

Thomas Pointhuber 11 vuotta sitten
vanhempi
commit
337bd6d907
5 muutettua tiedostoa jossa 15 lisäystä ja 13 poistoa
  1. 1
    0
      requirements.txt
  2. 4
    10
      searx/engines/google_news.py
  3. 2
    3
      searx/engines/yahoo_news.py
  4. 7
    0
      searx/engines/youtube.py
  5. 1
    0
      setup.py

+ 1
- 0
requirements.txt Näytä tiedosto

@@ -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 Näytä tiedosto

@@ -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'],

+ 2
- 3
searx/engines/yahoo_news.py Näytä tiedosto

@@ -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 Näytä tiedosto

@@ -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
setup.py Näytä tiedosto

@@ -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': [