Invidious is an alternative front-end to YouTube
Add '/api/v1/search'
Omar Rothfcb4f4cddb7f
diff --git a/src/invidious.cr b/src/invidious.crindex bc429fbf..4628dd86 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -81,16 +81,15 @@ crawl_threads.times doids = Deque(String).newrandom = Random.new- client = make_client(YT_URL)- search(random.base64(3), client) do |id|- ids << id+ search(random.base64(3)).each do |video|+ ids << video.idendloop doclient = make_client(YT_URL)if ids.empty?- search(random.base64(3), client) do |id|- ids << id+ search(random.base64(3)).each do |video|+ ids << video.idendend@@ -1267,13 +1266,65 @@ get "/api/v1/channels/:ucid/videos" do |env|videosend+get "/api/v1/search" do |env|+ if env.params.query["q"]?+ query = env.params.query["q"]+ else+ next env.redirect "/"+ end++ page = env.params.query["page"]?.try &.to_i?+ page ||= 1++ client = make_client(YT_URL)+ html = client.get("/results?q=#{URI.escape(query)}&page=#{page}&sp=EgIQAVAU&disable_polymer=1").body+ html = XML.parse_html(html)++ results = JSON.build do |json|+ json.array do+ html.xpath_nodes(%q(//ol[@class="item-section"]/li)).each do |item|+ anchor = item.xpath_node(%q(.//h3[contains(@class,"yt-lockup-title")]/a)).not_nil!+ if anchor["href"].starts_with? "https://www.googleadservices.com"+ next+ end++ title = anchor.content.strip+ video_id = anchor["href"].lchop("/watch?v=")++ anchor = item.xpath_node(%q(.//div[contains(@class, "yt-lockup-byline")]/a)).not_nil!+ author = anchor.content+ author_url = anchor["href"]++ published = item.xpath_node(%q(.//div[contains(@class,"yt-lockup-meta")]/ul/li[1]))if !publishednextendpublished = published.content+ if published.ends_with? "watching"+ next+ endpublished = decode_date(published)- length_seconds = item.xpath_node(%q(.//span[@class="video-time"]/span)).not_nil!.content+ view_count = item.xpath_node(%q(.//div[contains(@class,"yt-lockup-meta")]/ul/li[2])).not_nil!+ view_count = view_count.content.rchop(" views")+ if view_count = "No"+ view_count = 0+ else+ view_count = view_count.delete(",").to_i+ end++ descriptionHtml = item.xpath_node(%q(.//div[contains(@class, "yt-lockup-description")]))+ if !descriptionHtml+ description = ""+ descriptionHtml = ""+ else+ descriptionHtml = descriptionHtml.to_s+ description = descriptionHtml.gsub("<br>", "\n")+ description = description.gsub("<br/>", "\n")+ description = XML.parse_html(description).content.strip("\n ")+ end++ length_seconds = item.xpath_node(%q(.//span[@class="video-time"])).not_nil!.contentlength_seconds = length_seconds.split(":").map { |a| a.to_i }length_seconds = [0] * (3 - length_seconds.size) + length_secondslength_seconds = Time::Span.new(length_seconds[0], length_seconds[1], length_seconds[2])@@ -1282,6 +1333,9 @@ endjson.field "title", titlejson.field "videoId", video_id+ json.field "author", author+ json.field "authorUrl", author_url+json.field "videoThumbnails" dojson.object doqualities = [{name: "default", url: "default", width: 120, height: 90},@@ -1312,7 +1366,7 @@ endendenv.response.content_type = "application/json"- videos+ resultsendget "/embed/:id" do |env|@@ -1471,39 +1525,7 @@ get "/search" do |env|page = env.params.query["page"]?.try &.to_i?page ||= 1- client = make_client(YT_URL)- html = client.get("/results?q=#{URI.escape(query)}&page=#{page}&sp=EgIQAVAU").body- html = XML.parse_html(html)-- videos = [] of ChannelVideo-- html.xpath_nodes(%q(//ol[@class="item-section"]/li)).each do |item|- root = item.xpath_node(%q(div[contains(@class,"yt-lockup-video")]/div))- if root- id = root.xpath_node(%q(div[contains(@class,"yt-lockup-thumbnail")]/a/@href))- if id- id = id.content.lchop("/watch?v=")- end- id ||= ""-- title = root.xpath_node(%q(div[@class="yt-lockup-content"]/h3/a))- if title- title = title.content- end- title ||= ""-- author = root.xpath_node(%q(div[@class="yt-lockup-content"]/div/a))- if author- ucid = author["href"].rpartition("/")[-1]- author = author.content- end- author ||= ""- ucid ||= ""-- video = ChannelVideo.new(id, title, Time.now, Time.now, ucid, author)- videos << video- end- end+ videos = search(query, page)templated "search"enddiff --git a/src/invidious/helpers.cr b/src/invidious/helpers.crindex aa0939f1..68699fc8 100644--- a/src/invidious/helpers.cr+++ b/src/invidious/helpers.cr@@ -345,19 +345,42 @@ def get_video(id, db, refresh = true)return videoend-def search(query, client, &block)- html = client.get("/results?q=#{query}&sp=EgIQAVAU&disable_polymer=1").body+def search(query, page = 1)+ client = make_client(YT_URL)+ html = client.get("/results?q=#{URI.escape(query)}&page=#{page}&sp=EgIQAVAU").bodyhtml = XML.parse_html(html)+ videos = [] of ChannelVideo+html.xpath_nodes(%q(//ol[@class="item-section"]/li)).each do |item|root = item.xpath_node(%q(div[contains(@class,"yt-lockup-video")]/div))if root- link = root.xpath_node(%q(div[contains(@class,"yt-lockup-thumbnail")]/a/@href))- if link- yield link.content.split("=")[1]+ id = root.xpath_node(%q(div[contains(@class,"yt-lockup-thumbnail")]/a/@href))+ if id+ id = id.content.lchop("/watch?v=")+ end+ id ||= ""++ title = root.xpath_node(%q(div[@class="yt-lockup-content"]/h3/a))+ if title+ title = title.contentend+ title ||= ""++ author = root.xpath_node(%q(div[@class="yt-lockup-content"]/div/a))+ if author+ ucid = author["href"].rpartition("/")[-1]+ author = author.content+ end+ author ||= ""+ ucid ||= ""++ video = ChannelVideo.new(id, title, Time.now, Time.now, ucid, author)+ videos << videoendend++ return videosenddef splice(a, b)