mirror of
https://git.nadeko.net/Fijxu/invidious.git
synced 2025-12-21 02:18:52 +00:00
Merge pull request #2827 from SamantazFox/more-code-cleanup
More code cleanup
This commit is contained in:
@@ -98,7 +98,7 @@ module Invidious::Routes::API::Manifest
|
||||
height = fmt["height"].as_i
|
||||
|
||||
# Resolutions reported by YouTube player (may not accurately reflect source)
|
||||
height = potential_heights.min_by { |i| (height - i).abs }
|
||||
height = potential_heights.min_by { |x| (height - x).abs }
|
||||
next if unique_res && heights.includes? height
|
||||
heights << height
|
||||
|
||||
|
||||
@@ -115,8 +115,6 @@ module Invidious::Routes::API::V1::Authenticated
|
||||
end
|
||||
|
||||
def self.list_playlists(env)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
|
||||
env.response.content_type = "application/json"
|
||||
user = env.get("user").as(User)
|
||||
|
||||
@@ -125,7 +123,7 @@ module Invidious::Routes::API::V1::Authenticated
|
||||
JSON.build do |json|
|
||||
json.array do
|
||||
playlists.each do |playlist|
|
||||
playlist.to_json(0, locale, json)
|
||||
playlist.to_json(0, json)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -134,14 +132,13 @@ module Invidious::Routes::API::V1::Authenticated
|
||||
def self.create_playlist(env)
|
||||
env.response.content_type = "application/json"
|
||||
user = env.get("user").as(User)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
|
||||
title = env.params.json["title"]?.try &.as(String).delete("<>").byte_slice(0, 150)
|
||||
if !title
|
||||
return error_json(400, "Invalid title.")
|
||||
end
|
||||
|
||||
privacy = env.params.json["privacy"]?.try { |privacy| PlaylistPrivacy.parse(privacy.as(String).downcase) }
|
||||
privacy = env.params.json["privacy"]?.try { |p| PlaylistPrivacy.parse(p.as(String).downcase) }
|
||||
if !privacy
|
||||
return error_json(400, "Invalid privacy setting.")
|
||||
end
|
||||
@@ -160,8 +157,6 @@ module Invidious::Routes::API::V1::Authenticated
|
||||
end
|
||||
|
||||
def self.update_playlist_attribute(env)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
|
||||
env.response.content_type = "application/json"
|
||||
user = env.get("user").as(User)
|
||||
|
||||
@@ -180,7 +175,7 @@ module Invidious::Routes::API::V1::Authenticated
|
||||
end
|
||||
|
||||
title = env.params.json["title"].try &.as(String).delete("<>").byte_slice(0, 150) || playlist.title
|
||||
privacy = env.params.json["privacy"]?.try { |privacy| PlaylistPrivacy.parse(privacy.as(String).downcase) } || playlist.privacy
|
||||
privacy = env.params.json["privacy"]?.try { |p| PlaylistPrivacy.parse(p.as(String).downcase) } || playlist.privacy
|
||||
description = env.params.json["description"]?.try &.as(String).delete("\r") || playlist.description
|
||||
|
||||
if title != playlist.title ||
|
||||
@@ -197,8 +192,6 @@ module Invidious::Routes::API::V1::Authenticated
|
||||
end
|
||||
|
||||
def self.delete_playlist(env)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
|
||||
env.response.content_type = "application/json"
|
||||
user = env.get("user").as(User)
|
||||
|
||||
@@ -219,8 +212,6 @@ module Invidious::Routes::API::V1::Authenticated
|
||||
end
|
||||
|
||||
def self.insert_video_into_playlist(env)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
|
||||
env.response.content_type = "application/json"
|
||||
user = env.get("user").as(User)
|
||||
|
||||
@@ -274,8 +265,6 @@ module Invidious::Routes::API::V1::Authenticated
|
||||
end
|
||||
|
||||
def self.delete_video_in_playlist(env)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
|
||||
env.response.content_type = "application/json"
|
||||
user = env.get("user").as(User)
|
||||
|
||||
@@ -389,8 +378,8 @@ module Invidious::Routes::API::V1::Authenticated
|
||||
end
|
||||
|
||||
def self.unregister_token(env)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
env.response.content_type = "application/json"
|
||||
|
||||
user = env.get("user").as(User)
|
||||
scopes = env.get("scopes").as(Array(String))
|
||||
|
||||
|
||||
@@ -254,7 +254,7 @@ module Invidious::Routes::API::V1::Channels
|
||||
page = env.params.query["page"]?.try &.to_i?
|
||||
page ||= 1
|
||||
|
||||
count, search_results = channel_search(query, page, ucid)
|
||||
search_results = channel_search(query, page, ucid)
|
||||
JSON.build do |json|
|
||||
json.array do
|
||||
search_results.each do |item|
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
module Invidious::Routes::API::V1::Misc
|
||||
# Stats API endpoint for Invidious
|
||||
def self.stats(env)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
env.response.content_type = "application/json"
|
||||
|
||||
if !CONFIG.statistics_enabled
|
||||
@@ -14,9 +13,7 @@ module Invidious::Routes::API::V1::Misc
|
||||
# APIv1 currently uses the same logic for both
|
||||
# user playlists and Invidious playlists. This means that we can't
|
||||
# reasonably split them yet. This should be addressed in APIv2
|
||||
def self.get_playlist(env)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
|
||||
def self.get_playlist(env : HTTP::Server::Context)
|
||||
env.response.content_type = "application/json"
|
||||
plid = env.params.url["plid"]
|
||||
|
||||
@@ -34,7 +31,7 @@ module Invidious::Routes::API::V1::Misc
|
||||
end
|
||||
|
||||
begin
|
||||
playlist = get_playlist(plid, locale)
|
||||
playlist = get_playlist(plid)
|
||||
rescue ex : InfoException
|
||||
return error_json(404, ex)
|
||||
rescue ex
|
||||
@@ -49,7 +46,7 @@ module Invidious::Routes::API::V1::Misc
|
||||
# includes into the playlist a maximum of 20 videos, before the offset
|
||||
if offset > 0
|
||||
lookback = offset < 50 ? offset : 50
|
||||
response = playlist.to_json(offset - lookback, locale)
|
||||
response = playlist.to_json(offset - lookback)
|
||||
json_response = JSON.parse(response)
|
||||
else
|
||||
# Unless the continuation is really the offset 0, it becomes expensive.
|
||||
@@ -58,13 +55,13 @@ module Invidious::Routes::API::V1::Misc
|
||||
# it shouldn't happen often though
|
||||
|
||||
lookback = 0
|
||||
response = playlist.to_json(offset, locale, video_id: video_id)
|
||||
response = playlist.to_json(offset, video_id: video_id)
|
||||
json_response = JSON.parse(response)
|
||||
|
||||
if json_response["videos"].as_a[0]["index"] != offset
|
||||
offset = json_response["videos"].as_a[0]["index"].as_i
|
||||
lookback = offset < 50 ? offset : 50
|
||||
response = playlist.to_json(offset - lookback, locale)
|
||||
response = playlist.to_json(offset - lookback)
|
||||
json_response = JSON.parse(response)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -32,7 +32,7 @@ module Invidious::Routes::API::V1::Search
|
||||
return error_json(400, ex)
|
||||
end
|
||||
|
||||
count, search_results = search(query, search_params, region).as(Tuple)
|
||||
search_results = search(query, search_params, region)
|
||||
JSON.build do |json|
|
||||
json.array do
|
||||
search_results.each do |item|
|
||||
|
||||
@@ -20,8 +20,6 @@ module Invidious::Routes::API::V1::Videos
|
||||
end
|
||||
|
||||
def self.captions(env)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
|
||||
env.response.content_type = "application/json"
|
||||
|
||||
id = env.params.url["id"]
|
||||
@@ -73,9 +71,9 @@ module Invidious::Routes::API::V1::Videos
|
||||
env.response.content_type = "text/vtt; charset=UTF-8"
|
||||
|
||||
if lang
|
||||
caption = captions.select { |caption| caption.language_code == lang }
|
||||
caption = captions.select(&.language_code.== lang)
|
||||
else
|
||||
caption = captions.select { |caption| caption.name == label }
|
||||
caption = captions.select(&.name.== label)
|
||||
end
|
||||
|
||||
if caption.empty?
|
||||
@@ -149,8 +147,6 @@ module Invidious::Routes::API::V1::Videos
|
||||
# thumbnails for individual scenes in a video.
|
||||
# See https://support.jwplayer.com/articles/how-to-add-preview-thumbnails
|
||||
def self.storyboards(env)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
|
||||
env.response.content_type = "application/json"
|
||||
|
||||
id = env.params.url["id"]
|
||||
@@ -183,7 +179,7 @@ module Invidious::Routes::API::V1::Videos
|
||||
|
||||
env.response.content_type = "text/vtt"
|
||||
|
||||
storyboard = storyboards.select { |storyboard| width == "#{storyboard[:width]}" || height == "#{storyboard[:height]}" }
|
||||
storyboard = storyboards.select { |sb| width == "#{sb[:width]}" || height == "#{sb[:height]}" }
|
||||
|
||||
if storyboard.empty?
|
||||
haltf env, 404
|
||||
@@ -223,8 +219,6 @@ module Invidious::Routes::API::V1::Videos
|
||||
end
|
||||
|
||||
def self.annotations(env)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
|
||||
env.response.content_type = "text/xml"
|
||||
|
||||
id = env.params.url["id"]
|
||||
|
||||
@@ -2,13 +2,11 @@
|
||||
|
||||
module Invidious::Routes::Embed
|
||||
def self.redirect(env)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
|
||||
if plid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "")
|
||||
begin
|
||||
playlist = get_playlist(plid, locale: locale)
|
||||
playlist = get_playlist(plid)
|
||||
offset = env.params.query["index"]?.try &.to_i? || 0
|
||||
videos = get_playlist_videos(playlist, offset: offset, locale: locale)
|
||||
videos = get_playlist_videos(playlist, offset: offset)
|
||||
rescue ex
|
||||
return error_template(500, ex)
|
||||
end
|
||||
@@ -26,7 +24,6 @@ module Invidious::Routes::Embed
|
||||
end
|
||||
|
||||
def self.show(env)
|
||||
locale = env.get("preferences").as(Preferences).locale
|
||||
id = env.params.url["id"]
|
||||
|
||||
plid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "")
|
||||
@@ -60,9 +57,9 @@ module Invidious::Routes::Embed
|
||||
|
||||
if plid
|
||||
begin
|
||||
playlist = get_playlist(plid, locale: locale)
|
||||
playlist = get_playlist(plid)
|
||||
offset = env.params.query["index"]?.try &.to_i? || 0
|
||||
videos = get_playlist_videos(playlist, offset: offset, locale: locale)
|
||||
videos = get_playlist_videos(playlist, offset: offset)
|
||||
rescue ex
|
||||
return error_template(500, ex)
|
||||
end
|
||||
|
||||
@@ -265,7 +265,7 @@ module Invidious::Routes::Feeds
|
||||
|
||||
if plid.starts_with? "IV"
|
||||
if playlist = Invidious::Database::Playlists.select(id: plid)
|
||||
videos = get_playlist_videos(playlist, offset: 0, locale: locale)
|
||||
videos = get_playlist_videos(playlist, offset: 0)
|
||||
|
||||
return XML.build(indent: " ", encoding: "UTF-8") do |xml|
|
||||
xml.element("feed", "xmlns:yt": "http://www.youtube.com/xml/schemas/2015",
|
||||
|
||||
@@ -425,9 +425,9 @@ module Invidious::Routes::Login
|
||||
|
||||
found_valid_captcha = false
|
||||
error_exception = Exception.new
|
||||
tokens.each do |token|
|
||||
tokens.each do |tok|
|
||||
begin
|
||||
validate_request(token, answer, env.request, HMAC_KEY, locale)
|
||||
validate_request(tok, answer, env.request, HMAC_KEY, locale)
|
||||
found_valid_captcha = true
|
||||
rescue ex
|
||||
error_exception = ex
|
||||
|
||||
@@ -66,7 +66,7 @@ module Invidious::Routes::Playlists
|
||||
user = user.as(User)
|
||||
|
||||
playlist_id = env.params.query["list"]
|
||||
playlist = get_playlist(playlist_id, locale)
|
||||
playlist = get_playlist(playlist_id)
|
||||
subscribe_playlist(user, playlist)
|
||||
|
||||
env.redirect "/playlist?list=#{playlist.id}"
|
||||
@@ -157,7 +157,7 @@ module Invidious::Routes::Playlists
|
||||
end
|
||||
|
||||
begin
|
||||
videos = get_playlist_videos(playlist, offset: (page - 1) * 100, locale: locale)
|
||||
videos = get_playlist_videos(playlist, offset: (page - 1) * 100)
|
||||
rescue ex
|
||||
videos = [] of PlaylistVideo
|
||||
end
|
||||
@@ -239,15 +239,13 @@ module Invidious::Routes::Playlists
|
||||
query = env.params.query["q"]?
|
||||
if query
|
||||
begin
|
||||
search_query, count, items, operators = process_search_query(query, page, user, region: nil)
|
||||
search_query, items, operators = process_search_query(query, page, user, region: nil)
|
||||
videos = items.select(SearchVideo).map(&.as(SearchVideo))
|
||||
rescue ex
|
||||
videos = [] of SearchVideo
|
||||
count = 0
|
||||
end
|
||||
else
|
||||
videos = [] of SearchVideo
|
||||
count = 0
|
||||
end
|
||||
|
||||
env.set "add_playlist_items", plid
|
||||
@@ -306,7 +304,7 @@ module Invidious::Routes::Playlists
|
||||
|
||||
begin
|
||||
playlist_id = env.params.query["playlist_id"]
|
||||
playlist = get_playlist(playlist_id, locale).as(InvidiousPlaylist)
|
||||
playlist = get_playlist(playlist_id).as(InvidiousPlaylist)
|
||||
raise "Invalid user" if playlist.author != user.email
|
||||
rescue ex
|
||||
if redirect
|
||||
@@ -397,7 +395,7 @@ module Invidious::Routes::Playlists
|
||||
end
|
||||
|
||||
begin
|
||||
playlist = get_playlist(plid, locale)
|
||||
playlist = get_playlist(plid)
|
||||
rescue ex
|
||||
return error_template(500, ex)
|
||||
end
|
||||
@@ -414,7 +412,7 @@ module Invidious::Routes::Playlists
|
||||
end
|
||||
|
||||
begin
|
||||
videos = get_playlist_videos(playlist, offset: (page - 1) * 100, locale: locale)
|
||||
videos = get_playlist_videos(playlist, offset: (page - 1) * 100)
|
||||
rescue ex
|
||||
return error_template(500, "Error encountered while retrieving playlist videos.<br>#{ex.message}")
|
||||
end
|
||||
|
||||
@@ -54,7 +54,7 @@ module Invidious::Routes::Search
|
||||
user = env.get? "user"
|
||||
|
||||
begin
|
||||
search_query, count, videos, operators = process_search_query(query, page, user, region: region)
|
||||
search_query, videos, operators = process_search_query(query, page, user, region: region)
|
||||
rescue ex : ChannelSearchException
|
||||
return error_template(404, "Unable to find channel with id of '#{HTML.escape(ex.channel)}'. Are you sure that's an actual channel id? It should look like 'UC4QobU6STFB0P71PMvOGN5A'.")
|
||||
rescue ex
|
||||
|
||||
@@ -75,8 +75,8 @@ module Invidious::Routes::VideoPlayback
|
||||
end
|
||||
|
||||
begin
|
||||
client.get(url, headers) do |response|
|
||||
response.headers.each do |key, value|
|
||||
client.get(url, headers) do |resp|
|
||||
resp.headers.each do |key, value|
|
||||
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)
|
||||
env.response.headers[key] = value
|
||||
end
|
||||
@@ -84,7 +84,7 @@ module Invidious::Routes::VideoPlayback
|
||||
|
||||
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
||||
|
||||
if location = response.headers["Location"]?
|
||||
if location = resp.headers["Location"]?
|
||||
location = URI.parse(location)
|
||||
location = "#{location.request_target}&host=#{location.host}"
|
||||
|
||||
@@ -95,7 +95,7 @@ module Invidious::Routes::VideoPlayback
|
||||
return env.redirect location
|
||||
end
|
||||
|
||||
IO.copy(response.body_io, env.response)
|
||||
IO.copy(resp.body_io, env.response)
|
||||
end
|
||||
rescue ex
|
||||
end
|
||||
@@ -132,15 +132,15 @@ module Invidious::Routes::VideoPlayback
|
||||
headers["Range"] = "bytes=#{chunk_start}-#{chunk_end}"
|
||||
|
||||
begin
|
||||
client.get(url, headers) do |response|
|
||||
client.get(url, headers) do |resp|
|
||||
if first_chunk
|
||||
if !env.request.headers["Range"]? && response.status_code == 206
|
||||
if !env.request.headers["Range"]? && resp.status_code == 206
|
||||
env.response.status_code = 200
|
||||
else
|
||||
env.response.status_code = response.status_code
|
||||
env.response.status_code = resp.status_code
|
||||
end
|
||||
|
||||
response.headers.each do |key, value|
|
||||
resp.headers.each do |key, value|
|
||||
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase) && key.downcase != "content-range"
|
||||
env.response.headers[key] = value
|
||||
end
|
||||
@@ -148,7 +148,7 @@ module Invidious::Routes::VideoPlayback
|
||||
|
||||
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
||||
|
||||
if location = response.headers["Location"]?
|
||||
if location = resp.headers["Location"]?
|
||||
location = URI.parse(location)
|
||||
location = "#{location.request_target}&host=#{location.host}#{region ? "®ion=#{region}" : ""}"
|
||||
|
||||
@@ -161,8 +161,8 @@ module Invidious::Routes::VideoPlayback
|
||||
env.response.headers["Content-Disposition"] = "attachment; filename=\"#{URI.encode_www_form(title)}\"; filename*=UTF-8''#{URI.encode_www_form(title)}"
|
||||
end
|
||||
|
||||
if !response.headers.includes_word?("Transfer-Encoding", "chunked")
|
||||
content_length = response.headers["Content-Range"].split("/")[-1].to_i64
|
||||
if !resp.headers.includes_word?("Transfer-Encoding", "chunked")
|
||||
content_length = resp.headers["Content-Range"].split("/")[-1].to_i64
|
||||
if env.request.headers["Range"]?
|
||||
env.response.headers["Content-Range"] = "bytes #{range_start}-#{range_end || (content_length - 1)}/#{content_length}"
|
||||
env.response.content_length = ((range_end.try &.+ 1) || content_length) - range_start
|
||||
@@ -172,7 +172,7 @@ module Invidious::Routes::VideoPlayback
|
||||
end
|
||||
end
|
||||
|
||||
proxy_file(response, env)
|
||||
proxy_file(resp, env)
|
||||
end
|
||||
rescue ex
|
||||
if ex.message != "Error reading socket: Connection reset by peer"
|
||||
|
||||
Reference in New Issue
Block a user