From 6bddcea178d1e9ff5da170c50fcc3581449570a2 Mon Sep 17 00:00:00 2001 From: syeopite Date: Wed, 20 Sep 2023 10:55:02 -0700 Subject: [PATCH] Add separate method for constructing chapters json --- src/invidious/routes/api/v1/videos.cr | 29 +------------------- src/invidious/videos/chapters.cr | 39 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr index d42fbcb6..d1736900 100644 --- a/src/invidious/routes/api/v1/videos.cr +++ b/src/invidious/routes/api/v1/videos.cr @@ -434,34 +434,7 @@ module Invidious::Routes::API::V1::Videos if format == "json" env.response.content_type = "application/json" - response = JSON.build do |json| - 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 + response = Invidious::Videos::Chapters.to_json(chapters, video.automatically_generated_chapters?.as(Bool)) return response else diff --git a/src/invidious/videos/chapters.cr b/src/invidious/videos/chapters.cr index f8c31648..2ad469bb 100644 --- a/src/invidious/videos/chapters.cr +++ b/src/invidious/videos/chapters.cr @@ -83,4 +83,43 @@ module Invidious::Videos::Chapters 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