Add separate method for constructing chapters json

This commit is contained in:
syeopite 2023-09-20 10:55:02 -07:00
parent 3860c69a52
commit 6bddcea178
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A
2 changed files with 40 additions and 28 deletions

View File

@ -434,34 +434,7 @@ module Invidious::Routes::API::V1::Videos
if format == "json" if format == "json"
env.response.content_type = "application/json" env.response.content_type = "application/json"
response = JSON.build do |json| response = Invidious::Videos::Chapters.to_json(chapters, video.automatically_generated_chapters?.as(Bool))
json.object do
json.field "chapters" do
json.field "autoGenerated", video.automatically_generated_chapters?
json.array do
chapters.each do |chapter|
json.object do
json.field "title", chapter.title
json.field "startMs", chapter.start_ms
json.field "endMs", chapter.end_ms
json.field "thumbnails" do
json.array do
chapter.thumbnails.each do |thumbnail|
json.object do
json.field "url", thumbnail["url"]
json.field "width", thumbnail["width"]
json.field "height", thumbnail["height"]
end
end
end
end
end
end
end
end
end
end
return response return response
else else

View File

@ -83,4 +83,43 @@ module Invidious::Videos::Chapters
end end
end end
end end
def self.to_json(json : JSON::Builder, chapters : Array(Chapter), auto_generated? : Bool)
json.field "autoGenerated", auto_generated?.to_s
json.field "chapters" do
json.array do
chapters.each do |chapter|
json.object do
json.field "title", chapter.title
json.field "startMs", chapter.start_ms
json.field "endMs", chapter.end_ms
json.field "thumbnails" do
json.array do
chapter.thumbnails.each do |thumbnail|
json.object do
json.field "url", thumbnail["url"]
json.field "width", thumbnail["width"]
json.field "height", thumbnail["height"]
end
end
end
end
end
end
end
end
end
def self.to_json(chapters : Array(Chapter), auto_generated? : Bool)
JSON.build do |json|
json.object do
json.field "chapters" do
json.object do
to_json(json, chapters, auto_generated?)
end
end
end
end
end
end end