diff options
author | Adam Tauber <asciimoo@gmail.com> | 2015-06-02 17:26:27 -0400 |
---|---|---|
committer | Adam Tauber <asciimoo@gmail.com> | 2015-06-02 17:26:27 -0400 |
commit | 8ce604385901ddfe15ef030188970cbb9b807c6a (patch) | |
tree | 1c58f22a74534a64720aecdbf0570e26dac81563 /searx/engines/youtube_noapi.py | |
parent | 9d10277c22664933c676e8541b89072b38c34b17 (diff) | |
parent | e0774c849c48373c7a49515d5d769c5868596494 (diff) | |
download | searxng-8ce604385901ddfe15ef030188970cbb9b807c6a.tar.gz searxng-8ce604385901ddfe15ef030188970cbb9b807c6a.zip |
Merge pull request #346 from Cqoicebordel/youtube
Adds engines : Youtube with or without API and multiple Qwant
Diffstat (limited to 'searx/engines/youtube_noapi.py')
-rw-r--r-- | searx/engines/youtube_noapi.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/searx/engines/youtube_noapi.py b/searx/engines/youtube_noapi.py new file mode 100644 index 000000000..f78e43f0f --- /dev/null +++ b/searx/engines/youtube_noapi.py @@ -0,0 +1,72 @@ +# Youtube (Videos) +# +# @website https://www.youtube.com/ +# @provide-api yes (https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list) +# +# @using-api no +# @results HTML +# @stable no +# @parse url, title, content, publishedDate, thumbnail, embedded + +from urllib import quote_plus +from lxml import html +from searx.engines.xpath import extract_text + +# engine dependent config +categories = ['videos', 'music'] +paging = True +language_support = False + +# search-url +base_url = 'https://www.youtube.com/results' +search_url = base_url + '?search_query={query}&page={page}' + +embedded_url = '<iframe width="540" height="304" ' +\ + 'data-src="//www.youtube-nocookie.com/embed/{videoid}" ' +\ + 'frameborder="0" allowfullscreen></iframe>' + +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"]' + + +# do search-request +def request(query, params): + params['url'] = search_url.format(query=quote_plus(query), + page=params['pageno']) + + return params + + +# get response from search-request +def response(resp): + results = [] + + dom = html.fromstring(resp.text) + + # parse results + for result in dom.xpath(results_xpath): + videoid = result.xpath('@data-context-item-id')[0] + + url = base_youtube_url + videoid + thumbnail = 'https://i.ytimg.com/vi/' + videoid + '/hqdefault.jpg' + + title = extract_text(result.xpath(title_xpath)[0]) + content = extract_text(result.xpath(content_xpath)[0]) + + 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 |