From 9a78b55c53aae6b2d957f5563ebe01e1d12247ca Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Fri, 10 Aug 2018 08:44:23 -0500 Subject: [PATCH] Pull description into seperate function --- src/invidious.cr | 34 ++++++++------------------------ src/invidious/channels.cr | 2 +- src/invidious/helpers/helpers.cr | 4 ++-- src/invidious/search.cr | 10 +--------- 4 files changed, 12 insertions(+), 38 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 714a3e34b..7a6100bf6 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -1860,11 +1860,9 @@ get "/api/v1/videos/:id" do |env| end end - description = video.description.gsub("
", "\n") - description = description.gsub("
", "\n") - description = XML.parse_html(description) + description = html_to_description(video.description) - json.field "description", description.content + json.field "description", description json.field "descriptionHtml", video.description json.field "published", video.published.epoch json.field "keywords" do @@ -2057,16 +2055,8 @@ get "/api/v1/trending" do |env| end end - descriptionHtml = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-description")])) - if !descriptionHtml - description = "" - descriptionHtml = "" - else - descriptionHtml = descriptionHtml.to_s - description = descriptionHtml.gsub("
", "\n") - description = description.gsub("
", "\n") - description = XML.parse_html(description).content.strip("\n ") - end + description_html = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-description")])) + description, description_html = html_to_description(description_html) length_seconds = decode_length_seconds(node.xpath_node(%q(.//span[@class="video-time"])).not_nil!.content) @@ -2098,7 +2088,7 @@ get "/api/v1/trending" do |env| json.field "published", published.epoch json.field "description", description - json.field "descriptionHtml", descriptionHtml + json.field "descriptionHtml", description_html end end end @@ -2339,16 +2329,8 @@ get "/api/v1/channels/:ucid/videos" do |env| end end - descriptionHtml = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-description")])) - if !descriptionHtml - description = "" - descriptionHtml = "" - else - descriptionHtml = descriptionHtml.to_s - description = descriptionHtml.gsub("
", "\n") - description = description.gsub("
", "\n") - description = XML.parse_html(description).content.strip("\n ") - end + description_html = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-description")])) + description, description_html = html_to_description(description_html) length_seconds = decode_length_seconds(node.xpath_node(%q(.//span[@class="video-time"])).not_nil!.content) @@ -2374,7 +2356,7 @@ get "/api/v1/channels/:ucid/videos" do |env| end json.field "description", description - json.field "descriptionHtml", descriptionHtml + json.field "descriptionHtml", description_html json.field "viewCount", view_count json.field "published", published.epoch diff --git a/src/invidious/channels.cr b/src/invidious/channels.cr index a78e7c642..fc5f90f8b 100644 --- a/src/invidious/channels.cr +++ b/src/invidious/channels.cr @@ -164,7 +164,7 @@ def extract_channel_videos(document, author, ucid) end description_html = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-description")])) - description = description_html_to_description(description_html) + description, description_html = html_to_description(description_html) length_seconds = node.xpath_node(%q(.//span[@class="video-time"])) if length_seconds diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.cr index 3db160bd9..f3c2c2958 100644 --- a/src/invidious/helpers/helpers.cr +++ b/src/invidious/helpers/helpers.cr @@ -273,7 +273,7 @@ def generate_captcha(key) return {challenge: challenge, token: token} end -def description_html_to_description(description_html) +def html_to_description(description_html) if !description_html description = "" description_html = "" @@ -284,5 +284,5 @@ def description_html_to_description(description_html) description = XML.parse_html(description).content.strip("\n ") end - return description + return description, description_html end diff --git a/src/invidious/search.cr b/src/invidious/search.cr index e66d76fd8..82b14dcc3 100644 --- a/src/invidious/search.cr +++ b/src/invidious/search.cr @@ -81,15 +81,7 @@ def search(query, page = 1, search_params = build_search_params(content_type: "v end description_html = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-description")])) - if !description_html - description = "" - description_html = "" - else - description_html = description_html.to_s - description = description_html.gsub("
", "\n") - description = description.gsub("
", "\n") - description = XML.parse_html(description).content.strip("\n ") - end + description, description_html = html_to_description(description_html) length_seconds = node.xpath_node(%q(.//span[@class="video-time"])) if length_seconds