From 0e40b23d46b18ef6967c5fd538bae4e1c506f568 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Sun, 17 Apr 2022 17:34:54 +0200 Subject: [PATCH] Unify the multiple to_xml methods in a single function --- src/invidious/channels/channels.cr | 30 ----------- src/invidious/helpers/serialized_yt_data.cr | 38 -------------- src/invidious/playlists.cr | 29 ----------- src/invidious/rss_atom.cr | 57 ++++++++++++++++++++- 4 files changed, 56 insertions(+), 98 deletions(-) diff --git a/src/invidious/channels/channels.cr b/src/invidious/channels/channels.cr index 9315d165..1c12cf19 100644 --- a/src/invidious/channels/channels.cr +++ b/src/invidious/channels/channels.cr @@ -50,36 +50,6 @@ struct ChannelVideo end end - def to_xml(xml : XML::Builder, query_params : HTTP::Params) - xml.element("entry") do - xml.element("id") { xml.text "ni://invidious/sha-256;" + sha256("video/#{self.id}") } - xml.element("title") { xml.text self.title } - xml.element("link", rel: "alternate", href: "#{HOST_URL}/watch?#{query_params}") - - xml.element("author") do - xml.element("name") { xml.text self.author } - xml.element("uri") { xml.text "#{HOST_URL}/channel/#{self.ucid}" } - end - - xml.element("content", type: "xhtml") do - xml.element("div", xmlns: "http://www.w3.org/1999/xhtml") do - xml.element("a", href: "#{HOST_URL}/watch?v=#{self.id}&#{query_params}") do - xml.element("img", src: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg") - end - end - end - - xml.element("published") { xml.text self.published.to_s("%Y-%m-%dT%H:%M:%S%:z") } - xml.element("updated") { xml.text self.updated.to_s("%Y-%m-%dT%H:%M:%S%:z") } - - xml.element("media:group") do - xml.element("media:title") { xml.text self.title } - xml.element("media:thumbnail", url: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg", - width: "320", height: "180") - end - end - end - def to_tuple {% begin %} { diff --git a/src/invidious/helpers/serialized_yt_data.cr b/src/invidious/helpers/serialized_yt_data.cr index fc91031f..89ff5dad 100644 --- a/src/invidious/helpers/serialized_yt_data.cr +++ b/src/invidious/helpers/serialized_yt_data.cr @@ -14,44 +14,6 @@ struct SearchVideo property premiere_timestamp : Time? property author_verified : Bool - def to_xml(xml : XML::Builder, query_params : HTTP::Params) - xml.element("entry") do - xml.element("id") { xml.text "ni://invidious/sha-256;" + sha256("video/#{self.id}") } - xml.element("title") { xml.text self.title } - xml.element("link", rel: "alternate", href: "#{HOST_URL}/watch?#{query_params}") - - xml.element("author") do - xml.element("name") { xml.text self.author } - xml.element("uri") { xml.text "#{HOST_URL}/channel/#{self.ucid}" } - end - - xml.element("content", type: "xhtml") do - xml.element("div", xmlns: "http://www.w3.org/1999/xhtml") do - xml.element("a", href: "#{HOST_URL}/watch?v=#{self.id}&#{query_params}") do - xml.element("img", src: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg") - end - - xml.element("p", style: "white-space:pre-wrap") do - xml.text html_to_content(self.description_html) - end - end - end - - xml.element("published") { xml.text self.published.to_s("%Y-%m-%dT%H:%M:%S%:z") } - - xml.element("media:group") do - xml.element("media:title") { xml.text self.title } - xml.element("media:thumbnail", url: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg", - width: "320", height: "180") - xml.element("media:description") { xml.text html_to_content(self.description_html) } - end - - xml.element("media:community") do - xml.element("media:statistics", views: self.views) - end - end - end - def to_json(locale : String?, json : JSON::Builder) json.object do json.field "type", "video" diff --git a/src/invidious/playlists.cr b/src/invidious/playlists.cr index 839ba41e..3d7cb5a6 100644 --- a/src/invidious/playlists.cr +++ b/src/invidious/playlists.cr @@ -11,35 +11,6 @@ struct PlaylistVideo property index : Int64 property live_now : Bool - def to_xml(xml : XML::Builder, query_params : HTTP::Params) - xml.element("entry") do - xml.element("id") { xml.text "ni://invidious/sha-256;" + sha256("video/#{self.id}") } - xml.element("title") { xml.text self.title } - xml.element("link", rel: "alternate", href: "#{HOST_URL}/watch?v=#{self.id}") - - xml.element("author") do - xml.element("name") { xml.text self.author } - xml.element("uri") { xml.text "#{HOST_URL}/channel/#{self.ucid}" } - end - - xml.element("content", type: "xhtml") do - xml.element("div", xmlns: "http://www.w3.org/1999/xhtml") do - xml.element("a", href: "#{HOST_URL}/watch?v=#{self.id}&#{query_params}") do - xml.element("img", src: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg") - end - end - end - - xml.element("published") { xml.text self.published.to_s("%Y-%m-%dT%H:%M:%S%:z") } - - xml.element("media:group") do - xml.element("media:title") { xml.text self.title } - xml.element("media:thumbnail", url: "#{HOST_URL}/vi/#{self.id}/mqdefault.jpg", - width: "320", height: "180") - end - end - end - def to_json(json : JSON::Builder, index : Int32? = nil) json.object do json.field "title", self.title diff --git a/src/invidious/rss_atom.cr b/src/invidious/rss_atom.cr index 35f0c90f..2b69d8a8 100644 --- a/src/invidious/rss_atom.cr +++ b/src/invidious/rss_atom.cr @@ -110,7 +110,62 @@ module Invidious::RssAtom end # Video entries - videos.each { |video| video.to_xml(xml, params) } + videos.each do |video| + xml.element("entry") { atom_video(xml, video, params) } + end + end + end + end + + def atom_video(xml : XML::Builder, video : AnyVideo, query_params : HTTP::Params) + # URLs that are reused below + video_url = "#{HOST_URL}/watch?v=#{video.id}&#{query_params}" + video_thumb = "#{HOST_URL}/vi/#{video.id}/mqdefault.jpg" + + description = video.is_a?(SearchVideo) ? video.description_html : "" + + xml.element("id") { xml.text "ni://invidious/sha-256;" + sha256("video/#{video.id}") } + xml.element("title") { xml.text video.title } + xml.element("link", rel: "alternate", href: video_url) + + xml.element("author") do + xml.element("name") { xml.text video.author } + xml.element("uri") { xml.text "#{HOST_URL}/channel/#{video.ucid}" } + end + + xml.element("content", type: "xhtml") do + xml.element("div", xmlns: "http://www.w3.org/1999/xhtml") do + # Link to video + xml.element("a", href: video_url) do + xml.element("img", src: video_thumb) + end + + # Video sescription (SearchVideo only) + if video.is_a?(SearchVideo) + xml.element("p", style: "white-space:pre-wrap") { xml.text description } + end + end + end + + # Feed creation (if available) and update (ChannelVideo only) dates + xml.element("published") { xml.text video.published.to_rfc3339 } + xml.element("updated") { xml.text video.updated.to_rfc3339 } if video.is_a?(ChannelVideo) + + # Media properties + xml.element("media:group") do + xml.element("media:title") { xml.text video.title } + xml.element("media:thumbnail", url: video_thumb, width: "320", height: "180") + + # Video sescription (SearchVideo only) + if video.is_a?(SearchVideo) + xml.element("media:description") { xml.text description } + end + end + + # Views count (all except PlaylistVideo) + if !video.is_a?(PlaylistVideo) + xml.element("media:community") do + xml.element("media:statistics", views: video.views) end end end