Add possibility to don't clip url in video

This commit is contained in:
Féry Mathieu (Mathius) 2022-02-16 14:46:24 +01:00
parent 85ba04b715
commit 7aa41f4009
No known key found for this signature in database
GPG Key ID: F9CCC80C18A59037
2 changed files with 22 additions and 14 deletions

View File

@ -553,12 +553,12 @@ 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 parse_content(content : JSON::Any) : String def parse_content(content : JSON::Any, clip_urls : Bool = true) : 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, clip_urls).try &.to_s.gsub("\n", "<br>") } || ""
end end
def content_to_comment_html(content) def content_to_comment_html(content : Array(JSON::Any), clip_urls : Bool = true)
comment_html = content.map do |run| comment_html = content.map do |run|
text = HTML.escape(run["text"].as_s) text = HTML.escape(run["text"].as_s)
@ -586,7 +586,11 @@ def content_to_comment_html(content)
end end
end end
text = %(<a href="#{url}">#{text}</a>) if clip_urls
text = %(<a href="#{url}">#{text}</a>)
else
text = %(<a href="#{url}">#{url}</a>)
end
elsif watch_endpoint = run["navigationEndpoint"]["watchEndpoint"]? elsif watch_endpoint = run["navigationEndpoint"]["watchEndpoint"]?
length_seconds = watch_endpoint["startTimeSeconds"]? length_seconds = watch_endpoint["startTimeSeconds"]?
video_id = watch_endpoint["videoId"].as_s video_id = watch_endpoint["videoId"].as_s
@ -597,7 +601,11 @@ def content_to_comment_html(content)
text = %(<a href="/watch?v=#{video_id}">#{text}</a>) text = %(<a href="/watch?v=#{video_id}">#{text}</a>)
end end
elsif url = run.dig?("navigationEndpoint", "commandMetadata", "webCommandMetadata", "url").try &.as_s elsif url = run.dig?("navigationEndpoint", "commandMetadata", "webCommandMetadata", "url").try &.as_s
text = %(<a href="#{url}">#{text}</a>) if clip_urls
text = %(<a href="#{url}">#{text}</a>)
else
text = %(<a href="#{url}">#{url}</a>)
end
end end
end end

View File

@ -871,7 +871,7 @@ def parse_related_video(related : JSON::Any) : Hash(String, JSON::Any)?
} }
end end
def extract_video_info(video_id : String, proxy_region : String? = nil, context_screen : String? = nil) def extract_video_info(video_id : String, proxy_region : String? = nil, context_screen : String? = nil, clip_urls : Bool? = true)
params = {} of String => JSON::Any params = {} of String => JSON::Any
client_config = YoutubeAPI::ClientConfig.new(proxy_region: proxy_region) client_config = YoutubeAPI::ClientConfig.new(proxy_region: proxy_region)
@ -1024,7 +1024,7 @@ def extract_video_info(video_id : String, proxy_region : String? = nil, context_
# Description # Description
description_html = video_secondary_renderer.try &.dig?("description", "runs") description_html = video_secondary_renderer.try &.dig?("description", "runs")
.try &.as_a.try { |t| content_to_comment_html(t) } .try &.as_a.try { |t| content_to_comment_html(t, clip_urls) }
params["descriptionHtml"] = JSON::Any.new(description_html || "<p></p>") params["descriptionHtml"] = JSON::Any.new(description_html || "<p></p>")
@ -1072,7 +1072,7 @@ def extract_video_info(video_id : String, proxy_region : String? = nil, context_
return params return params
end end
def get_video(id, refresh = true, region = nil, force_refresh = false) def get_video(id, refresh = true, region = nil, force_refresh = false, clip_urls = true)
if (video = Invidious::Database::Videos.select(id)) && !region if (video = Invidious::Database::Videos.select(id)) && !region
# If record was last updated over 10 minutes ago, or video has since premiered, # If record was last updated over 10 minutes ago, or video has since premiered,
# refresh (expire param in response lasts for 6 hours) # refresh (expire param in response lasts for 6 hours)
@ -1081,7 +1081,7 @@ def get_video(id, refresh = true, region = nil, force_refresh = false)
(video.premiere_timestamp.try &.< Time.utc)) || (video.premiere_timestamp.try &.< Time.utc)) ||
force_refresh force_refresh
begin begin
video = fetch_video(id, region) video = fetch_video(id, region, clip_urls)
Invidious::Database::Videos.update(video) Invidious::Database::Videos.update(video)
rescue ex rescue ex
Invidious::Database::Videos.delete(id) Invidious::Database::Videos.delete(id)
@ -1089,15 +1089,15 @@ def get_video(id, refresh = true, region = nil, force_refresh = false)
end end
end end
else else
video = fetch_video(id, region) video = fetch_video(id, region, clip_urls)
Invidious::Database::Videos.insert(video) if !region Invidious::Database::Videos.insert(video) if !region
end end
return video return video
end end
def fetch_video(id, region) def fetch_video(id, region, clip_urls)
info = extract_video_info(video_id: id) info = extract_video_info(video_id: id, clip_urls: clip_urls)
allowed_regions = info allowed_regions = info
.dig?("microformat", "playerMicroformatRenderer", "availableCountries") .dig?("microformat", "playerMicroformatRenderer", "availableCountries")
@ -1108,7 +1108,7 @@ def fetch_video(id, region)
bypass_regions = PROXY_LIST.keys & allowed_regions bypass_regions = PROXY_LIST.keys & allowed_regions
if !bypass_regions.empty? if !bypass_regions.empty?
region = bypass_regions[rand(bypass_regions.size)] region = bypass_regions[rand(bypass_regions.size)]
region_info = extract_video_info(video_id: id, proxy_region: region) region_info = extract_video_info(video_id: id, proxy_region: region, clip_urls: clip_urls)
region_info["region"] = JSON::Any.new(region) if region region_info["region"] = JSON::Any.new(region) if region
info = region_info if !region_info["reason"]? info = region_info if !region_info["reason"]?
end end
@ -1116,7 +1116,7 @@ def fetch_video(id, region)
# Try to fetch video info using an embedded client # Try to fetch video info using an embedded client
if info["reason"]? if info["reason"]?
embed_info = extract_video_info(video_id: id, context_screen: "embed") embed_info = extract_video_info(video_id: id, context_screen: "embed", clip_urls: clip_urls)
info = embed_info if !embed_info["reason"]? info = embed_info if !embed_info["reason"]?
end end