Invidious is an alternative front-end to YouTube
Fix warnings in latest version of Crystal
Omar Rothca1185d0be46
diff --git a/src/invidious.cr b/src/invidious.crindex 1448c502..55974e4a 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -385,6 +385,8 @@ get "/" do |env|elsetemplated "popular"end+ else+ templated "empty"endend@@ -722,6 +724,7 @@ get "/embed/:id" do |env|endnext env.redirect url+ else nil # Continueendparams = process_video_params(env.params.query, preferences)@@ -1213,6 +1216,10 @@ post "/playlist_ajax" do |env|error_message = {"error" => "Playlist cannot have more than 500 videos"}.to_jsonnext error_messageend+ else+ error_message = {"error" => "Unsupported action #{action}"}.to_json+ env.response.status_code = 400+ next error_messageendvideo_id = env.params.query["video_id"]@@ -1253,6 +1260,10 @@ post "/playlist_ajax" do |env|PG_DB.exec("UPDATE playlists SET index = array_remove(index, $1), video_count = cardinality(index), updated = $2 WHERE id = $3", index, Time.utc, playlist_id)when "action_move_video_before"# TODO: Playlist stub+ else+ error_message = {"error" => "Unsupported action #{action}"}.to_json+ env.response.status_code = 400+ next error_messageendif redirect@@ -1547,7 +1558,7 @@ post "/login" do |env|case prompt_typewhen "TWO_STEP_VERIFICATION"prompt_type = 2- when "LOGIN_CHALLENGE"+ else # "LOGIN_CHALLENGE"prompt_type = 4end@@ -1840,7 +1851,7 @@ post "/login" do |env|env.response.status_code = 400next templated "error"end- when "text"+ else # "text"answer = Digest::MD5.hexdigest(answer.downcase.strip)found_valid_captcha = false@@ -2251,6 +2262,10 @@ post "/watch_ajax" do |env|endwhen "action_mark_unwatched"PG_DB.exec("UPDATE users SET watched = array_remove(watched, $1) WHERE email = $2", id, user.email)+ else+ error_message = {"error" => "Unsupported action #{action}"}.to_json+ env.response.status_code = 400+ next error_messageendif redirect@@ -2405,6 +2420,10 @@ post "/subscription_ajax" do |env|endwhen "action_remove_subscriptions"PG_DB.exec("UPDATE users SET feed_needs_update = true, subscriptions = array_remove(subscriptions, $1) WHERE email = $2", channel_id, email)+ else+ error_message = {"error" => "Unsupported action #{action}"}.to_json+ env.response.status_code = 400+ next error_messageendif redirect@@ -2559,6 +2578,7 @@ post "/data_control" do |env|nextend+ # TODO: Unify into single import based on content-typecase part.namewhen "import_invidious"body = JSON.parse(body)@@ -2645,6 +2665,7 @@ post "/data_control" do |env|endendend+ else nil # Ignoreendendend@@ -2986,6 +3007,10 @@ post "/token_ajax" do |env|case actionwhen .starts_with? "action_revoke_token"PG_DB.exec("DELETE FROM session_ids * WHERE id = $1 AND email = $2", session, user.email)+ else+ error_message = {"error" => "Unsupported action #{action}"}.to_json+ env.response.status_code = 400+ next error_messageendif redirect@@ -3280,6 +3305,7 @@ get "/feed/playlist/:plid" do |env|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}"+ else nil # Skipendendend@@ -4037,7 +4063,7 @@ get "/api/v1/annotations/:id" do |env|cache_annotation(PG_DB, id, annotations)end- when "youtube"+ else # "youtube"response = YT_POOL.client &.get("/annotations_invideo?video_id=#{id}")if response.status_code != 200diff --git a/src/invidious/channels.cr b/src/invidious/channels.crindex 35ef5df2..afc1528e 100644--- a/src/invidious/channels.cr+++ b/src/invidious/channels.cr@@ -412,6 +412,7 @@ def fetch_channel_playlists(ucid, author, auto_generated, continuation, sort_by)url += "&sort=da"when "newest", "newest_created"url += "&sort=dd"+ else nil # Ignoreendresponse = YT_POOL.client &.get(url)@@ -469,6 +470,7 @@ def produce_channel_videos_url(ucid, page = 1, auto_generated = nil, sort_by = "object["80226972:embedded"]["3:base64"].as(Hash)["3:varint"] = 0x01_i64when "oldest"object["80226972:embedded"]["3:base64"].as(Hash)["3:varint"] = 0x02_i64+ else nil # Ignoreendobject["80226972:embedded"]["3:string"] = Base64.urlsafe_encode(Protodec::Any.from_json(Protodec::Any.cast_json(object["80226972:embedded"]["3:base64"])))@@ -513,6 +515,7 @@ def produce_channel_playlists_url(ucid, cursor, sort = "newest", auto_generatedobject["80226972:embedded"]["3:base64"].as(Hash)["3:varint"] = 3_i64when "last", "last_added"object["80226972:embedded"]["3:base64"].as(Hash)["3:varint"] = 4_i64+ else nil # Ignoreendenddiff --git a/src/invidious/comments.cr b/src/invidious/comments.crindex 4a048d7a..24564bb9 100644--- a/src/invidious/comments.cr+++ b/src/invidious/comments.cr@@ -356,6 +356,7 @@ def template_youtube_comments(comments, locale, thin_mode)</div></div>END_HTML+ else nil # Ignoreendend@@ -609,6 +610,8 @@ def produce_comment_continuation(video_id, cursor = "", sort_by = "top")object["6:embedded"].as(Hash)["4:embedded"].as(Hash)["6:varint"] = 0_i64when "new", "newest"object["6:embedded"].as(Hash)["4:embedded"].as(Hash)["6:varint"] = 1_i64+ else # top+ object["6:embedded"].as(Hash)["4:embedded"].as(Hash)["6:varint"] = 0_i64endcontinuation = object.try { |i| Protodec::Any.cast_json(object) }diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.crindex e168c55e..96d14737 100644--- a/src/invidious/helpers/helpers.cr+++ b/src/invidious/helpers/helpers.cr@@ -173,6 +173,8 @@ struct Configyaml.scalar "ipv4"when Socket::Family::INET6yaml.scalar "ipv6"+ when Socket::Family::UNIX+ raise "Invalid socket family #{value}"endend@@ -223,6 +225,8 @@ struct Configelsereturn falseend+ else+ return falseendend@@ -520,9 +524,7 @@ def extract_shelf_items(nodeset, ucid = nil, author_name = nil)shelf.xpath_nodes(%q(.//ul[contains(@class, "yt-uix-shelfslider-list") or contains(@class, "expanded-shelf-content-list")]/li)).each do |child_node|type = child_node.xpath_node(%q(./div))- if !type- next- end+ next if !typecase type["class"]when .includes? "yt-lockup-video"@@ -599,6 +601,8 @@ def extract_shelf_items(nodeset, ucid = nil, author_name = nil)videos: videos,thumbnail: playlist_thumbnail)+ else+ next # Skipendend@@ -763,7 +767,7 @@ def create_notification_stream(env, config, kemal_config, decrypt_function, topiloop dotime_span = [0, 0, 0, 0]time_span[rand(4)] = rand(30) + 5- published = Time.utc - Time::Span.new(time_span[0], time_span[1], time_span[2], time_span[3])+ published = Time.utc - Time::Span.new(days: time_span[0], hours: time_span[1], minutes: time_span[2], seconds: time_span[3])video_id = TEST_IDS[rand(TEST_IDS.size)]video = get_video(video_id, PG_DB)diff --git a/src/invidious/helpers/i18n.cr b/src/invidious/helpers/i18n.crindex 4c9bb2d6..0faa2e32 100644--- a/src/invidious/helpers/i18n.cr+++ b/src/invidious/helpers/i18n.cr@@ -24,6 +24,8 @@ def translate(locale : Hash(String, JSON::Any) | Nil, translation : String, textif !locale[translation].as_s.empty?translation = locale[translation].as_send+ else+ raise "Invalid translation #{translation}"endenddiff --git a/src/invidious/helpers/tokens.cr b/src/invidious/helpers/tokens.crindex 0b609e80..39aae367 100644--- a/src/invidious/helpers/tokens.cr+++ b/src/invidious/helpers/tokens.cr@@ -43,15 +43,10 @@ def sign_token(key, hash)string_to_sign = [] of Stringhash.each do |key, value|- if key == "signature"- next- end+ next if key == "signature"- if value.is_a?(JSON::Any)- case value- when .as_a?- value = value.as_a.map { |item| item.as_s }- end+ if value.is_a?(JSON::Any) && value.as_a?+ value = value.as_a.map { |i| i.as_s }endcase valuediff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.crindex d0892862..79a69cf9 100644--- a/src/invidious/helpers/utils.cr+++ b/src/invidious/helpers/utils.cr@@ -100,7 +100,7 @@ enddef decode_length_seconds(string)length_seconds = string.gsub(/[^0-9:]/, "").split(":").map &.to_ilength_seconds = [0] * (3 - length_seconds.size) + length_seconds- length_seconds = Time::Span.new(length_seconds[0], length_seconds[1], length_seconds[2])+ length_seconds = Time::Span.new hours: length_seconds[0], minutes: length_seconds[1], seconds: length_seconds[2]length_seconds = length_seconds.total_seconds.to_ireturn length_seconds@@ -162,6 +162,7 @@ def decode_date(string : String)return Time.utcwhen "yesterday"return Time.utc - 1.day+ else nil # Continueend# String matches format "20 hours ago", "4 months ago"...diff --git a/src/invidious/search.cr b/src/invidious/search.crindex 92996f75..e8521629 100644--- a/src/invidious/search.cr+++ b/src/invidious/search.cr@@ -310,6 +310,7 @@ def produce_search_params(sort : String = "relevance", date : String = "", conteobject["2:embedded"].as(Hash)["1:varint"] = 4_i64when "year"object["2:embedded"].as(Hash)["1:varint"] = 5_i64+ else nil # Ignoreendcase content_type@@ -334,6 +335,7 @@ def produce_search_params(sort : String = "relevance", date : String = "", conteobject["2:embedded"].as(Hash)["3:varint"] = 1_i64when "long"object["2:embedded"].as(Hash)["3:varint"] = 2_i64+ else nil # Ignoreendfeatures.each do |feature|@@ -358,6 +360,7 @@ def produce_search_params(sort : String = "relevance", date : String = "", conteobject["2:embedded"].as(Hash)["23:varint"] = 1_i64when "hdr"object["2:embedded"].as(Hash)["25:varint"] = 1_i64+ else nil # Ignoreendenddiff --git a/src/invidious/users.cr b/src/invidious/users.crindex afb100f2..0aa94d82 100644--- a/src/invidious/users.cr+++ b/src/invidious/users.cr@@ -350,6 +350,7 @@ def get_subscription_feed(db, user, max_results = 40, page = 1)notifications.sort_by! { |video| video.author }when "channel name - reverse"notifications.sort_by! { |video| video.author }.reverse!+ else nil # Ignoreendelseif user.preferences.latest_only@@ -398,6 +399,7 @@ def get_subscription_feed(db, user, max_results = 40, page = 1)videos.sort_by! { |video| video.author }when "channel name - reverse"videos.sort_by! { |video| video.author }.reverse!+ else nil # Ignoreendnotifications = PG_DB.query_one("SELECT notifications FROM users WHERE email = $1", user.email, as: Array(String))diff --git a/src/invidious/videos.cr b/src/invidious/videos.crindex 1c7599f8..f9d3dc28 100644--- a/src/invidious/videos.cr+++ b/src/invidious/videos.cr@@ -1250,6 +1250,7 @@ def fetch_video(id, region)genre_url = "/channel/UCfFyYRYslvuhwMDnx6KjUvw"when "Trailers"genre_url = "/channel/UClgRkhTL3_hImCAmdLfDE4g"+ else nil # Ignoreendlicense = html.xpath_node(%q(//h4[contains(text(),"License")]/parent::*/ul/li)).try &.content || ""diff --git a/src/invidious/views/login.ecr b/src/invidious/views/login.ecrindex 59fa90e5..b6e8117b 100644--- a/src/invidious/views/login.ecr+++ b/src/invidious/views/login.ecr@@ -22,7 +22,43 @@<hr><% case account_type when %>- <% when "invidious" %>+ <% when "google" %>+ <form class="pure-form pure-form-stacked" action="/login?referer=<%= URI.encode_www_form(referer) %>&type=google" method="post">+ <fieldset>+ <% if email %>+ <input name="email" type="hidden" value="<%= email %>">+ <% else %>+ <label for="email"><%= translate(locale, "E-mail") %> :</label>+ <input required class="pure-input-1" name="email" type="email" placeholder="<%= translate(locale, "E-mail") %>">+ <% end %>++ <% if password %>+ <input name="password" type="hidden" value="<%= HTML.escape(password) %>">+ <% else %>+ <label for="password"><%= translate(locale, "Password") %> :</label>+ <input required class="pure-input-1" name="password" type="password" placeholder="<%= translate(locale, "Password") %>">+ <% end %>++ <% if prompt %>+ <label for="tfa"><%= translate(locale, prompt) %> :</label>+ <input required class="pure-input-1" name="tfa" type="text" placeholder="<%= translate(locale, prompt) %>">+ <% end %>++ <% if tfa %>+ <input type="hidden" name="tfa" value="<%= tfa %>">+ <% end %>++ <% if captcha %>+ <img style="width:50%" src="/Captcha?v=2&ctoken=<%= captcha[:tokens][0] %>"/>+ <input type="hidden" name="token" value="<%= captcha[:tokens][0] %>">+ <label for="answer"><%= translate(locale, "Answer") %> :</label>+ <input type="text" name="answer" type="text" placeholder="<%= translate(locale, "Answer") %>">+ <% end %>++ <button type="submit" class="pure-button pure-button-primary"><%= translate(locale, "Sign In") %></button>+ </fieldset>+ </form>+ <% else # "invidious" %><form class="pure-form pure-form-stacked" action="/login?referer=<%= URI.encode_www_form(referer) %>&type=invidious" method="post"><fieldset><% if email %>@@ -50,7 +86,7 @@<input type="hidden" name="captcha_type" value="image"><label for="answer"><%= translate(locale, "Time (h:mm:ss):") %></label><input type="text" name="answer" type="text" placeholder="h:mm:ss">- <% when "text" %>+ <% else # "text" %><% captcha = captcha.not_nil! %><% captcha[:tokens].each_with_index do |token, i| %><input type="hidden" name="token[<%= i %>]" value="<%= URI.encode_www_form(token) %>">@@ -71,7 +107,7 @@<%= translate(locale, "Text CAPTCHA") %></button></label>- <% when "text" %>+ <% else # "text" %><label><button type="submit" name="change_type" class="pure-button pure-button-primary" value="image"><%= translate(locale, "Image CAPTCHA") %>@@ -85,42 +121,6 @@<% end %></fieldset></form>- <% when "google" %>- <form class="pure-form pure-form-stacked" action="/login?referer=<%= URI.encode_www_form(referer) %>&type=google" method="post">- <fieldset>- <% if email %>- <input name="email" type="hidden" value="<%= email %>">- <% else %>- <label for="email"><%= translate(locale, "E-mail") %> :</label>- <input required class="pure-input-1" name="email" type="email" placeholder="<%= translate(locale, "E-mail") %>">- <% end %>-- <% if password %>- <input name="password" type="hidden" value="<%= HTML.escape(password) %>">- <% else %>- <label for="password"><%= translate(locale, "Password") %> :</label>- <input required class="pure-input-1" name="password" type="password" placeholder="<%= translate(locale, "Password") %>">- <% end %>-- <% if prompt %>- <label for="tfa"><%= translate(locale, prompt) %> :</label>- <input required class="pure-input-1" name="tfa" type="text" placeholder="<%= translate(locale, prompt) %>">- <% end %>-- <% if tfa %>- <input type="hidden" name="tfa" value="<%= tfa %>">- <% end %>-- <% if captcha %>- <img style="width:50%" src="/Captcha?v=2&ctoken=<%= captcha[:tokens][0] %>"/>- <input type="hidden" name="token" value="<%= captcha[:tokens][0] %>">- <label for="answer"><%= translate(locale, "Answer") %> :</label>- <input type="text" name="answer" type="text" placeholder="<%= translate(locale, "Answer") %>">- <% end %>-- <button type="submit" class="pure-button pure-button-primary"><%= translate(locale, "Sign In") %></button>- </fieldset>- </form><% end %></div></div>