Sfoglia il codice sorgente

fix dailymotion engine and add comments to it

Thomas Pointhuber 10 anni fa
parent
commit
90dcfc1ddd
1 ha cambiato i file con 35 aggiunte e 19 eliminazioni
  1. 35
    19
      searx/engines/dailymotion.py

+ 35
- 19
searx/engines/dailymotion.py Vedi File

@@ -1,45 +1,61 @@
1
+## Dailymotion (Videos)
2
+# 
3
+# @website     https://www.dailymotion.com
4
+# @provide-api yes (http://www.dailymotion.com/developer)
5
+# 
6
+# @using-api   yes
7
+# @results     JSON
8
+# @stable      yes
9
+# @parse       url, title, thumbnail
10
+#
11
+# @todo        set content-parameter with correct data
12
+
1 13
 from urllib import urlencode
2 14
 from json import loads
3 15
 from lxml import html
4 16
 
17
+# engine dependent config
5 18
 categories = ['videos']
6 19
 locale = 'en_US'
20
+paging = True
7 21
 
22
+# search-url
8 23
 # see http://www.dailymotion.com/doc/api/obj-video.html
9
-search_url = 'https://api.dailymotion.com/videos?fields=title,description,duration,url,thumbnail_360_url&sort=relevance&limit=25&page={pageno}&{query}'  # noqa
10
-
11
-# TODO use video result template
12
-content_tpl = '<a href="{0}" title="{0}" ><img src="{1}" /></a><br />'
13
-
14
-paging = True
24
+search_url = 'https://api.dailymotion.com/videos?fields=title,description,duration,url,thumbnail_360_url&sort=relevance&limit=5&page={pageno}&{query}'  # noqa
15 25
 
16 26
 
27
+# do search-request
17 28
 def request(query, params):
18 29
     params['url'] = search_url.format(
19 30
         query=urlencode({'search': query, 'localization': locale}),
20 31
         pageno=params['pageno'])
32
+
21 33
     return params
22 34
 
23 35
 
36
+# get response from search-request
24 37
 def response(resp):
25 38
     results = []
39
+
26 40
     search_res = loads(resp.text)
41
+
42
+    # return empty array if there are no results
27 43
     if not 'list' in search_res:
28
-        return results
44
+        return []
45
+
46
+    # parse results
29 47
     for res in search_res['list']:
30 48
         title = res['title']
31 49
         url = res['url']
32
-        if res['thumbnail_360_url']:
33
-            content = content_tpl.format(url, res['thumbnail_360_url'])
34
-        else:
35
-            content = ''
36
-        if res['description']:
37
-            description = text_content_from_html(res['description'])
38
-            content += description[:500]
39
-        results.append({'url': url, 'title': title, 'content': content})
40
-    return results
50
+        #content = res['description']
51
+        content = ''
52
+        thumbnail = res['thumbnail_360_url']
41 53
 
54
+        results.append({'template': 'videos.html',
55
+                        'url': url,
56
+                        'title': title,
57
+                        'content': content,
58
+                        'thumbnail': thumbnail})
42 59
 
43
-def text_content_from_html(html_string):
44
-    desc_html = html.fragment_fromstring(html_string, create_parent=True)
45
-    return desc_html.text_content()
60
+    # return results
61
+    return results