summaryrefslogtreecommitdiff
path: root/searx
diff options
context:
space:
mode:
authorAlexandre Flament <alex@al-f.net>2019-04-09 03:32:39 +0200
committerGitHub <noreply@github.com>2019-04-09 03:32:39 +0200
commit3e9ce2638ae0b2f06c9a9f9a8c1cd4ab0082c05d (patch)
tree8f3801a89474801430722fd6da44ef05b2cbad1f /searx
parentcef77f0d51db75465c0a8f6107e4a5ac9ad9f5ed (diff)
parentca09d9102310e24c08db4c413e695fabfd5def4f (diff)
Merge branch 'master' into patch-1
Diffstat (limited to 'searx')
-rw-r--r--searx/engines/youtube_noapi.py70
-rw-r--r--searx/query.py15
2 files changed, 40 insertions, 45 deletions
diff --git a/searx/engines/youtube_noapi.py b/searx/engines/youtube_noapi.py
index 9f01841f6..3bf25932b 100644
--- a/searx/engines/youtube_noapi.py
+++ b/searx/engines/youtube_noapi.py
@@ -8,7 +8,8 @@
# @stable no
# @parse url, title, content, publishedDate, thumbnail, embedded
-from lxml import html
+from functools import reduce
+from json import loads
from searx.engines.xpath import extract_text
from searx.utils import list_get
from searx.url_utils import quote_plus
@@ -34,20 +35,6 @@ embedded_url = '<iframe width="540" height="304" ' +\
base_youtube_url = 'https://www.youtube.com/watch?v='
-# specific xpath variables
-results_xpath = "//ol/li/div[contains(@class, 'yt-lockup yt-lockup-tile yt-lockup-video vve-check')]"
-url_xpath = './/h3/a/@href'
-title_xpath = './/div[@class="yt-lockup-content"]/h3/a'
-content_xpath = './/div[@class="yt-lockup-content"]/div[@class="yt-lockup-description yt-ui-ellipsis yt-ui-ellipsis-2"]'
-
-
-# returns extract_text on the first result selected by the xpath or None
-def extract_text_from_dom(result, xpath):
- r = result.xpath(xpath)
- if len(r) > 0:
- return extract_text(r[0])
- return None
-
# do search-request
def request(query, params):
@@ -63,27 +50,38 @@ def request(query, params):
def response(resp):
results = []
- dom = html.fromstring(resp.text)
-
- # parse results
- for result in dom.xpath(results_xpath):
- videoid = list_get(result.xpath('@data-context-item-id'), 0)
- if videoid is not None:
- url = base_youtube_url + videoid
- thumbnail = 'https://i.ytimg.com/vi/' + videoid + '/hqdefault.jpg'
-
- title = extract_text_from_dom(result, title_xpath) or videoid
- content = extract_text_from_dom(result, content_xpath)
-
- embedded = embedded_url.format(videoid=videoid)
-
- # append result
- results.append({'url': url,
- 'title': title,
- 'content': content,
- 'template': 'videos.html',
- 'embedded': embedded,
- 'thumbnail': thumbnail})
+ results_data = resp.text[resp.text.find('ytInitialData'):]
+ results_data = results_data[results_data.find('{'):results_data.find(';\n')]
+
+ results_json = loads(results_data) if results_data else {}
+ sections = results_json.get('contents', {})\
+ .get('twoColumnSearchResultsRenderer', {})\
+ .get('primaryContents', {})\
+ .get('sectionListRenderer', {})\
+ .get('contents', [])
+
+ for section in sections:
+ for video_container in section.get('itemSectionRenderer', {}).get('contents', []):
+ video = video_container.get('videoRenderer', {})
+ videoid = video.get('videoId')
+ if videoid is not None:
+ url = base_youtube_url + videoid
+ thumbnail = 'https://i.ytimg.com/vi/' + videoid + '/hqdefault.jpg'
+ title = video.get('title', {}).get('simpleText', videoid)
+ description_snippet = video.get('descriptionSnippet', {})
+ if 'runs' in description_snippet:
+ content = reduce(lambda a, b: a + b.get('text', ''), description_snippet.get('runs'), '')
+ else:
+ content = description_snippet.get('simpleText', '')
+ embedded = embedded_url.format(videoid=videoid)
+
+ # append result
+ results.append({'url': url,
+ 'title': title,
+ 'content': content,
+ 'template': 'videos.html',
+ 'embedded': embedded,
+ 'thumbnail': thumbnail})
# return results
return results
diff --git a/searx/query.py b/searx/query.py
index 0b5143d39..f76bd91d3 100644
--- a/searx/query.py
+++ b/searx/query.py
@@ -113,19 +113,16 @@ class RawTextQuery(object):
parse_next = True
engine_name = engine_shortcuts[prefix]
if engine_name in engines:
- for engine_category in engines[engine_name].categories:
- self.engines.append({'category': engine_category,
- 'name': engine_name,
- 'from_bang': True})
+ self.engines.append({'category': 'none',
+ 'name': engine_name,
+ 'from_bang': True})
# check if prefix is equal with engine name
elif prefix in engines:
parse_next = True
- if prefix in engines:
- for engine_category in engines[prefix].categories:
- self.engines.append({'category': engine_category,
- 'name': prefix,
- 'from_bang': True})
+ self.engines.append({'category': 'none',
+ 'name': prefix,
+ 'from_bang': True})
# check if prefix is equal with categorie name
elif prefix in categories: