Update management of channel description

Follow this comment :
https://github.com/iv-org/invidious/pull/2968#issuecomment-1066428317
This commit is contained in:
Féry Mathieu (Mathius) 2022-03-14 22:37:22 +01:00
parent 3d2c4ea266
commit 9671e6ee0c
No known key found for this signature in database
GPG key ID: F9CCC80C18A59037
5 changed files with 45 additions and 18 deletions

View file

@ -6,6 +6,7 @@ record AboutChannel,
author_url : String, author_url : String,
author_thumbnail : String, author_thumbnail : String,
banner : String?, banner : String?,
description : String,
description_html : String, description_html : String,
total_views : Int64, total_views : Int64,
sub_count : Int32, sub_count : Int32,
@ -51,8 +52,7 @@ def get_about_info(ucid, locale) : AboutChannel
banners = initdata["header"]["interactiveTabbedHeaderRenderer"]?.try &.["banner"]?.try &.["thumbnails"]? banners = initdata["header"]["interactiveTabbedHeaderRenderer"]?.try &.["banner"]?.try &.["thumbnails"]?
banner = banners.try &.[-1]?.try &.["url"].as_s? banner = banners.try &.[-1]?.try &.["url"].as_s?
description = initdata["header"]["interactiveTabbedHeaderRenderer"]["description"]["simpleText"].as_s description_node = initdata["header"]["interactiveTabbedHeaderRenderer"]["description"]
description_html = HTML.escape(description)
is_family_friendly = initdata["microformat"]["microformatDataRenderer"]["familySafe"].as_bool is_family_friendly = initdata["microformat"]["microformatDataRenderer"]["familySafe"].as_bool
allowed_regions = initdata["microformat"]["microformatDataRenderer"]["availableCountries"].as_a.map(&.as_s) allowed_regions = initdata["microformat"]["microformatDataRenderer"]["availableCountries"].as_a.map(&.as_s)
@ -70,14 +70,24 @@ def get_about_info(ucid, locale) : AboutChannel
# if banner.includes? "channels/c4/default_banner" # if banner.includes? "channels/c4/default_banner"
# banner = nil # banner = nil
# end # end
description_node = initdata["metadata"]["channelMetadataRenderer"]?.try &.["description"]?
description = initdata["metadata"]["channelMetadataRenderer"]?.try &.["description"]?.try &.as_s? || ""
description_html = HTML.escape(description)
is_family_friendly = initdata["microformat"]["microformatDataRenderer"]["familySafe"].as_bool is_family_friendly = initdata["microformat"]["microformatDataRenderer"]["familySafe"].as_bool
allowed_regions = initdata["microformat"]["microformatDataRenderer"]["availableCountries"].as_a.map(&.as_s) allowed_regions = initdata["microformat"]["microformatDataRenderer"]["availableCountries"].as_a.map(&.as_s)
end end
description = !description_node.nil? ? description_node.as_s : ""
description_html = HTML.escape(description)
if !description_node.nil?
if description_node.as_h?.nil?
description_node = text_to_parsed_content(description_node.as_s)
end
description_html = parse_content(description_node)
if description_html == "" && description != ""
description_html = HTML.escape(description)
end
end
total_views = 0_i64 total_views = 0_i64
joined = Time.unix(0) joined = Time.unix(0)
@ -121,6 +131,7 @@ def get_about_info(ucid, locale) : AboutChannel
author_url: author_url, author_url: author_url,
author_thumbnail: author_thumbnail, author_thumbnail: author_thumbnail,
banner: banner, banner: banner,
description: description,
description_html: description_html, description_html: description_html,
total_views: total_views, total_views: total_views,
sub_count: sub_count, sub_count: sub_count,

View file

@ -554,6 +554,32 @@ def fill_links(html, scheme, host)
return html.to_xml(options: XML::SaveOptions::NO_DECL) return html.to_xml(options: XML::SaveOptions::NO_DECL)
end end
def text_to_parsed_content(text : String) : JSON::Any
nodes = [] of JSON::Any
text.split('\n').each do |line|
currentNodes = [] of JSON::Any
initialNode = {"text" => line}
currentNodes << (JSON.parse(initialNode.to_json))
line.scan(/https?:\/\/[^ ]*/).each do |uriMatch|
lastNode = currentNodes[currentNodes.size - 1].as_h
splittedLastNode = lastNode["text"].as_s.split(uriMatch[0])
lastNode["text"] = JSON.parse(splittedLastNode[0].to_json)
currentNodes[currentNodes.size - 1] = JSON.parse(lastNode.to_json)
currentNode = {"text" => uriMatch[0], "navigationEndpoint" => {"urlEndpoint" => {"url" => uriMatch[0]}}}
currentNodes << (JSON.parse(currentNode.to_json))
afterNode = {"text" => splittedLastNode.size > 0 ? splittedLastNode[1] : ""}
currentNodes << (JSON.parse(afterNode.to_json))
end
lastNode = currentNodes[currentNodes.size - 1].as_h
lastNode["text"] = JSON.parse("#{currentNodes[currentNodes.size - 1]["text"]}\n".to_json)
currentNodes[currentNodes.size - 1] = JSON.parse(lastNode.to_json)
currentNodes.each do |node|
nodes << (node)
end
end
return JSON.parse({"runs" => nodes}.to_json)
end
def parse_content(content : JSON::Any) : String def parse_content(content : JSON::Any) : String
content["simpleText"]?.try &.as_s.rchop('\ufeff').try { |b| HTML.escape(b) }.to_s || content["simpleText"]?.try &.as_s.rchop('\ufeff').try { |b| HTML.escape(b) }.to_s ||
content["runs"]?.try &.as_a.try { |r| content_to_comment_html(r).try &.to_s.gsub("\n", "<br>") } || "" content["runs"]?.try &.as_a.try { |r| content_to_comment_html(r).try &.to_s.gsub("\n", "<br>") } || ""

View file

@ -383,11 +383,3 @@ def fetch_random_instance
return filtered_instance_list.sample(1)[0] return filtered_instance_list.sample(1)[0]
end end
def make_html_with_links(baseText : String) : String
returnValue = baseText.dup
returnValue.scan(/https?:\/\/[^ \n]*/).each do |match|
returnValue = returnValue.sub(match[0], "<a href=\"#{match[0]}\">#{match[0]}</a>")
end
return returnValue
end

View file

@ -10,7 +10,7 @@ module Invidious::Routes::Channels
if !data.is_a?(Tuple) if !data.is_a?(Tuple)
return data return data
end end
locale, user, subscriptions, continuation, ucid, channel, description = data locale, user, subscriptions, continuation, ucid, channel = data
page = env.params.query["page"]?.try &.to_i? page = env.params.query["page"]?.try &.to_i?
page ||= 1 page ||= 1
@ -201,8 +201,6 @@ module Invidious::Routes::Channels
return error_template(500, ex) return error_template(500, ex)
end end
description = make_html_with_links(channel.description_html) return {locale, user, subscriptions, continuation, ucid, channel}
return {locale, user, subscriptions, continuation, ucid, channel, description}
end end
end end

View file

@ -32,7 +32,7 @@
<div class="h-box"> <div class="h-box">
<div id="descriptionWrapper"> <div id="descriptionWrapper">
<p><span style="white-space:pre-wrap"><%= description %></span></p> <p><span style="white-space:pre-wrap"><%= channel.description_html %></span></p>
</div> </div>
</div> </div>