Explorar el Código

[fix] youtube_noapi : don't crash when there is no content

Dalf hace 10 años
padre
commit
4e0f079a8c
Se han modificado 1 ficheros con 33 adiciones y 17 borrados
  1. 33
    17
      searx/engines/youtube_noapi.py

+ 33
- 17
searx/engines/youtube_noapi.py Ver fichero

@@ -34,6 +34,22 @@ title_xpath = './/div[@class="yt-lockup-content"]/h3/a'
34 34
 content_xpath = './/div[@class="yt-lockup-content"]/div[@class="yt-lockup-description yt-ui-ellipsis yt-ui-ellipsis-2"]'
35 35
 
36 36
 
37
+# get element in list or default value
38
+def list_get(a_list, index, default=None):
39
+    if len(a_list) > index:
40
+        return a_list[index]
41
+    else:
42
+        return default
43
+
44
+
45
+# returns extract_text on the first result selected by the xpath or None
46
+def extract_text_from_dom(result, xpath):
47
+    r = result.xpath(xpath)
48
+    if len(r) > 0:
49
+        return extract_text(r[0])
50
+    return None
51
+
52
+
37 53
 # do search-request
38 54
 def request(query, params):
39 55
     params['url'] = search_url.format(query=quote_plus(query),
@@ -50,23 +66,23 @@ def response(resp):
50 66
 
51 67
     # parse results
52 68
     for result in dom.xpath(results_xpath):
53
-        videoid = result.xpath('@data-context-item-id')[0]
54
-
55
-        url = base_youtube_url + videoid
56
-        thumbnail = 'https://i.ytimg.com/vi/' + videoid + '/hqdefault.jpg'
57
-
58
-        title = extract_text(result.xpath(title_xpath)[0])
59
-        content = extract_text(result.xpath(content_xpath)[0])
60
-
61
-        embedded = embedded_url.format(videoid=videoid)
62
-
63
-        # append result
64
-        results.append({'url': url,
65
-                        'title': title,
66
-                        'content': content,
67
-                        'template': 'videos.html',
68
-                        'embedded': embedded,
69
-                        'thumbnail': thumbnail})
69
+        videoid = list_get(result.xpath('@data-context-item-id'), 0)
70
+        if videoid is not None:
71
+            url = base_youtube_url + videoid
72
+            thumbnail = 'https://i.ytimg.com/vi/' + videoid + '/hqdefault.jpg'
73
+
74
+            title = extract_text_from_dom(result, title_xpath) or videoid
75
+            content = extract_text_from_dom(result, content_xpath)
76
+
77
+            embedded = embedded_url.format(videoid=videoid)
78
+
79
+            # append result
80
+            results.append({'url': url,
81
+                            'title': title,
82
+                            'content': content,
83
+                            'template': 'videos.html',
84
+                            'embedded': embedded,
85
+                            'thumbnail': thumbnail})
70 86
 
71 87
     # return results
72 88
     return results