diff options
| author | techmetx11 <techmetx11@disroot.org> | 2023-03-23 05:10:21 +0000 |
|---|---|---|
| committer | techmetx11 <techmetx11@disroot.org> | 2023-03-23 05:10:21 +0000 |
| commit | 49ddf8b6bdb98c7a9678cbec800c45350a54a786 (patch) | |
| tree | 449e603f9c4a55bbd06e4a5d10234b4cd6b329c4 /src | |
| parent | 8a44bd11d2abab8c9203d6dc3c3d5824e5e26327 (diff) | |
| download | invidious-49ddf8b6bdb98c7a9678cbec800c45350a54a786.tar.gz invidious-49ddf8b6bdb98c7a9678cbec800c45350a54a786.tar.bz2 invidious-49ddf8b6bdb98c7a9678cbec800c45350a54a786.zip | |
Added attributed description support
Diffstat (limited to 'src')
| -rw-r--r-- | src/invidious/videos/description.cr | 81 | ||||
| -rw-r--r-- | src/invidious/videos/parser.cr | 6 |
2 files changed, 85 insertions, 2 deletions
diff --git a/src/invidious/videos/description.cr b/src/invidious/videos/description.cr new file mode 100644 index 00000000..d4c60a84 --- /dev/null +++ b/src/invidious/videos/description.cr @@ -0,0 +1,81 @@ +require "json" +require "uri" + +def parse_command(command : JSON::Any?, string : String) : String? + on_tap = command.dig?("onTap", "innertubeCommand") + + # 3rd party URL, extract original URL from YouTube tracking URL + if url_endpoint = on_tap.try &.["urlEndpoint"]? + youtube_url = URI.parse url_endpoint["url"].as_s + + original_url = youtube_url.query_params["q"]? + if original_url.nil? + return "" + else + return "<a href=\"#{original_url}\">#{original_url}</a>" + end + # 1st party watch URL + elsif watch_endpoint = on_tap.try &.["watchEndpoint"]? + video_id = watch_endpoint["videoId"].as_s + time = watch_endpoint["startTimeSeconds"].as_i + + url = "/watch?v=#{video_id}&t=#{time}s" + + # if text is a timestamp, use the string instead + if /(?:\d{2}:){1,2}\d{2}/ =~ string + return "<a href=\"#{url}\">#{string}</a>" + else + return "<a href=\"#{url}\">#{url}</a>" + end + # hashtag/other browse URLs + elsif browse_endpoint = on_tap.try &.dig?("commandMetadata", "webCommandMetadata") + url = browse_endpoint["url"].try &.as_s + + # remove unnecessary character in a channel name + if browse_endpoint["webPageType"]?.try &.as_s == "WEB_PAGE_TYPE_CHANNEL" + name = string.match(/@[\w\d]+/) + if name.try &.[0]? + return "<a href=\"#{url}\">#{name.try &.[0]}</a>" + end + end + + return "<a href=\"#{url}\">#{string}</a>" + end + + return "(unknown YouTube desc command)" +end + +def parse_description(desc : JSON::Any?) : String? + if desc.nil? + return "" + end + + content = desc["content"].as_s + if content.empty? + return "" + end + + if commands = desc["commandRuns"]?.try &.as_a + description = String.build do |str| + index = 0 + commands.each do |command| + start_index = command["startIndex"].as_i + length = command["length"].as_i + + if start_index > 0 && start_index - index > 0 + str << content[index..(start_index - 1)] + index += start_index - index + end + + str << parse_command(command, content[start_index, length]) + index += length + end + if index < content.size + str << content[index..content.size] + end + end + return description + end + + return content +end diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index 608ae99d..3a342a95 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -284,8 +284,10 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any description = microformat.dig?("description", "simpleText").try &.as_s || "" short_description = player_response.dig?("videoDetails", "shortDescription") - description_html = video_secondary_renderer.try &.dig?("description", "runs") - .try &.as_a.try { |t| content_to_comment_html(t, video_id) } + # description_html = video_secondary_renderer.try &.dig?("description", "runs") + # .try &.as_a.try { |t| content_to_comment_html(t, video_id) } + + description_html = parse_description(video_secondary_renderer.try &.dig?("attributedDescription")) # Video metadata |
