From 86a8e181bf0dd88c7a6b974aea45e186c6b24545 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sat, 30 Dec 2017 15:21:43 -0600 Subject: [PATCH] Add search --- src/invidious.cr | 25 +++++++++++++++++++++++++ src/views/search.ecr | 3 +++ 2 files changed, 28 insertions(+) create mode 100644 src/views/search.ecr diff --git a/src/invidious.cr b/src/invidious.cr index 9ebd82a69..630ed5461 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -213,6 +213,31 @@ get "/watch/:video_id" do |env| templated "watch" end +get "/search" do |env| + query = URI.escape(env.params.query["q"]) + client = HTTP::Client.new("www.youtube.com", 443, context) + results_html = client.get("https://www.youtube.com/results?q=#{query}&page=1").body + html = XML.parse(results_html) + + videos = html.xpath_nodes(%q(//div[@class="style-scope ytd-item-section-renderer"]/ytd-video-renderer)) + channels = html.xpath_nodes(%q(//div[@class="style-scope ytd-item-section-renderer"]/ytd-channel-renderer)) + + if videos.empty? + videos = html.xpath_nodes(%q(//div[contains(@class,"yt-lockup-video")]/div/div[contains(@class,"yt-lockup-thumbnail")]/a/@href)) + channels = html.xpath_nodes(%q(//div[contains(@class,"yt-lockup-channel")]/div/div[contains(@class,"yt-lockup-thumbnail")]/a/@href)) + end + + templated "search" +end + +error 404 do |env| + templated "index" +end + +error 500 do |env| + templated "index" +end + public_folder "assets" Kemal.run diff --git a/src/views/search.ecr b/src/views/search.ecr new file mode 100644 index 000000000..3590bc7c7 --- /dev/null +++ b/src/views/search.ecr @@ -0,0 +1,3 @@ +<% videos.each do |video| %> +

<%= video.content %>

+<% end %> \ No newline at end of file