Merge pull request #2827 from SamantazFox/more-code-cleanup

More code cleanup
This commit is contained in:
Samantaz Fox
2022-02-02 00:36:19 +01:00
committed by GitHub
30 changed files with 193 additions and 305 deletions

View File

@@ -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

View File

@@ -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))

View File

@@ -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|

View File

@@ -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

View File

@@ -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|

View File

@@ -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"]