Invidious is an alternative front-end to YouTube
Make HOST_URL constant
Omar Rothc1cbdae5ee52
diff --git a/src/invidious.cr b/src/invidious.crindex 6b408dc6..958f95f7 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -50,6 +50,7 @@ PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com")REDDIT_URL = URI.parse("https://www.reddit.com")TEXTCAPTCHA_URL = URI.parse("https://textcaptcha.com")YT_URL = URI.parse("https://www.youtube.com")+HOST_URL = make_host_url(CONFIG, Kemal.config)CHARS_SAFE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"TEST_IDS = {"AgbeGFYluEA", "BaW_jenozKc", "a9LDPn-MO4I", "ddFvjfvPnqk", "iqKdEhx-dD4"}@@ -202,10 +203,11 @@ spawn doendend-decrypt_function = [] of {SigProc, Int32}+DECRYPT_FUNCTION = [] of {SigProc, Int32}spawn doupdate_decrypt_function do |function|- decrypt_function = function+ DECRYPT_FUNCTION.clear+ function.each { |i| DECRYPT_FUNCTION << i }endend@@ -1351,16 +1353,14 @@ get "/opensearch.xml" do |env|locale = LOCALES[env.get("preferences").as(Preferences).locale]?env.response.content_type = "application/opensearchdescription+xml"- host = make_host_url(config, Kemal.config)-XML.build(indent: " ", encoding: "UTF-8") do |xml|xml.element("OpenSearchDescription", xmlns: "http://a9.com/-/spec/opensearch/1.1/") doxml.element("ShortName") { xml.text "Invidious" }xml.element("LongName") { xml.text "Invidious Search" }xml.element("Description") { xml.text "Search for videos, channels, and playlists on Invidious" }xml.element("InputEncoding") { xml.text "UTF-8" }- xml.element("Image", width: 48, height: 48, type: "image/x-icon") { xml.text "#{host}/favicon.ico" }- xml.element("Url", type: "text/html", method: "get", template: "#{host}/search?q={searchTerms}")+ xml.element("Image", width: 48, height: 48, type: "image/x-icon") { xml.text "#{HOST_URL}/favicon.ico" }+ xml.element("Url", type: "text/html", method: "get", template: "#{HOST_URL}/search?q={searchTerms}")endendend@@ -2473,8 +2473,6 @@ get "/subscription_manager" do |env|subscriptions.sort_by! { |channel| channel.author.downcase }if action_takeout- host_url = make_host_url(config, Kemal.config)-if format == "json"env.response.content_type = "application/json"env.response.headers["content-disposition"] = "attachment"@@ -2500,7 +2498,7 @@ get "/subscription_manager" do |env|if format == "newpipe"xmlUrl = "https://www.youtube.com/feeds/videos.xml?channel_id=#{channel.id}"else- xmlUrl = "#{host_url}/feed/channel/#{channel.id}"+ xmlUrl = "#{HOST_URL}/feed/channel/#{channel.id}"endxml.element("outline", text: channel.author, title: channel.author,@@ -3179,25 +3177,23 @@ get "/feed/channel/:ucid" do |env|)end- host_url = make_host_url(config, Kemal.config)-XML.build(indent: " ", encoding: "UTF-8") do |xml|xml.element("feed", "xmlns:yt": "http://www.youtube.com/xml/schemas/2015","xmlns:media": "http://search.yahoo.com/mrss/", xmlns: "http://www.w3.org/2005/Atom","xml:lang": "en-US") do- xml.element("link", rel: "self", href: "#{host_url}#{env.request.resource}")+ xml.element("link", rel: "self", href: "#{HOST_URL}#{env.request.resource}")xml.element("id") { xml.text "yt:channel:#{channel.ucid}" }xml.element("yt:channelId") { xml.text channel.ucid }xml.element("title") { xml.text channel.author }- xml.element("link", rel: "alternate", href: "#{host_url}/channel/#{channel.ucid}")+ xml.element("link", rel: "alternate", href: "#{HOST_URL}/channel/#{channel.ucid}")xml.element("author") doxml.element("name") { xml.text channel.author }- xml.element("uri") { xml.text "#{host_url}/channel/#{channel.ucid}" }+ xml.element("uri") { xml.text "#{HOST_URL}/channel/#{channel.ucid}" }endvideos.each do |video|- video.to_xml(host_url, channel.auto_generated, params, xml)+ video.to_xml(channel.auto_generated, params, xml)endendend@@ -3231,19 +3227,18 @@ get "/feed/private" do |env|params = HTTP::Params.parse(env.params.query["params"]? || "")videos, notifications = get_subscription_feed(PG_DB, user, max_results, page)- host_url = make_host_url(config, Kemal.config)XML.build(indent: " ", encoding: "UTF-8") do |xml|xml.element("feed", "xmlns:yt": "http://www.youtube.com/xml/schemas/2015","xmlns:media": "http://search.yahoo.com/mrss/", xmlns: "http://www.w3.org/2005/Atom","xml:lang": "en-US") do- xml.element("link", "type": "text/html", rel: "alternate", href: "#{host_url}/feed/subscriptions")+ xml.element("link", "type": "text/html", rel: "alternate", href: "#{HOST_URL}/feed/subscriptions")xml.element("link", "type": "application/atom+xml", rel: "self",- href: "#{host_url}#{env.request.resource}")+ href: "#{HOST_URL}#{env.request.resource}")xml.element("title") { xml.text translate(locale, "Invidious Private Feed for `x`", user.email) }(notifications + videos).each do |video|- video.to_xml(locale, host_url, params, xml)+ video.to_xml(locale, params, xml)endendend@@ -3257,8 +3252,6 @@ get "/feed/playlist/:plid" do |env|plid = env.params.url["plid"]params = HTTP::Params.parse(env.params.query["params"]? || "")-- host_url = make_host_url(config, Kemal.config)path = env.request.pathif plid.starts_with? "IV"@@ -3269,18 +3262,18 @@ get "/feed/playlist/:plid" do |env|xml.element("feed", "xmlns:yt": "http://www.youtube.com/xml/schemas/2015","xmlns:media": "http://search.yahoo.com/mrss/", xmlns: "http://www.w3.org/2005/Atom","xml:lang": "en-US") do- xml.element("link", rel: "self", href: "#{host_url}#{env.request.resource}")+ xml.element("link", rel: "self", href: "#{HOST_URL}#{env.request.resource}")xml.element("id") { xml.text "iv:playlist:#{plid}" }xml.element("iv:playlistId") { xml.text plid }xml.element("title") { xml.text playlist.title }- xml.element("link", rel: "alternate", href: "#{host_url}/playlist?list=#{plid}")+ xml.element("link", rel: "alternate", href: "#{HOST_URL}/playlist?list=#{plid}")xml.element("author") doxml.element("name") { xml.text playlist.author }endvideos.each do |video|- video.to_xml(host_url, false, xml)+ video.to_xml(false, xml)endendend@@ -3299,7 +3292,7 @@ get "/feed/playlist/:plid" do |env|when "url", "href"full_path = URI.parse(node[attribute.name]).full_pathquery_string_opt = full_path.starts_with?("/watch?v=") ? "&#{params}" : ""- node[attribute.name] = "#{host_url}#{full_path}#{query_string_opt}"+ node[attribute.name] = "#{HOST_URL}#{full_path}#{query_string_opt}"else nil # Skipendend@@ -3308,7 +3301,7 @@ get "/feed/playlist/:plid" do |env|document = document.to_xml(options: XML::SaveOptions::NO_DECL)document.scan(/<uri>(?<url>[^<]+)<\/uri>/).each do |match|- content = "#{host_url}#{URI.parse(match["url"]).full_path}"+ content = "#{HOST_URL}#{URI.parse(match["url"]).full_path}"document = document.gsub(match[0], "<uri>#{content}</uri>")end@@ -3684,7 +3677,7 @@ get "/channel/:ucid/community" do |env|endbegin- items = JSON.parse(fetch_channel_community(ucid, continuation, locale, config, Kemal.config, "json", thin_mode))+ items = JSON.parse(fetch_channel_community(ucid, continuation, locale, "json", thin_mode))rescue exenv.response.status_code = 500error_message = ex.message@@ -3737,7 +3730,6 @@ get "/api/v1/storyboards/:id" do |env|endstoryboards = video.storyboards-width = env.params.query["width"]?height = env.params.query["height"]?@@ -3745,7 +3737,7 @@ get "/api/v1/storyboards/:id" do |env|response = JSON.build do |json|json.object dojson.field "storyboards" do- generate_storyboards(json, id, storyboards, config, Kemal.config)+ generate_storyboards(json, id, storyboards)endendend@@ -3775,8 +3767,7 @@ get "/api/v1/storyboards/:id" do |env|end_time = storyboard[:interval].millisecondsstoryboard[:storyboard_count].times do |i|- host_url = make_host_url(config, Kemal.config)- url = storyboard[:url].gsub("$M", i).gsub("https://i9.ytimg.com", host_url)+ url = storyboard[:url].gsub("$M", i).gsub("https://i9.ytimg.com", HOST_URL)storyboard[:storyboard_height].times do |j|storyboard[:storyboard_width].times do |k|@@ -4099,7 +4090,7 @@ get "/api/v1/videos/:id" do |env|next error_messageend- video.to_json(locale, config, Kemal.config, decrypt_function)+ video.to_json(locale)endget "/api/v1/trending" do |env|@@ -4121,7 +4112,7 @@ get "/api/v1/trending" do |env|videos = JSON.build do |json|json.array dotrending.each do |video|- video.to_json(locale, config, Kemal.config, json)+ video.to_json(locale, json)endendend@@ -4137,7 +4128,7 @@ get "/api/v1/popular" do |env|JSON.build do |json|json.array dopopular_videos.each do |video|- video.to_json(locale, config, Kemal.config, json)+ video.to_json(locale, json)endendend@@ -4178,7 +4169,7 @@ get "/api/v1/channels/:ucid" do |env|count = 0elsebegin- videos, count = get_60_videos(channel.ucid, channel.author, page, channel.auto_generated, sort_by)+ count, videos = get_60_videos(channel.ucid, channel.author, page, channel.auto_generated, sort_by)rescue exerror_message = {"error" => ex.message}.to_jsonenv.response.status_code = 500@@ -4247,7 +4238,7 @@ get "/api/v1/channels/:ucid" do |env|json.field "latestVideos" dojson.array dovideos.each do |video|- video.to_json(locale, config, Kemal.config, json)+ video.to_json(locale, json)endendend@@ -4308,7 +4299,7 @@ endendbegin- videos, count = get_60_videos(channel.ucid, channel.author, page, channel.auto_generated, sort_by)+ count, videos = get_60_videos(channel.ucid, channel.author, page, channel.auto_generated, sort_by)rescue exerror_message = {"error" => ex.message}.to_jsonenv.response.status_code = 500@@ -4318,7 +4309,7 @@ endJSON.build do |json|json.array dovideos.each do |video|- video.to_json(locale, config, Kemal.config, json)+ video.to_json(locale, json)endendend@@ -4344,7 +4335,7 @@ endJSON.build do |json|json.array dovideos.each do |video|- video.to_json(locale, config, Kemal.config, json)+ video.to_json(locale, json)endendend@@ -4359,9 +4350,9 @@ enducid = env.params.url["ucid"]continuation = env.params.query["continuation"]?- sort_by = env.params.query["sort"]?.try &.downcase- sort_by ||= env.params.query["sort_by"]?.try &.downcase- sort_by ||= "last"+ sort_by = env.params.query["sort"]?.try &.downcase ||+ env.params.query["sort_by"]?.try &.downcase ||+ "last"beginchannel = get_about_info(ucid, locale)@@ -4383,9 +4374,7 @@ endjson.field "playlists" dojson.array doitems.each do |item|- if item.is_a?(SearchPlaylist)- item.to_json(locale, config, Kemal.config, json)- end+ item.to_json(locale, json) if item.is_a?(SearchPlaylist)endendend@@ -4414,7 +4403,7 @@ end# sort_by = env.params.query["sort_by"]?.try &.downcasebegin- fetch_channel_community(ucid, continuation, locale, config, Kemal.config, format, thin_mode)+ fetch_channel_community(ucid, continuation, locale, format, thin_mode)rescue exenv.response.status_code = 400error_message = {"error" => ex.message}.to_json@@ -4440,7 +4429,7 @@ get "/api/v1/channels/search/:ucid" do |env|JSON.build do |json|json.array dosearch_results.each do |item|- item.to_json(locale, config, Kemal.config, json)+ item.to_json(locale, json)endendend@@ -4485,7 +4474,7 @@ get "/api/v1/search" do |env|JSON.build do |json|json.array dosearch_results.each do |item|- item.to_json(locale, config, Kemal.config, json)+ item.to_json(locale, json)endendend@@ -4562,7 +4551,7 @@ endnext error_messageend- response = playlist.to_json(offset, locale, config, Kemal.config, continuation: continuation)+ response = playlist.to_json(offset, locale, continuation: continuation)if format == "html"response = JSON.parse(response)@@ -4626,7 +4615,7 @@ get "/api/v1/mixes/:rdid" do |env|json.field "videoThumbnails" dojson.array do- generate_thumbnails(json, video.id, config, Kemal.config)+ generate_thumbnails(json, video.id)endend@@ -4661,7 +4650,7 @@ get "/api/v1/auth/notifications" do |env|topics = env.params.query["topics"]?.try &.split(",").uniq.first(1000)topics ||= [] of String- create_notification_stream(env, config, Kemal.config, decrypt_function, topics, connection_channel)+ create_notification_stream(env, topics, connection_channel)endpost "/api/v1/auth/notifications" do |env|@@ -4670,7 +4659,7 @@ post "/api/v1/auth/notifications" do |env|topics = env.params.body["topics"]?.try &.split(",").uniq.first(1000)topics ||= [] of String- create_notification_stream(env, config, Kemal.config, decrypt_function, topics, connection_channel)+ create_notification_stream(env, topics, connection_channel)endget "/api/v1/auth/preferences" do |env|@@ -4714,7 +4703,7 @@ get "/api/v1/auth/feed" do |env|json.field "notifications" dojson.array donotifications.each do |video|- video.to_json(locale, config, Kemal.config, json)+ video.to_json(locale, json)endendend@@ -4722,7 +4711,7 @@ get "/api/v1/auth/feed" do |env|json.field "videos" dojson.array dovideos.each do |video|- video.to_json(locale, config, Kemal.config, json)+ video.to_json(locale, json)endendend@@ -4794,7 +4783,7 @@ get "/api/v1/auth/playlists" do |env|JSON.build do |json|json.array doplaylists.each do |playlist|- playlist.to_json(0, locale, config, Kemal.config, json)+ playlist.to_json(0, locale, json)endendend@@ -4825,10 +4814,8 @@ post "/api/v1/auth/playlists" do |env|next error_messageend- host_url = make_host_url(config, Kemal.config)-playlist = create_playlist(PG_DB, title, privacy, user)- env.response.headers["Location"] = "#{host_url}/api/v1/auth/playlists/#{playlist.id}"+ env.response.headers["Location"] = "#{HOST_URL}/api/v1/auth/playlists/#{playlist.id}"env.response.status_code = 201{"title" => title,@@ -4958,11 +4945,9 @@ post "/api/v1/auth/playlists/:plid/videos" do |env|PG_DB.exec("INSERT INTO playlist_videos VALUES (#{args})", args: video_array)PG_DB.exec("UPDATE playlists SET index = array_append(index, $1), video_count = video_count + 1, updated = $2 WHERE id = $3", playlist_video.index, Time.utc, plid)- host_url = make_host_url(config, Kemal.config)-- env.response.headers["Location"] = "#{host_url}/api/v1/auth/playlists/#{plid}/videos/#{playlist_video.index.to_u64.to_s(16).upcase}"+ env.response.headers["Location"] = "#{HOST_URL}/api/v1/auth/playlists/#{plid}/videos/#{playlist_video.index.to_u64.to_s(16).upcase}"env.response.status_code = 201- playlist_video.to_json(locale, config, Kemal.config, index: playlist.index.size)+ playlist_video.to_json(locale, index: playlist.index.size)enddelete "/api/v1/auth/playlists/:plid/videos/:index" do |env|@@ -5251,12 +5236,10 @@ get "/api/manifest/hls_variant/*" do |env|env.response.content_type = "application/x-mpegURL"env.response.headers.add("Access-Control-Allow-Origin", "*")- host_url = make_host_url(config, Kemal.config)-- manifest = manifest.body+ manifest = response.bodyif local- manifest = manifest.gsub("https://www.youtube.com", host_url)+ manifest = manifest.gsub("https://www.youtube.com", HOST_URL)manifest = manifest.gsub("index.m3u8", "index.m3u8?local=true")end@@ -5276,9 +5259,7 @@ get "/api/manifest/hls_playlist/*" do |env|env.response.content_type = "application/x-mpegURL"env.response.headers.add("Access-Control-Allow-Origin", "*")- host_url = make_host_url(config, Kemal.config)-- manifest = manifest.body+ manifest = response.bodyif localmanifest = manifest.gsub(/^https:\/\/r\d---.{11}\.c\.youtube\.com[^\n]*/m) do |match|@@ -5313,7 +5294,7 @@ get "/api/manifest/hls_playlist/*" do |env|raw_params["local"] = "true"- "#{host_url}/videoplayback?#{raw_params}"+ "#{HOST_URL}/videoplayback?#{raw_params}"endend@@ -5784,7 +5765,7 @@ get "/vi/:id/:name" do |env|headers = HTTP::Headers{":authority" => "i.ytimg.com"}if name == "maxres.jpg"- build_thumbnails(id, config, Kemal.config).each do |thumb|+ build_thumbnails(id).each do |thumb|if YT_POOL.client &.head("/vi/#{id}/#{thumb[:url]}.jpg", headers).status_code == 200name = thumb[:url] + ".jpg"breakdiff --git a/src/invidious/channels.cr b/src/invidious/channels.crindex afc1528e..f1a57eee 100644--- a/src/invidious/channels.cr+++ b/src/invidious/channels.cr@@ -9,14 +9,14 @@ struct InvidiousChannelendstruct ChannelVideo- def to_json(locale, config, kemal_config, json : JSON::Builder)+ def to_json(locale, json : JSON::Builder)json.object dojson.field "type", "shortVideo"json.field "title", self.titlejson.field "videoId", self.idjson.field "videoThumbnails" do- generate_thumbnails(json, self.id, config, Kemal.config)+ generate_thumbnails(json, self.id)endjson.field "lengthSeconds", self.length_seconds@@ -31,17 +31,17 @@ struct ChannelVideoendend- def to_json(locale, config, kemal_config, json : JSON::Builder | Nil = nil)+ def to_json(locale, json : JSON::Builder | Nil = nil)if json- to_json(locale, config, kemal_config, json)+ to_json(locale, json)elseJSON.build do |json|- to_json(locale, config, kemal_config, json)+ to_json(locale, json)endendend- def to_xml(locale, host_url, query_params, xml : XML::Builder)+ def to_xml(locale, query_params, xml : XML::Builder)query_params["v"] = self.idxml.element("entry") do@@ -49,17 +49,17 @@ struct ChannelVideoxml.element("yt:videoId") { xml.text self.id }xml.element("yt:channelId") { xml.text self.ucid }xml.element("title") { xml.text self.title }- xml.element("link", rel: "alternate", href: "#{host_url}/watch?#{query_params}")+ xml.element("link", rel: "alternate", href: "#{HOST_URL}/watch?#{query_params}")xml.element("author") doxml.element("name") { xml.text self.author }- xml.element("uri") { xml.text "#{host_url}/channel/#{self.ucid}" }+ xml.element("uri") { xml.text "#{HOST_URL}/channel/#{self.ucid}" }endxml.element("content", type: "xhtml") doxml.element("div", xmlns: "http://www.w3.org/1999/xhtml") do- xml.element("a", href: "#{host_url}/watch?#{query_params}") do- xml.element("img", src: "#{host_url}/vi/#{self.id}/mqdefault.jpg")+ xml.element("a", href: "#{HOST_URL}/watch?#{query_params}") do+ xml.element("img", src: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg")endendend@@ -69,18 +69,18 @@ struct ChannelVideoxml.element("media:group") doxml.element("media:title") { xml.text self.title }- xml.element("media:thumbnail", url: "#{host_url}/vi/#{self.id}/mqdefault.jpg",+ xml.element("media:thumbnail", url: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg",width: "320", height: "180")endendend- def to_xml(locale, config, kemal_config, xml : XML::Builder | Nil = nil)+ def to_xml(locale, xml : XML::Builder | Nil = nil)if xml- to_xml(locale, config, kemal_config, xml)+ to_xml(locale, xml)elseXML.build do |xml|- to_xml(locale, config, kemal_config, xml)+ to_xml(locale, xml)endendend@@ -557,7 +557,7 @@ def extract_channel_playlists_cursor(url, auto_generated)end# TODO: Add "sort_by"-def fetch_channel_community(ucid, continuation, locale, config, kemal_config, format, thin_mode)+def fetch_channel_community(ucid, continuation, locale, format, thin_mode)response = YT_POOL.client &.get("/channel/#{ucid}/community?gl=US&hl=en")if response.status_code != 200response = YT_POOL.client &.get("/user/#{ucid}/community?gl=US&hl=en")@@ -708,7 +708,7 @@ def fetch_channel_community(ucid, continuation, locale, config, kemal_config, fojson.field "title", attachment["title"]["simpleText"].as_sjson.field "videoId", video_idjson.field "videoThumbnails" do- generate_thumbnails(json, video_id, config, kemal_config)+ generate_thumbnails(json, video_id)endjson.field "lengthSeconds", decode_length_seconds(attachment["lengthText"]["simpleText"].as_s)@@ -956,33 +956,17 @@ def get_about_info(ucid, locale)enddef get_60_videos(ucid, author, page, auto_generated, sort_by = "newest")- count = 0videos = [] of SearchVideo2.times do |i|url = produce_channel_videos_url(ucid, page * 2 + (i - 1), auto_generated: auto_generated, sort_by: sort_by)- response = YT_POOL.client &.get(url)- json = JSON.parse(response.body)-- if json["content_html"]? && !json["content_html"].as_s.empty?- document = XML.parse_html(json["content_html"].as_s)- nodeset = document.xpath_nodes(%q(//li[contains(@class, "feed-item-container")]))-- if !json["load_more_widget_html"]?.try &.as_s.empty?- count += 30- end-- if auto_generated- videos += extract_videos(nodeset)- else- videos += extract_videos(nodeset, ucid, author)- end- else- break- end+ response = YT_POOL.client &.get(url, headers)+ initial_data = JSON.parse(response.body).as_a.find &.["response"]?+ break if !initial_data+ videos.concat extract_videos(initial_data.as_h)end- return videos, count+ return videos.size, videosenddef get_latest_videos(ucid)diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.crindex f6ba33cd..b572ee1c 100644--- a/src/invidious/helpers/helpers.cr+++ b/src/invidious/helpers/helpers.cr@@ -727,13 +727,10 @@ def cache_annotation(db, id, annotations)endend- if has_legacy_annotations- # TODO: Update on conflict?- db.exec("INSERT INTO annotations VALUES ($1, $2) ON CONFLICT DO NOTHING", id, annotations)- end+ db.exec("INSERT INTO annotations VALUES ($1, $2) ON CONFLICT DO NOTHING", id, annotations) if has_legacy_annotationsend-def create_notification_stream(env, config, kemal_config, decrypt_function, topics, connection_channel)+def create_notification_stream(env, topics, connection_channel)connection = Channel(PQ::Notification).new(8)connection_channel.send({true, connection})@@ -753,7 +750,7 @@ def create_notification_stream(env, config, kemal_config, decrypt_function, topivideo = get_video(video_id, PG_DB)video.published = published- response = JSON.parse(video.to_json(locale, config, kemal_config, decrypt_function))+ response = JSON.parse(video.to_json(locale))if fields_text = env.params.query["fields"]?begin@@ -787,7 +784,7 @@ def create_notification_stream(env, config, kemal_config, decrypt_function, topiwhen .match(/UC[A-Za-z0-9_-]{22}/)PG_DB.query_all("SELECT * FROM channel_videos WHERE ucid = $1 AND published > $2 ORDER BY published DESC LIMIT 15",topic, Time.unix(since.not_nil!), as: ChannelVideo).each do |video|- response = JSON.parse(video.to_json(locale, config, Kemal.config))+ response = JSON.parse(video.to_json(locale))if fields_text = env.params.query["fields"]?begin@@ -829,7 +826,7 @@ def create_notification_stream(env, config, kemal_config, decrypt_function, topivideo = get_video(video_id, PG_DB)video.published = Time.unix(published)- response = JSON.parse(video.to_json(locale, config, Kemal.config, decrypt_function))+ response = JSON.parse(video.to_json(locale))if fields_text = env.params.query["fields"]?begindiff --git a/src/invidious/helpers/signatures.cr b/src/invidious/helpers/signatures.crindex f82cc8dd..0aaacd04 100644--- a/src/invidious/helpers/signatures.cr+++ b/src/invidious/helpers/signatures.cr@@ -40,12 +40,12 @@ def fetch_decrypt_function(id = "CvFH_6DNRCY")return decrypt_functionend-def decrypt_signature(fmt, op)+def decrypt_signature(fmt : Hash(String, JSON::Any))return "" if !fmt["s"]? || !fmt["sp"]?- sp = fmt["sp"]- sig = fmt["s"].split("")- op.each do |proc, value|+ sp = fmt["sp"].as_s+ sig = fmt["s"].as_s.split("")+ DECRYPT_FUNCTION.each do |proc, value|sig = proc.call(sig, value)enddiff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.crindex a4bd1d54..a39a0b16 100644--- a/src/invidious/helpers/utils.cr+++ b/src/invidious/helpers/utils.cr@@ -351,10 +351,8 @@ def subscribe_pubsub(topic, key, config)nonce = Random::Secure.hex(4)signature = "#{time}:#{nonce}"- host_url = make_host_url(config, Kemal.config)-body = {- "hub.callback" => "#{host_url}/feed/webhook/v1:#{time}:#{nonce}:#{OpenSSL::HMAC.hexdigest(:sha1, key, signature)}",+ "hub.callback" => "#{HOST_URL}/feed/webhook/v1:#{time}:#{nonce}:#{OpenSSL::HMAC.hexdigest(:sha1, key, signature)}","hub.topic" => "https://www.youtube.com/xml/feeds/videos.xml?#{topic}","hub.verify" => "async","hub.mode" => "subscribe",diff --git a/src/invidious/playlists.cr b/src/invidious/playlists.crindex 184329dc..fcf73dad 100644--- a/src/invidious/playlists.cr+++ b/src/invidious/playlists.cr@@ -1,26 +1,26 @@struct PlaylistVideo- def to_xml(host_url, auto_generated, xml : XML::Builder)+ def to_xml(auto_generated, xml : XML::Builder)xml.element("entry") doxml.element("id") { xml.text "yt:video:#{self.id}" }xml.element("yt:videoId") { xml.text self.id }xml.element("yt:channelId") { xml.text self.ucid }xml.element("title") { xml.text self.title }- xml.element("link", rel: "alternate", href: "#{host_url}/watch?v=#{self.id}")+ xml.element("link", rel: "alternate", href: "#{HOST_URL}/watch?v=#{self.id}")xml.element("author") doif auto_generatedxml.element("name") { xml.text self.author }- xml.element("uri") { xml.text "#{host_url}/channel/#{self.ucid}" }+ xml.element("uri") { xml.text "#{HOST_URL}/channel/#{self.ucid}" }elsexml.element("name") { xml.text author }- xml.element("uri") { xml.text "#{host_url}/channel/#{ucid}" }+ xml.element("uri") { xml.text "#{HOST_URL}/channel/#{ucid}" }endendxml.element("content", type: "xhtml") doxml.element("div", xmlns: "http://www.w3.org/1999/xhtml") do- xml.element("a", href: "#{host_url}/watch?v=#{self.id}") do- xml.element("img", src: "#{host_url}/vi/#{self.id}/mqdefault.jpg")+ xml.element("a", href: "#{HOST_URL}/watch?v=#{self.id}") do+ xml.element("img", src: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg")endendend@@ -29,23 +29,23 @@ struct PlaylistVideoxml.element("media:group") doxml.element("media:title") { xml.text self.title }- xml.element("media:thumbnail", url: "#{host_url}/vi/#{self.id}/mqdefault.jpg",+ xml.element("media:thumbnail", url: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg",width: "320", height: "180")endendend- def to_xml(host_url, auto_generated, xml : XML::Builder? = nil)+ def to_xml(auto_generated, xml : XML::Builder? = nil)if xml- to_xml(host_url, auto_generated, xml)+ to_xml(auto_generated, xml)elseXML.build do |json|- to_xml(host_url, auto_generated, xml)+ to_xml(auto_generated, xml)endendend- def to_json(locale, config, kemal_config, json : JSON::Builder, index : Int32?)+ def to_json(locale, json : JSON::Builder, index : Int32?)json.object dojson.field "title", self.titlejson.field "videoId", self.id@@ -55,7 +55,7 @@ struct PlaylistVideojson.field "authorUrl", "/channel/#{self.ucid}"json.field "videoThumbnails" do- generate_thumbnails(json, self.id, config, kemal_config)+ generate_thumbnails(json, self.id)endif index@@ -69,12 +69,12 @@ struct PlaylistVideoendend- def to_json(locale, config, kemal_config, json : JSON::Builder? = nil, index : Int32? = nil)+ def to_json(locale, json : JSON::Builder? = nil, index : Int32? = nil)if json- to_json(locale, config, kemal_config, json, index: index)+ to_json(locale, json, index: index)elseJSON.build do |json|- to_json(locale, config, kemal_config, json, index: index)+ to_json(locale, json, index: index)endendend@@ -93,7 +93,7 @@ struct PlaylistVideoendstruct Playlist- def to_json(offset, locale, config, kemal_config, json : JSON::Builder, continuation : String? = nil)+ def to_json(offset, locale, json : JSON::Builder, continuation : String? = nil)json.object dojson.field "type", "playlist"json.field "title", self.title@@ -130,19 +130,19 @@ struct Playlistjson.array dovideos = get_playlist_videos(PG_DB, self, offset: offset, locale: locale, continuation: continuation)videos.each_with_index do |video, index|- video.to_json(locale, config, Kemal.config, json)+ video.to_json(locale, json)endendendendend- def to_json(offset, locale, config, kemal_config, json : JSON::Builder? = nil, continuation : String? = nil)+ def to_json(offset, locale, json : JSON::Builder? = nil, continuation : String? = nil)if json- to_json(offset, locale, config, kemal_config, json, continuation: continuation)+ to_json(offset, locale, json, continuation: continuation)elseJSON.build do |json|- to_json(offset, locale, config, kemal_config, json, continuation: continuation)+ to_json(offset, locale, json, continuation: continuation)endendend@@ -172,7 +172,7 @@ enum PlaylistPrivacyendstruct InvidiousPlaylist- def to_json(offset, locale, config, kemal_config, json : JSON::Builder, continuation : String? = nil)+ def to_json(offset, locale, json : JSON::Builder, continuation : String? = nil)json.object dojson.field "type", "invidiousPlaylist"json.field "title", self.title@@ -195,19 +195,19 @@ struct InvidiousPlaylistjson.array dovideos = get_playlist_videos(PG_DB, self, offset: offset, locale: locale, continuation: continuation)videos.each_with_index do |video, index|- video.to_json(locale, config, Kemal.config, json, offset + index)+ video.to_json(locale, json, offset + index)endendendendend- def to_json(offset, locale, config, kemal_config, json : JSON::Builder? = nil, continuation : String? = nil)+ def to_json(offset, locale, json : JSON::Builder? = nil, continuation : String? = nil)if json- to_json(offset, locale, config, kemal_config, json, continuation: continuation)+ to_json(offset, locale, json, continuation: continuation)elseJSON.build do |json|- to_json(offset, locale, config, kemal_config, json, continuation: continuation)+ to_json(offset, locale, json, continuation: continuation)endendenddiff --git a/src/invidious/search.cr b/src/invidious/search.crindex e8521629..7a88f316 100644--- a/src/invidious/search.cr+++ b/src/invidious/search.cr@@ -1,5 +1,5 @@struct SearchVideo- def to_xml(host_url, auto_generated, query_params, xml : XML::Builder)+ def to_xml(auto_generated, query_params, xml : XML::Builder)query_params["v"] = self.idxml.element("entry") do@@ -7,22 +7,22 @@ struct SearchVideoxml.element("yt:videoId") { xml.text self.id }xml.element("yt:channelId") { xml.text self.ucid }xml.element("title") { xml.text self.title }- xml.element("link", rel: "alternate", href: "#{host_url}/watch?#{query_params}")+ xml.element("link", rel: "alternate", href: "#{HOST_URL}/watch?#{query_params}")xml.element("author") doif auto_generatedxml.element("name") { xml.text self.author }- xml.element("uri") { xml.text "#{host_url}/channel/#{self.ucid}" }+ xml.element("uri") { xml.text "#{HOST_URL}/channel/#{self.ucid}" }elsexml.element("name") { xml.text author }- xml.element("uri") { xml.text "#{host_url}/channel/#{ucid}" }+ xml.element("uri") { xml.text "#{HOST_URL}/channel/#{ucid}" }endendxml.element("content", type: "xhtml") doxml.element("div", xmlns: "http://www.w3.org/1999/xhtml") do- xml.element("a", href: "#{host_url}/watch?#{query_params}") do- xml.element("img", src: "#{host_url}/vi/#{self.id}/mqdefault.jpg")+ xml.element("a", href: "#{HOST_URL}/watch?#{query_params}") do+ xml.element("img", src: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg")endxml.element("p", style: "word-break:break-word;white-space:pre-wrap") { xml.text html_to_content(self.description_html) }@@ -33,7 +33,7 @@ struct SearchVideoxml.element("media:group") doxml.element("media:title") { xml.text self.title }- xml.element("media:thumbnail", url: "#{host_url}/vi/#{self.id}/mqdefault.jpg",+ xml.element("media:thumbnail", url: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg",width: "320", height: "180")xml.element("media:description") { xml.text html_to_content(self.description_html) }end@@ -44,17 +44,17 @@ struct SearchVideoendend- def to_xml(host_url, auto_generated, query_params, xml : XML::Builder | Nil = nil)+ def to_xml(auto_generated, query_params, xml : XML::Builder | Nil = nil)if xml- to_xml(host_url, auto_generated, query_params, xml)+ to_xml(HOST_URL, auto_generated, query_params, xml)elseXML.build do |json|- to_xml(host_url, auto_generated, query_params, xml)+ to_xml(HOST_URL, auto_generated, query_params, xml)endendend- def to_json(locale, config, kemal_config, json : JSON::Builder)+ def to_json(locale, json : JSON::Builder)json.object dojson.field "type", "video"json.field "title", self.title@@ -65,7 +65,7 @@ struct SearchVideojson.field "authorUrl", "/channel/#{self.ucid}"json.field "videoThumbnails" do- generate_thumbnails(json, self.id, config, kemal_config)+ generate_thumbnails(json, self.id)endjson.field "description", html_to_content(self.description_html)@@ -78,15 +78,20 @@ struct SearchVideojson.field "liveNow", self.live_nowjson.field "paid", self.paidjson.field "premium", self.premium+ json.field "isUpcoming", self.is_upcoming++ if self.premiere_timestamp+ json.field "premiereTimestamp", self.premiere_timestamp.try &.to_unix+ endendend- def to_json(locale, config, kemal_config, json : JSON::Builder | Nil = nil)+ def to_json(locale, json : JSON::Builder | Nil = nil)if json- to_json(locale, config, kemal_config, json)+ to_json(locale, json)elseJSON.build do |json|- to_json(locale, config, kemal_config, json)+ to_json(locale, json)endendend@@ -116,7 +121,7 @@ struct SearchPlaylistVideoendstruct SearchPlaylist- def to_json(locale, config, kemal_config, json : JSON::Builder)+ def to_json(locale, json : JSON::Builder)json.object dojson.field "type", "playlist"json.field "title", self.title@@ -137,7 +142,7 @@ struct SearchPlaylistjson.field "lengthSeconds", video.length_secondsjson.field "videoThumbnails" do- generate_thumbnails(json, video.id, config, Kemal.config)+ generate_thumbnails(json, video.id)endendend@@ -146,12 +151,12 @@ struct SearchPlaylistendend- def to_json(locale, config, kemal_config, json : JSON::Builder | Nil = nil)+ def to_json(locale, json : JSON::Builder | Nil = nil)if json- to_json(locale, config, kemal_config, json)+ to_json(locale, json)elseJSON.build do |json|- to_json(locale, config, kemal_config, json)+ to_json(locale, json)endendend@@ -168,7 +173,7 @@ struct SearchPlaylistendstruct SearchChannel- def to_json(locale, config, kemal_config, json : JSON::Builder)+ def to_json(locale, json : JSON::Builder)json.object dojson.field "type", "channel"json.field "author", self.author@@ -198,12 +203,12 @@ struct SearchChannelendend- def to_json(locale, config, kemal_config, json : JSON::Builder | Nil = nil)+ def to_json(locale, json : JSON::Builder | Nil = nil)if json- to_json(locale, config, kemal_config, json)+ to_json(locale, json)elseJSON.build do |json|- to_json(locale, config, kemal_config, json)+ to_json(locale, json)endendenddiff --git a/src/invidious/videos.cr b/src/invidious/videos.crindex 7e815ca1..ed5847e4 100644--- a/src/invidious/videos.cr+++ b/src/invidious/videos.cr@@ -255,17 +255,20 @@ struct Videoendend- def to_json(locale, config, kemal_config, decrypt_function, json : JSON::Builder)+ def to_json(locale, json : JSON::Builder)json.object dojson.field "type", "video"json.field "title", self.titlejson.field "videoId", self.id++ json.field "error", info["reason"] if info["reason"]?+json.field "videoThumbnails" do- generate_thumbnails(json, self.id, config, kemal_config)+ generate_thumbnails(json, self.id)endjson.field "storyboards" do- generate_storyboards(json, self.id, self.storyboards, config, kemal_config)+ generate_storyboards(json, self.id, self.storyboards)endjson.field "description", html_to_content(self.description_html)@@ -316,16 +319,12 @@ struct Videojson.field "premiereTimestamp", self.premiere_timestamp.not_nil!.to_unixend- if player_response["streamingData"]?.try &.["hlsManifestUrl"]?- host_url = make_host_url(config, kemal_config)-- hlsvp = player_response["streamingData"]["hlsManifestUrl"].as_s- hlsvp = hlsvp.gsub("https://manifest.googlevideo.com", host_url)-+ if hlsvp = self.hls_manifest_url+ hlsvp = hlsvp.gsub("https://manifest.googlevideo.com", HOST_URL)json.field "hlsUrl", hlsvpend- json.field "dashUrl", "#{make_host_url(config, kemal_config)}/api/manifest/dash/id/#{id}"+ json.field "dashUrl", "#{HOST_URL}/api/manifest/dash/id/#{id}"json.field "adaptiveFormats" dojson.array do@@ -424,7 +423,7 @@ struct Videojson.field "videoId", rv["id"]json.field "title", rv["title"]json.field "videoThumbnails" do- generate_thumbnails(json, rv["id"], config, kemal_config)+ generate_thumbnails(json, rv["id"])endjson.field "author", rv["author"]@@ -457,12 +456,12 @@ struct Videoendend- def to_json(locale, config, kemal_config, decrypt_function, json : JSON::Builder | Nil = nil)+ def to_json(locale, json : JSON::Builder | Nil = nil)if json- to_json(locale, config, kemal_config, decrypt_function, json)+ to_json(locale, json)elseJSON.build do |json|- to_json(locale, config, kemal_config, decrypt_function, json)+ to_json(locale, json)endendend@@ -1391,9 +1390,9 @@ def process_video_params(query, preferences)return paramsend-def build_thumbnails(id, config, kemal_config)+def build_thumbnails(id)return {- {name: "maxres", host: "#{make_host_url(config, kemal_config)}", url: "maxres", height: 720, width: 1280},+ {name: "maxres", host: "#{HOST_URL}", url: "maxres", height: 720, width: 1280},{name: "maxresdefault", host: "https://i.ytimg.com", url: "maxresdefault", height: 720, width: 1280},{name: "sddefault", host: "https://i.ytimg.com", url: "sddefault", height: 480, width: 640},{name: "high", host: "https://i.ytimg.com", url: "hqdefault", height: 360, width: 480},@@ -1405,9 +1404,9 @@ def build_thumbnails(id, config, kemal_config)}end-def generate_thumbnails(json, id, config, kemal_config)+def generate_thumbnails(json, id)json.array do- build_thumbnails(id, config, kemal_config).each do |thumbnail|+ build_thumbnails(id).each do |thumbnail|json.object dojson.field "quality", thumbnail[:name]json.field "url", "#{thumbnail[:host]}/vi/#{id}/#{thumbnail["url"]}.jpg"@@ -1418,7 +1417,7 @@ def generate_thumbnails(json, id, config, kemal_config)endend-def generate_storyboards(json, id, storyboards, config, kemal_config)+def generate_storyboards(json, id, storyboards)json.array dostoryboards.each do |storyboard|json.object dodiff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecrindex 708456f9..ae6341e0 100644--- a/src/invidious/views/watch.ecr+++ b/src/invidious/views/watch.ecr@@ -3,23 +3,23 @@<meta name="description" content="<%= HTML.escape(video.short_description) %>"><meta name="keywords" content="<%= video.keywords.join(",") %>"><meta property="og:site_name" content="Invidious">-<meta property="og:url" content="<%= host_url %>/watch?v=<%= video.id %>">+<meta property="og:url" content="<%= HOST_URL %>/watch?v=<%= video.id %>"><meta property="og:title" content="<%= HTML.escape(video.title) %>"><meta property="og:image" content="/vi/<%= video.id %>/maxres.jpg">-<meta property="og:description" content="<%= HTML.escape(video.short_description) %>">+<meta property="og:description" content="<%= video.short_description %>"><meta property="og:type" content="video.other">-<meta property="og:video:url" content="<%= host_url %>/embed/<%= video.id %>">-<meta property="og:video:secure_url" content="<%= host_url %>/embed/<%= video.id %>">+<meta property="og:video:url" content="<%= HOST_URL %>/embed/<%= video.id %>">+<meta property="og:video:secure_url" content="<%= HOST_URL %>/embed/<%= video.id %>"><meta property="og:video:type" content="text/html"><meta property="og:video:width" content="1280"><meta property="og:video:height" content="720"><meta name="twitter:card" content="player"><meta name="twitter:site" content="@omarroth1">-<meta name="twitter:url" content="<%= host_url %>/watch?v=<%= video.id %>">+<meta name="twitter:url" content="<%= HOST_URL %>/watch?v=<%= video.id %>"><meta name="twitter:title" content="<%= HTML.escape(video.title) %>">-<meta name="twitter:description" content="<%= HTML.escape(video.short_description) %>">-<meta name="twitter:image" content="<%= host_url %>/vi/<%= video.id %>/maxres.jpg">-<meta name="twitter:player" content="<%= host_url %>/embed/<%= video.id %>">+<meta name="twitter:description" content="<%= video.short_description %>">+<meta name="twitter:image" content="<%= HOST_URL %>/vi/<%= video.id %>/maxres.jpg">+<meta name="twitter:player" content="<%= HOST_URL %>/embed/<%= video.id %>"><meta name="twitter:player:width" content="1280"><meta name="twitter:player:height" content="720"><%= rendered "components/player_sources" %>