Przeglądaj źródła

fix dailymotion engine and add comments to it

Thomas Pointhuber 10 lat temu
rodzic
commit
90dcfc1ddd
1 zmienionych plików z 35 dodań i 19 usunięć
  1. 35
    19
      searx/engines/dailymotion.py

+ 35
- 19
searx/engines/dailymotion.py Wyświetl plik

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
 from urllib import urlencode
13
 from urllib import urlencode
2
 from json import loads
14
 from json import loads
3
 from lxml import html
15
 from lxml import html
4
 
16
 
17
+# engine dependent config
5
 categories = ['videos']
18
 categories = ['videos']
6
 locale = 'en_US'
19
 locale = 'en_US'
20
+paging = True
7
 
21
 
22
+# search-url
8
 # see http://www.dailymotion.com/doc/api/obj-video.html
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
 def request(query, params):
28
 def request(query, params):
18
     params['url'] = search_url.format(
29
     params['url'] = search_url.format(
19
         query=urlencode({'search': query, 'localization': locale}),
30
         query=urlencode({'search': query, 'localization': locale}),
20
         pageno=params['pageno'])
31
         pageno=params['pageno'])
32
+
21
     return params
33
     return params
22
 
34
 
23
 
35
 
36
+# get response from search-request
24
 def response(resp):
37
 def response(resp):
25
     results = []
38
     results = []
39
+
26
     search_res = loads(resp.text)
40
     search_res = loads(resp.text)
41
+
42
+    # return empty array if there are no results
27
     if not 'list' in search_res:
43
     if not 'list' in search_res:
28
-        return results
44
+        return []
45
+
46
+    # parse results
29
     for res in search_res['list']:
47
     for res in search_res['list']:
30
         title = res['title']
48
         title = res['title']
31
         url = res['url']
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