Add 'to_json' into respective structs

This commit is contained in:
Omar Roth 2019-06-08 13:31:41 -05:00
parent 1c9085556c
commit 12b2ab5da8
No known key found for this signature in database
GPG Key ID: B8254FB7EC3D37F2
7 changed files with 307 additions and 451 deletions

View File

@ -3405,28 +3405,7 @@ get "/api/v1/trending" do |env|
videos = JSON.build do |json| videos = JSON.build do |json|
json.array do json.array do
trending.each do |video| trending.each do |video|
json.object do video.to_json(locale, config, Kemal.config, json)
json.field "title", video.title
json.field "videoId", video.id
json.field "videoThumbnails" do
generate_thumbnails(json, video.id, config, Kemal.config)
end
json.field "lengthSeconds", video.length_seconds
json.field "viewCount", video.views
json.field "author", video.author
json.field "authorId", video.ucid
json.field "authorUrl", "/channel/#{video.ucid}"
json.field "published", video.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
json.field "description", video.description
json.field "descriptionHtml", video.description_html
json.field "liveNow", video.live_now
json.field "paid", video.paid
json.field "premium", video.premium
end
end end
end end
end end
@ -3459,9 +3438,12 @@ get "/api/v1/top" do |env|
next error_message next error_message
end end
videos = JSON.build do |json| JSON.build do |json|
json.array do json.array do
top_videos.each do |video| top_videos.each do |video|
# Top videos have much more information than provided below (adaptiveFormats, etc)
# but can be very out of date, so we only provide a subset here
json.object do json.object do
json.field "title", video.title json.field "title", video.title
json.field "videoId", video.id json.field "videoId", video.id
@ -3487,8 +3469,6 @@ get "/api/v1/top" do |env|
end end
end end
end end
videos
end end
get "/api/v1/channels/:ucid" do |env| get "/api/v1/channels/:ucid" do |env|
@ -3577,6 +3557,7 @@ get "/api/v1/channels/:ucid" do |env|
end end
channel_info = JSON.build do |json| channel_info = JSON.build do |json|
# TODO: Refactor into `to_json` for InvidiousChannel
json.object do json.object do
json.field "author", author json.field "author", author
json.field "authorId", ucid json.field "authorId", ucid
@ -3634,35 +3615,7 @@ get "/api/v1/channels/:ucid" do |env|
json.field "latestVideos" do json.field "latestVideos" do
json.array do json.array do
videos.each do |video| videos.each do |video|
json.object do video.to_json(locale, config, Kemal.config, json)
json.field "title", video.title
json.field "videoId", video.id
if auto_generated
json.field "author", video.author
json.field "authorId", video.ucid
json.field "authorUrl", "/channel/#{video.ucid}"
else
json.field "author", author
json.field "authorId", ucid
json.field "authorUrl", "/channel/#{ucid}"
end
json.field "videoThumbnails" do
generate_thumbnails(json, video.id, config, Kemal.config)
end
json.field "description", video.description
json.field "descriptionHtml", video.description_html
json.field "viewCount", video.views
json.field "published", video.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
json.field "lengthSeconds", video.length_seconds
json.field "liveNow", video.live_now
json.field "paid", video.paid
json.field "premium", video.premium
end
end end
end end
end end
@ -3727,44 +3680,14 @@ end
next error_message next error_message
end end
result = JSON.build do |json| JSON.build do |json|
json.array do json.array do
videos.each do |video| videos.each do |video|
json.object do video.to_json(locale, config, Kemal.config, json)
json.field "title", video.title
json.field "videoId", video.id
if auto_generated
json.field "author", video.author
json.field "authorId", video.ucid
json.field "authorUrl", "/channel/#{video.ucid}"
else
json.field "author", author
json.field "authorId", ucid
json.field "authorUrl", "/channel/#{ucid}"
end
json.field "videoThumbnails" do
generate_thumbnails(json, video.id, config, Kemal.config)
end
json.field "description", video.description
json.field "descriptionHtml", video.description_html
json.field "viewCount", video.views
json.field "published", video.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
json.field "lengthSeconds", video.length_seconds
json.field "liveNow", video.live_now
json.field "paid", video.paid
json.field "premium", video.premium
end end
end end
end end
end end
result
end
end end
{"/api/v1/channels/:ucid/latest", "/api/v1/channels/latest/:ucid"}.each do |route| {"/api/v1/channels/:ucid/latest", "/api/v1/channels/latest/:ucid"}.each do |route|
@ -3786,28 +3709,7 @@ end
JSON.build do |json| JSON.build do |json|
json.array do json.array do
videos.each do |video| videos.each do |video|
json.object do video.to_json(locale, config, Kemal.config, json)
json.field "title", video.title
json.field "videoId", video.id
json.field "authorId", ucid
json.field "authorUrl", "/channel/#{ucid}"
json.field "videoThumbnails" do
generate_thumbnails(json, video.id, config, Kemal.config)
end
json.field "description", video.description
json.field "descriptionHtml", video.description_html
json.field "viewCount", video.views
json.field "published", video.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
json.field "lengthSeconds", video.length_seconds
json.field "liveNow", video.live_now
json.field "paid", video.paid
json.field "premium", video.premium
end
end end
end end
end end
@ -3841,32 +3743,8 @@ end
json.field "playlists" do json.field "playlists" do
json.array do json.array do
items.each do |item| items.each do |item|
json.object do
if item.is_a?(SearchPlaylist) if item.is_a?(SearchPlaylist)
json.field "title", item.title item.to_json(locale, config, Kemal.config, json)
json.field "playlistId", item.id
json.field "author", item.author
json.field "authorId", item.ucid
json.field "authorUrl", "/channel/#{item.ucid}"
json.field "videoCount", item.video_count
json.field "videos" do
json.array do
item.videos.each do |video|
json.object do
json.field "title", video.title
json.field "videoId", video.id
json.field "lengthSeconds", video.length_seconds
json.field "videoThumbnails" do
generate_thumbnails(json, video.id, config, Kemal.config)
end
end
end
end
end
end
end end
end end
end end
@ -3894,91 +3772,14 @@ get "/api/v1/channels/search/:ucid" do |env|
page ||= 1 page ||= 1
count, search_results = channel_search(query, page, ucid) count, search_results = channel_search(query, page, ucid)
response = JSON.build do |json| JSON.build do |json|
json.array do json.array do
search_results.each do |item| search_results.each do |item|
json.object do item.to_json(locale, config, Kemal.config, json)
case item
when SearchVideo
json.field "type", "video"
json.field "title", item.title
json.field "videoId", item.id
json.field "author", item.author
json.field "authorId", item.ucid
json.field "authorUrl", "/channel/#{item.ucid}"
json.field "videoThumbnails" do
generate_thumbnails(json, item.id, config, Kemal.config)
end
json.field "description", item.description
json.field "descriptionHtml", item.description_html
json.field "viewCount", item.views
json.field "published", item.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(item.published, locale))
json.field "lengthSeconds", item.length_seconds
json.field "liveNow", item.live_now
json.field "paid", item.paid
json.field "premium", item.premium
when SearchPlaylist
json.field "type", "playlist"
json.field "title", item.title
json.field "playlistId", item.id
json.field "author", item.author
json.field "authorId", item.ucid
json.field "authorUrl", "/channel/#{item.ucid}"
json.field "videoCount", item.video_count
json.field "videos" do
json.array do
item.videos.each do |video|
json.object do
json.field "title", video.title
json.field "videoId", video.id
json.field "lengthSeconds", video.length_seconds
json.field "videoThumbnails" do
generate_thumbnails(json, video.id, config, Kemal.config)
end end
end end
end end
end end
end
when SearchChannel
json.field "type", "channel"
json.field "author", item.author
json.field "authorId", item.ucid
json.field "authorUrl", "/channel/#{item.ucid}"
json.field "authorThumbnails" do
json.array do
qualities = {32, 48, 76, 100, 176, 512}
qualities.each do |quality|
json.object do
json.field "url", item.author_thumbnail.gsub("=s176-", "=s#{quality}-")
json.field "width", quality
json.field "height", quality
end
end
end
end
json.field "subCount", item.subscriber_count
json.field "videoCount", item.video_count
json.field "description", item.description
json.field "descriptionHtml", item.description_html
end
end
end
end
end
response
end
get "/api/v1/search" do |env| get "/api/v1/search" do |env|
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
@ -4019,91 +3820,14 @@ get "/api/v1/search" do |env|
end end
count, search_results = search(query, page, search_params, proxies, region).as(Tuple) count, search_results = search(query, page, search_params, proxies, region).as(Tuple)
response = JSON.build do |json| JSON.build do |json|
json.array do json.array do
search_results.each do |item| search_results.each do |item|
json.object do item.to_json(locale, config, Kemal.config, json)
case item
when SearchVideo
json.field "type", "video"
json.field "title", item.title
json.field "videoId", item.id
json.field "author", item.author
json.field "authorId", item.ucid
json.field "authorUrl", "/channel/#{item.ucid}"
json.field "videoThumbnails" do
generate_thumbnails(json, item.id, config, Kemal.config)
end
json.field "description", item.description
json.field "descriptionHtml", item.description_html
json.field "viewCount", item.views
json.field "published", item.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(item.published, locale))
json.field "lengthSeconds", item.length_seconds
json.field "liveNow", item.live_now
json.field "paid", item.paid
json.field "premium", item.premium
when SearchPlaylist
json.field "type", "playlist"
json.field "title", item.title
json.field "playlistId", item.id
json.field "author", item.author
json.field "authorId", item.ucid
json.field "authorUrl", "/channel/#{item.ucid}"
json.field "videoCount", item.video_count
json.field "videos" do
json.array do
item.videos.each do |video|
json.object do
json.field "title", video.title
json.field "videoId", video.id
json.field "lengthSeconds", video.length_seconds
json.field "videoThumbnails" do
generate_thumbnails(json, video.id, config, Kemal.config)
end end
end end
end end
end end
end
when SearchChannel
json.field "type", "channel"
json.field "author", item.author
json.field "authorId", item.ucid
json.field "authorUrl", "/channel/#{item.ucid}"
json.field "authorThumbnails" do
json.array do
qualities = {32, 48, 76, 100, 176, 512}
qualities.each do |quality|
json.object do
json.field "url", item.author_thumbnail.gsub("=s176-", "=s#{quality}-")
json.field "width", quality
json.field "height", quality
end
end
end
end
json.field "subCount", item.subscriber_count
json.field "videoCount", item.video_count
json.field "description", item.description
json.field "descriptionHtml", item.description_html
end
end
end
end
end
response
end
get "/api/v1/playlists/:plid" do |env| get "/api/v1/playlists/:plid" do |env|
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
@ -4170,21 +3894,7 @@ get "/api/v1/playlists/:plid" do |env|
json.field "videos" do json.field "videos" do
json.array do json.array do
videos.each do |video| videos.each do |video|
json.object do video.to_json(locale, config, Kemal.config, json)
json.field "title", video.title
json.field "videoId", video.id
json.field "author", video.author
json.field "authorId", video.ucid
json.field "authorUrl", "/channel/#{video.ucid}"
json.field "videoThumbnails" do
generate_thumbnails(json, video.id, config, Kemal.config)
end
json.field "index", video.index
json.field "lengthSeconds", video.length_seconds
end
end end
end end
end end

View File

@ -135,9 +135,7 @@ def get_batch_channels(channels, db, refresh = false, pull_all_videos = true, ma
end end
def get_channel(id, db, refresh = true, pull_all_videos = true) def get_channel(id, db, refresh = true, pull_all_videos = true)
if db.query_one?("SELECT EXISTS (SELECT true FROM channels WHERE id = $1)", id, as: Bool) if channel = db.query_one?("SELECT * FROM channels WHERE id = $1", id, as: InvidiousChannel)
channel = db.query_one("SELECT * FROM channels WHERE id = $1", id, as: InvidiousChannel)
if refresh && Time.utc - channel.updated > 10.minutes if refresh && Time.utc - channel.updated > 10.minutes
channel = fetch_channel(id, db, pull_all_videos: pull_all_videos) channel = fetch_channel(id, db, pull_all_videos: pull_all_videos)
channel_array = channel.to_a channel_array = channel.to_a

View File

@ -6,7 +6,7 @@ struct MixVideo
ucid: String, ucid: String,
length_seconds: Int32, length_seconds: Int32,
index: Int32, index: Int32,
mixes: Array(String), rdid: String,
}) })
end end
@ -70,7 +70,7 @@ def fetch_mix(rdid, video_id, cookies = nil, locale = nil)
ucid, ucid,
length_seconds, length_seconds,
index, index,
[rdid] rdid
) )
end end

