Unify the multiple to_xml methods in a single function

This commit is contained in:
Samantaz Fox 2022-04-17 17:34:54 +02:00
parent 6b8189b465
commit 0e40b23d46
No known key found for this signature in database
GPG key ID: F42821059186176E
4 changed files with 56 additions and 98 deletions

View file

@ -50,36 +50,6 @@ struct ChannelVideo
end end
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 def to_tuple
{% begin %} {% begin %}
{ {

View file

@ -14,44 +14,6 @@ struct SearchVideo
property premiere_timestamp : Time? property premiere_timestamp : Time?
property author_verified : Bool 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) def to_json(locale : String?, json : JSON::Builder)
json.object do json.object do
json.field "type", "video" json.field "type", "video"

View file

@ -11,35 +11,6 @@ struct PlaylistVideo
property index : Int64 property index : Int64
property live_now : Bool 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) def to_json(json : JSON::Builder, index : Int32? = nil)
json.object do json.object do
json.field "title", self.title json.field "title", self.title

View file

@ -110,7 +110,62 @@ module Invidious::RssAtom
end end
# Video entries # 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 end
end end