on master help sign in
fix dailymotion engine and add comments to it
90dcfc1Thomas Pointhuber
searx/engines/dailymotion.py+35−19
@@ -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+
1from urllib import urlencode
13from urllib import urlencode
2from json import loads
14from json import loads
3from lxml import html
15from lxml import html
4
16
17+# engine dependent config
5categories = ['videos']
18categories = ['videos']
6locale = 'en_US'
19locale = '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
24+search_url = 'https://api.dailymotion.com/videos?fields=title,description,duration,url,thumbnail_360_url&sort=relevance&limit=5&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
15
25
16
26
27+# do search-request
17def request(query, params):
28def 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
24def response(resp):
37def 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']:
50+ #content = res['description']
33− content = content_tpl.format(url, res['thumbnail_360_url'])
34− else:
35− content = ''
51+ content = ''
36− if res['description']:
52+ thumbnail = res['thumbnail_360_url']
37− description = text_content_from_html(res['description'])
38− content += description[:500]
39− results.append({'url': url, 'title': title, 'content': content})
40− return results
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):
60+ # return results
44− desc_html = html.fragment_fromstring(html_string, create_parent=True)
61+ return results
45− return desc_html.text_content()