View File

@ -1,4 +1,32 @@
struct PlaylistVideo struct PlaylistVideo
def to_json(locale, config, kemal_config, json : JSON::Builder)
json.object do
json.field "title", self.title
json.field "videoId", self.id
json.field "author", self.author
json.field "authorId", self.ucid
json.field "authorUrl", "/channel/#{self.ucid}"
json.field "videoThumbnails" do
generate_thumbnails(json, self.id, config, kemal_config)
end
json.field "index", self.index
json.field "lengthSeconds", self.length_seconds
end
end
def to_json(locale, config, kemal_config, json : JSON::Builder | Nil = nil)
if json
to_json(locale, config, kemal_config, json)
else
JSON.build do |json|
to_json(locale, config, kemal_config, json)
end
end
end
db_mapping({ db_mapping({
title: String, title: String,
id: String, id: String,

View File

@ -50,6 +50,43 @@ struct SearchVideo
end end
end end
def to_json(locale, config, kemal_config, json : JSON::Builder)
json.object do
json.field "type", "video"
json.field "title", self.title
json.field "videoId", self.id
json.field "author", self.author
json.field "authorId", self.ucid
json.field "authorUrl", "/channel/#{self.ucid}"
json.field "videoThumbnails" do
generate_thumbnails(json, self.id, config, kemal_config)
end
json.field "description", self.description
json.field "descriptionHtml", self.description_html
json.field "viewCount", self.views
json.field "published", self.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(self.published, locale))
json.field "lengthSeconds", self.length_seconds
json.field "liveNow", self.live_now
json.field "paid", self.paid
json.field "premium", self.premium
end
end
def to_json(locale, config, kemal_config, json : JSON::Builder | Nil = nil)
if json
to_json(locale, config, kemal_config, json)
else
JSON.build do |json|
to_json(locale, config, kemal_config, json)
end
end
end
db_mapping({ db_mapping({
title: String, title: String,
id: String, id: String,
@ -76,6 +113,45 @@ struct SearchPlaylistVideo
end end
struct SearchPlaylist struct SearchPlaylist
def to_json(locale, config, kemal_config, json : JSON::Builder)
json.object do
json.field "type", "playlist"
json.field "title", self.title
json.field "playlistId", self.id
json.field "author", self.author
json.field "authorId", self.ucid
json.field "authorUrl", "/channel/#{self.ucid}"
json.field "videoCount", self.video_count
json.field "videos" do
json.array do
self.videos.each do |video|
json.object do
json.field "title", video.title
json.field "videoId", video.id
json.field "lengthSeconds", video.length_seconds
json.field "videoThumbnails" do
generate_thumbnails(json, video.id, config, Kemal.config)
end
end
end
end
end
end
end
def to_json(locale, config, kemal_config, json : JSON::Builder | Nil = nil)
if json
to_json(locale, config, kemal_config, json)
else
JSON.build do |json|
to_json(locale, config, kemal_config, json)
end
end
end
db_mapping({ db_mapping({
title: String, title: String,
id: String, id: String,
@ -88,6 +164,44 @@ struct SearchPlaylist
end end
struct SearchChannel struct SearchChannel
def to_json(locale, config, kemal_config, json : JSON::Builder)
json.object do
json.field "type", "channel"
json.field "author", self.author
json.field "authorId", self.ucid
json.field "authorUrl", "/channel/#{self.ucid}"
json.field "authorThumbnails" do
json.array do
qualities = {32, 48, 76, 100, 176, 512}
qualities.each do |quality|
json.object do
json.field "url", self.author_thumbnail.gsub("=s176-", "=s#{quality}-")
json.field "width", quality
json.field "height", quality
end
end
end
end
json.field "subCount", self.subscriber_count
json.field "videoCount", self.video_count
json.field "description", self.description
json.field "descriptionHtml", self.description_html
end
end
def to_json(locale, config, kemal_config, json : JSON::Builder | Nil = nil)
if json
to_json(locale, config, kemal_config, json)
else
JSON.build do |json|
to_json(locale, config, kemal_config, json)
end
end
end
db_mapping({ db_mapping({
author: String, author: String,
ucid: String, ucid: String,

View File

@ -273,8 +273,7 @@ struct Video
end end
end end
def to_json(locale, config, kemal_config, decrypt_function) def to_json(locale, config, kemal_config, decrypt_function, json : JSON::Builder)
JSON.build do |json|
json.object do json.object do
json.field "type", "video" json.field "type", "video"
@ -457,6 +456,15 @@ struct Video
end end
end end
end end
def to_json(locale, config, kemal_config, decrypt_function, json : JSON::Builder | Nil = nil)
if json
to_json(locale, config, kemal_config, decrypt_function, json)
else
JSON.build do |json|
to_json(locale, config, kemal_config, decrypt_function, json)
end
end
end end
def allow_ratings def allow_ratings
@ -848,9 +856,7 @@ class VideoRedirect < Exception
end end
def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32}), refresh = true, region = nil, force_refresh = false) def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32}), refresh = true, region = nil, force_refresh = false)
if db.query_one?("SELECT EXISTS (SELECT true FROM videos WHERE id = $1)", id, as: Bool) && !region if (video = db.query_one?("SELECT * FROM videos WHERE id = $1", id, as: Video)) && !region
video = db.query_one("SELECT * FROM videos WHERE id = $1", id, as: Video)
# 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)
if (refresh && if (refresh &&

View File

@ -35,7 +35,7 @@
</b> </b>
</p> </p>
<% when MixVideo %> <% when MixVideo %>
<a style="width:100%" href="/watch?v=<%= item.id %>&list=<%= item.mixes[0] %>"> <a style="width:100%" href="/watch?v=<%= item.id %>&list=<%= item.rdid %>">
<% if !env.get("preferences").as(Preferences).thin_mode %> <% if !env.get("preferences").as(Preferences).thin_mode %>
<div class="thumbnail"> <div class="thumbnail">
<img class="thumbnail" src="/vi/<%= item.id %>/mqdefault.jpg"/> <img class="thumbnail" src="/vi/<%= item.id %>/mqdefault.jpg"/>