Browse Source

[mod] add/modify image fetching for bing_news, qwant and twitter engines

Alexandre Flament 8 years ago
parent
commit
f5128c7cb9

+ 2
- 3
searx/engines/bing_news.py View File

@@ -112,12 +112,11 @@ def response(resp):
112 112
 
113 113
         # append result
114 114
         if thumbnail is not None:
115
-            results.append({'template': 'videos.html',
116
-                            'url': url,
115
+            results.append({'url': url,
117 116
                             'title': title,
118 117
                             'publishedDate': publishedDate,
119 118
                             'content': content,
120
-                            'thumbnail': thumbnail})
119
+                            'img_src': thumbnail})
121 120
         else:
122 121
             results.append({'url': url,
123 122
                             'title': title,

+ 16
- 3
searx/engines/qwant.py View File

@@ -96,14 +96,27 @@ def response(resp):
96 96
                             'thumbnail_src': thumbnail_src,
97 97
                             'img_src': img_src})
98 98
 
99
-        elif (category_to_keyword.get(categories[0], '') == 'news' or
100
-              category_to_keyword.get(categories[0], '') == 'social'):
99
+        elif category_to_keyword.get(categories[0], '') == 'social':
101 100
             published_date = datetime.fromtimestamp(result['date'], None)
101
+            img_src = result.get('img', None)
102
+            results.append({'url': res_url,
103
+                            'title': title,
104
+                            'publishedDate': published_date,
105
+                            'content': content,
106
+                            'img_src': img_src})
102 107
 
108
+        elif category_to_keyword.get(categories[0], '') == 'news':
109
+            published_date = datetime.fromtimestamp(result['date'], None)
110
+            media = result.get('media', [])
111
+            if len(media) > 0:
112
+                img_src = media[0].get('pict', {}).get('url', None)
113
+            else:
114
+                img_src = None
103 115
             results.append({'url': res_url,
104 116
                             'title': title,
105 117
                             'publishedDate': published_date,
106
-                            'content': content})
118
+                            'content': content,
119
+                            'img_src': img_src})
107 120
 
108 121
     return results
109 122
 

+ 6
- 1
searx/engines/twitter.py View File

@@ -27,6 +27,7 @@ search_url = base_url + 'search?'
27 27
 
28 28
 # specific xpath variables
29 29
 results_xpath = '//li[@data-item-type="tweet"]'
30
+avatar_xpath = './/img[contains(@class, "avatar")]/@src'
30 31
 link_xpath = './/small[@class="time"]//a'
31 32
 title_xpath = './/span[contains(@class, "username")]'
32 33
 content_xpath = './/p[contains(@class, "tweet-text")]'
@@ -57,6 +58,8 @@ def response(resp):
57 58
         try:
58 59
             link = tweet.xpath(link_xpath)[0]
59 60
             content = extract_text(tweet.xpath(content_xpath)[0])
61
+            img_src = tweet.xpath(avatar_xpath)[0]
62
+            img_src = img_src.replace('_bigger', '_normal')
60 63
         except Exception:
61 64
             continue
62 65
 
@@ -71,12 +74,14 @@ def response(resp):
71 74
             results.append({'url': url,
72 75
                             'title': title,
73 76
                             'content': content,
77
+                            'img_src': img_src,
74 78
                             'publishedDate': publishedDate})
75 79
         else:
76 80
             # append result
77 81
             results.append({'url': url,
78 82
                             'title': title,
79
-                            'content': content})
83
+                            'content': content,
84
+                            'img_src': img_src})
80 85
 
81 86
     # return results
82 87
     return results

+ 3
- 3
tests/unit/engines/test_bing_news.py View File

@@ -81,11 +81,11 @@ class TestBingNewsEngine(SearxTestCase):
81 81
         self.assertEqual(results[0]['title'], 'Title')
82 82
         self.assertEqual(results[0]['url'], 'http://url.of.article/')
83 83
         self.assertEqual(results[0]['content'], 'Article Content')
84
-        self.assertEqual(results[0]['thumbnail'], 'https://www.bing.com/th?id=ON.13371337133713371337133713371337')
84
+        self.assertEqual(results[0]['img_src'], 'https://www.bing.com/th?id=ON.13371337133713371337133713371337')
85 85
         self.assertEqual(results[1]['title'], 'Another Title')
86 86
         self.assertEqual(results[1]['url'], 'http://another.url.of.article/')
87 87
         self.assertEqual(results[1]['content'], 'Another Article Content')
88
-        self.assertNotIn('thumbnail', results[1])
88
+        self.assertNotIn('img_src', results[1])
89 89
 
90 90
         html = """<?xml version="1.0" encoding="utf-8" ?>
91 91
 <rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS">
@@ -120,7 +120,7 @@ class TestBingNewsEngine(SearxTestCase):
120 120
         self.assertEqual(results[0]['title'], 'Title')
121 121
         self.assertEqual(results[0]['url'], 'http://another.url.of.article/')
122 122
         self.assertEqual(results[0]['content'], 'Article Content')
123
-        self.assertEqual(results[0]['thumbnail'], 'http://another.bing.com/image')
123
+        self.assertEqual(results[0]['img_src'], 'http://another.bing.com/image')
124 124
 
125 125
         html = """<?xml version="1.0" encoding="utf-8" ?>
126 126
 <rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS">