Add 'pretty=1' option to API endpoints

This commit is contained in:
Omar Roth 2019-01-25 10:50:18 -06:00
parent 586c0a0579
commit ba36ab9559

View File

@ -2262,8 +2262,12 @@ get "/api/v1/captions/:id" do |env|
end end
end end
if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
next JSON.parse(response).to_pretty_json
else
next response next response
end end
end
env.response.content_type = "text/vtt" env.response.content_type = "text/vtt"
@ -2368,13 +2372,24 @@ get "/api/v1/comments/:id" do |env|
if format == "json" if format == "json"
reddit_thread = JSON.parse(reddit_thread.to_json).as_h reddit_thread = JSON.parse(reddit_thread.to_json).as_h
reddit_thread["comments"] = JSON.parse(comments.to_json) 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 else
next { next reddit_thread.to_json
end
else
response = {
"title" => reddit_thread.title, "title" => reddit_thread.title,
"permalink" => reddit_thread.permalink, "permalink" => reddit_thread.permalink,
"contentHtml" => content_html, "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 end
end end
@ -2454,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 = 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) avg_view_duration_seconds = decode_length_seconds(avg_view_duration_seconds)
{ response = {
"viewCount" => view_count, "viewCount" => view_count,
"timeWatchedText" => time_watched, "timeWatchedText" => time_watched,
"subscriptionsDriven" => subscriptions_driven, "subscriptionsDriven" => subscriptions_driven,
"shares" => shares, "shares" => shares,
"avgViewDurationSeconds" => avg_view_duration_seconds, "avgViewDurationSeconds" => avg_view_duration_seconds,
"graphData" => graph_data, "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 end
get "/api/v1/videos/:id" do |env| get "/api/v1/videos/:id" do |env|
@ -2667,12 +2688,18 @@ get "/api/v1/videos/:id" do |env|
end end
end end
if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
JSON.parse(video_info).to_pretty_json
else
video_info video_info
end end
end
get "/api/v1/trending" do |env| get "/api/v1/trending" do |env|
locale = LOCALES[env.get("locale").as(String)]? locale = LOCALES[env.get("locale").as(String)]?
env.response.content_type = "application/json"
region = env.params.query["region"]? region = env.params.query["region"]?
trending_type = env.params.query["type"]? trending_type = env.params.query["type"]?
@ -2712,13 +2739,18 @@ get "/api/v1/trending" do |env|
end end
end end
env.response.content_type = "application/json" if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
JSON.parse(videos).to_pretty_json
else
videos videos
end end
end
get "/api/v1/popular" do |env| get "/api/v1/popular" do |env|
locale = LOCALES[env.get("locale").as(String)]? locale = LOCALES[env.get("locale").as(String)]?
env.response.content_type = "application/json"
videos = JSON.build do |json| videos = JSON.build do |json|
json.array do json.array do
popular_videos.each do |video| popular_videos.each do |video|
@ -2741,13 +2773,18 @@ get "/api/v1/popular" do |env|
end end
end end
env.response.content_type = "application/json" if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
JSON.parse(videos).to_pretty_json
else
videos videos
end end
end
get "/api/v1/top" do |env| get "/api/v1/top" do |env|
locale = LOCALES[env.get("locale").as(String)]? locale = LOCALES[env.get("locale").as(String)]?
env.response.content_type = "application/json"
videos = JSON.build do |json| videos = JSON.build do |json|
json.array do json.array do
top_videos.each do |video| top_videos.each do |video|
@ -2777,9 +2814,12 @@ get "/api/v1/top" do |env|
end end
end end
env.response.content_type = "application/json" if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
JSON.parse(videos).to_pretty_json
else
videos videos
end end
end
get "/api/v1/channels/:ucid" do |env| get "/api/v1/channels/:ucid" do |env|
locale = LOCALES[env.get("locale").as(String)]? locale = LOCALES[env.get("locale").as(String)]?
@ -2975,8 +3015,12 @@ get "/api/v1/channels/:ucid" do |env|
end end
end end
if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
JSON.parse(channel_info).to_pretty_json
else
channel_info channel_info
end end
end
["/api/v1/channels/:ucid/videos", "/api/v1/channels/videos/:ucid"].each do |route| ["/api/v1/channels/:ucid/videos", "/api/v1/channels/videos/:ucid"].each do |route|
get route do |env| get route do |env|
@ -3040,9 +3084,13 @@ end
end end
end end
if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
JSON.parse(result).to_pretty_json
else
result result
end end
end end
end
get "/api/v1/channels/search/:ucid" do |env| get "/api/v1/channels/search/:ucid" do |env|
locale = LOCALES[env.get("locale").as(String)]? locale = LOCALES[env.get("locale").as(String)]?
@ -3141,8 +3189,12 @@ get "/api/v1/channels/search/:ucid" do |env|
end end
end end
if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
JSON.parse(response).to_pretty_json
else
response response
end end
end
get "/api/v1/search" do |env| get "/api/v1/search" do |env|
locale = LOCALES[env.get("locale").as(String)]? locale = LOCALES[env.get("locale").as(String)]?
@ -3266,8 +3318,12 @@ get "/api/v1/search" do |env|
end end
end end
if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
JSON.parse(response).to_pretty_json
else
response response
end end
end
get "/api/v1/playlists/:plid" do |env| get "/api/v1/playlists/:plid" do |env|
locale = LOCALES[env.get("locale").as(String)]? locale = LOCALES[env.get("locale").as(String)]?
@ -3365,8 +3421,12 @@ get "/api/v1/playlists/:plid" do |env|
}.to_json }.to_json
end end
if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
JSON.parse(response).to_pretty_json
else
response response
end end
end
get "/api/v1/mixes/:rdid" do |env| get "/api/v1/mixes/:rdid" do |env|
locale = LOCALES[env.get("locale").as(String)]? locale = LOCALES[env.get("locale").as(String)]?
@ -3439,8 +3499,12 @@ get "/api/v1/mixes/:rdid" do |env|
}.to_json }.to_json
end end
if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
JSON.parse(response).to_pretty_json
else
response response
end end
end
get "/api/manifest/dash/id/videoplayback" do |env| get "/api/manifest/dash/id/videoplayback" do |env|
env.response.headers["Access-Control-Allow-Origin"] = "*" env.response.headers["Access-Control-Allow-Origin"] = "*"