summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/invidious.cr105
-rw-r--r--src/invidious/videos.cr15
2 files changed, 97 insertions, 23 deletions
diff --git a/src/invidious.cr b/src/invidious.cr
index f3404d23..69b825ce 100644
--- a/src/invidious.cr
+++ b/src/invidious.cr
@@ -2262,7 +2262,11 @@ get "/api/v1/captions/:id" do |env|
end
end
- next response
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ next JSON.parse(response).to_pretty_json
+ else
+ next response
+ end
end
env.response.content_type = "text/vtt"
@@ -2368,13 +2372,24 @@ get "/api/v1/comments/:id" do |env|
if format == "json"
reddit_thread = JSON.parse(reddit_thread.to_json).as_h
reddit_thread["comments"] = JSON.parse(comments.to_json)
- next reddit_thread.to_json
+
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ next reddit_thread.to_pretty_json
+ else
+ next reddit_thread.to_json
+ end
else
- next {
+ response = {
"title" => reddit_thread.title,
"permalink" => reddit_thread.permalink,
"contentHtml" => content_html,
- }.to_json
+ }
+
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ next response.to_pretty_json
+ else
+ next response.to_json
+ end
end
end
end
@@ -2385,6 +2400,9 @@ get "/api/v1/insights/:id" do |env|
id = env.params.url["id"]
env.response.content_type = "application/json"
+ error_message = {"error" => "YouTube has removed publicly-available analytics."}.to_json
+ halt env, status_code: 503, response: error_message
+
client = make_client(YT_URL)
headers = HTTP::Headers.new
html = client.get("/watch?v=#{id}&gl=US&hl=en&disable_polymer=1")
@@ -2451,14 +2469,20 @@ get "/api/v1/insights/:id" do |env|
avg_view_duration_seconds = html_content.xpath_node(%q(//div[@id="stats-chart-tab-watch-time"]/span/span[2])).not_nil!.content
avg_view_duration_seconds = decode_length_seconds(avg_view_duration_seconds)
- {
+ response = {
"viewCount" => view_count,
"timeWatchedText" => time_watched,
"subscriptionsDriven" => subscriptions_driven,
"shares" => shares,
"avgViewDurationSeconds" => avg_view_duration_seconds,
"graphData" => graph_data,
- }.to_json
+ }
+
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ next response.to_pretty_json
+ else
+ next response.to_json
+ end
end
get "/api/v1/videos/:id" do |env|
@@ -2664,12 +2688,18 @@ get "/api/v1/videos/:id" do |env|
end
end
- video_info
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ JSON.parse(video_info).to_pretty_json
+ else
+ video_info
+ end
end
get "/api/v1/trending" do |env|
locale = LOCALES[env.get("locale").as(String)]?
+ env.response.content_type = "application/json"
+
region = env.params.query["region"]?
trending_type = env.params.query["type"]?
@@ -2709,13 +2739,18 @@ get "/api/v1/trending" do |env|
end
end
- env.response.content_type = "application/json"
- videos
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ JSON.parse(videos).to_pretty_json
+ else
+ videos
+ end
end
get "/api/v1/popular" do |env|
locale = LOCALES[env.get("locale").as(String)]?
+ env.response.content_type = "application/json"
+
videos = JSON.build do |json|
json.array do
popular_videos.each do |video|
@@ -2738,13 +2773,18 @@ get "/api/v1/popular" do |env|
end
end
- env.response.content_type = "application/json"
- videos
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ JSON.parse(videos).to_pretty_json
+ else
+ videos
+ end
end
get "/api/v1/top" do |env|
locale = LOCALES[env.get("locale").as(String)]?
+ env.response.content_type = "application/json"
+
videos = JSON.build do |json|
json.array do
top_videos.each do |video|
@@ -2774,8 +2814,11 @@ get "/api/v1/top" do |env|
end
end
- env.response.content_type = "application/json"
- videos
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ JSON.parse(videos).to_pretty_json
+ else
+ videos
+ end
end
get "/api/v1/channels/:ucid" do |env|
@@ -2972,7 +3015,11 @@ get "/api/v1/channels/:ucid" do |env|
end
end
- channel_info
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ JSON.parse(channel_info).to_pretty_json
+ else
+ channel_info
+ end
end
["/api/v1/channels/:ucid/videos", "/api/v1/channels/videos/:ucid"].each do |route|
@@ -3037,7 +3084,11 @@ end
end
end
- result
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ JSON.parse(result).to_pretty_json
+ else
+ result
+ end
end
end
@@ -3138,7 +3189,11 @@ get "/api/v1/channels/search/:ucid" do |env|
end
end
- response
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ JSON.parse(response).to_pretty_json
+ else
+ response
+ end
end
get "/api/v1/search" do |env|
@@ -3263,7 +3318,11 @@ get "/api/v1/search" do |env|
end
end
- response
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ JSON.parse(response).to_pretty_json
+ else
+ response
+ end
end
get "/api/v1/playlists/:plid" do |env|
@@ -3362,7 +3421,11 @@ get "/api/v1/playlists/:plid" do |env|
}.to_json
end
- response
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ JSON.parse(response).to_pretty_json
+ else
+ response
+ end
end
get "/api/v1/mixes/:rdid" do |env|
@@ -3436,7 +3499,11 @@ get "/api/v1/mixes/:rdid" do |env|
}.to_json
end
- response
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ JSON.parse(response).to_pretty_json
+ else
+ response
+ end
end
get "/api/manifest/dash/id/videoplayback" do |env|
diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr
index 18a2ff8a..ec911e3f 100644
--- a/src/invidious/videos.cr
+++ b/src/invidious/videos.cr
@@ -670,13 +670,20 @@ def fetch_video(id, proxies, region)
genre = html.xpath_node(%q(//meta[@itemprop="genre"])).try &.["content"]
genre ||= ""
- genre_url = html.xpath_node(%(//a[text()="#{genre}"])).try &.["href"]
+ genre_url = html.xpath_node(%(//ul[contains(@class, "watch-info-tag-list")]/li/a[text()="#{genre}"])).try &.["href"]
+
+ # Sometimes YouTube tries to link to invalid/missing channels, so we fix that here
case genre
+ when "Education"
+ genre_url = "/channel/UCdxpofrI-dO6oYfsqHDHphw"
+ when "Gaming"
+ genre_url = "/channel/UCOpNcN46UbXVtpKMrmU4Abg"
when "Movies"
genre_url = "/channel/UClgRkhTL3_hImCAmdLfDE4g"
- when "Education"
- # Education channel is linked but does not exist
- genre_url = "/channel/UC3yA8nDwraeOfnYfBWun83g"
+ when "Nonprofits & Activism"
+ genre_url = "/channel/UCfFyYRYslvuhwMDnx6KjUvw"
+ when "Trailers"
+ genre_url = "/channel/UClgRkhTL3_hImCAmdLfDE4g"
end
genre_url ||= ""