Add JSON mapping for captions

This commit is contained in:
Omar Roth 2018-08-06 18:25:25 -05:00
parent f3646dc0bb
commit 6c71227766
4 changed files with 39 additions and 18 deletions

View file

@ -87,6 +87,7 @@ CAPTION_LANGUAGES = [
"Somali",
"Southern Sotho",
"Spanish",
"Spanish (Latin America)",
"Sundanese",
"Swahili",
"Swedish",
@ -164,10 +165,16 @@ class Video
def captions
player_response = JSON.parse(self.info["player_response"])
captions = [] of Caption
if player_response["captions"]?
captions = player_response["captions"]["playerCaptionsTracklistRenderer"]["captionTracks"]?.try &.as_a
caption_list = player_response["captions"]["playerCaptionsTracklistRenderer"]["captionTracks"].as_a
caption_list.each do |caption|
caption = Caption.from_json(caption.to_json)
caption.name.simpleText = caption.name.simpleText.split(" - ")[0]
captions << caption
end
end
captions ||= [] of JSON::Any
return captions
end
@ -207,6 +214,20 @@ class Video
})
end
class Caption
JSON.mapping(
name: CaptionName,
baseUrl: String,
languageCode: String
)
end
class CaptionName
JSON.mapping(
simpleText: String,
)
end
def get_video(id, db, refresh = true)
if db.query_one?("SELECT EXISTS (SELECT true FROM videos WHERE id = $1)", id, as: Bool)
video = db.query_one("SELECT * FROM videos WHERE id = $1", id, as: Video)

View file

@ -56,8 +56,8 @@ video, #my_video, .video-js, .vjs-default-skin
<% end %>
<% captions.each do |caption| %>
<track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption["name"]["simpleText"] %>"
label="<%= caption["name"]["simpleText"]%> ">
<track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption.name.simpleText %>"
label="<%= caption.name.simpleText %> ">
<% end %>
<% end %>
</video>

View file

@ -61,14 +61,14 @@
<% end %>
<% preferred_captions.each_with_index do |caption, i| %>
<track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption["name"]["simpleText"] %>"
label="<%= caption["name"]["simpleText"]%>" <% if i == 0 %>default<% end %>>
<track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption.name.simpleText %>"
label="<%= caption.name.simpleText %>" <% if i == 0 %>default<% end %>>
<% end %>
<% end %>
<% captions.each do |caption| %>
<track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption["name"]["simpleText"] %>"
label="<%= caption["name"]["simpleText"]%>">
<track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption.name.simpleText %>"
label="<%= caption.name.simpleText %>">
<% end %>
<% end %>
</video>