summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOmar Roth <omarroth@hotmail.com>2018-08-06 18:25:25 -0500
committerOmar Roth <omarroth@hotmail.com>2018-08-06 18:25:25 -0500
commit6c71227766ce43b5ae9738cd37c128dd9c7349f5 (patch)
tree3be51ba92968aa99337db641edf7e7e19f9cc0a5 /src
parentf3646dc0bb172b98a3a4e489d7d3cfa212b458bd (diff)
downloadinvidious-6c71227766ce43b5ae9738cd37c128dd9c7349f5.tar.gz
invidious-6c71227766ce43b5ae9738cd37c128dd9c7349f5.tar.bz2
invidious-6c71227766ce43b5ae9738cd37c128dd9c7349f5.zip
Add JSON mapping for captions
Diffstat (limited to 'src')
-rw-r--r--src/invidious.cr20
-rw-r--r--src/invidious/videos.cr25
-rw-r--r--src/invidious/views/embed.ecr4
-rw-r--r--src/invidious/views/watch.ecr8
4 files changed, 39 insertions, 18 deletions
diff --git a/src/invidious.cr b/src/invidious.cr
index ce818b69..df3d4cfe 100644
--- a/src/invidious.cr
+++ b/src/invidious.cr
@@ -216,12 +216,12 @@ get "/watch" do |env|
captions = video.captions
if preferences
- preferred_captions = captions.select { |caption| preferences.captions.includes? caption["name"]["simpleText"] }
- preferred_captions.sort_by! { |caption| preferences.captions.index(caption["name"]["simpleText"]).not_nil! }
+ preferred_captions = captions.select { |caption| preferences.captions.includes? caption.name.simpleText }
+ preferred_captions.sort_by! { |caption| preferences.captions.index(caption.name.simpleText).not_nil! }
captions = captions - preferred_captions
end
- preferred_captions ||= [] of JSON::Any
+ preferred_captions ||= [] of Caption
video.description = fill_links(video.description, "https", "www.youtube.com")
video.description = add_alt_links(video.description)
@@ -1586,8 +1586,8 @@ get "/api/v1/captions/:id" do |env|
json.array do
captions.each do |caption|
json.object do
- json.field "label", caption["name"]["simpleText"]
- json.field "languageCode", caption["languageCode"]
+ json.field "label", caption.name.simpleText
+ json.field "languageCode", caption.languageCode
end
end
end
@@ -1598,7 +1598,7 @@ get "/api/v1/captions/:id" do |env|
next response
end
- caption = captions.select { |caption| caption["name"]["simpleText"] == label }
+ caption = captions.select { |caption| caption.name.simpleText == label }
env.response.content_type = "text/vtt"
if caption.empty?
@@ -1607,13 +1607,13 @@ get "/api/v1/captions/:id" do |env|
caption = caption[0]
end
- caption_xml = client.get(caption["baseUrl"].as_s).body
+ caption_xml = client.get(caption.baseUrl).body
caption_xml = XML.parse(caption_xml)
webvtt = <<-END_VTT
WEBVTT
Kind: captions
- Language: #{caption["languageCode"]}
+ Language: #{caption.languageCode}
END_VTT
@@ -1965,8 +1965,8 @@ get "/api/v1/videos/:id" do |env|
json.array do
captions.each do |caption|
json.object do
- json.field "label", caption["name"]["simpleText"]
- json.field "languageCode", caption["languageCode"]
+ json.field "label", caption.name.simpleText
+ json.field "languageCode", caption.languageCode
end
end
end
diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr
index 1985ac77..a4b96799 100644
--- a/src/invidious/videos.cr
+++ b/src/invidious/videos.cr
@@ -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)
diff --git a/src/invidious/views/embed.ecr b/src/invidious/views/embed.ecr
index 1786a90b..c63ee028 100644
--- a/src/invidious/views/embed.ecr
+++ b/src/invidious/views/embed.ecr
@@ -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>
diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr
index 1840642d..e3665917 100644
--- a/src/invidious/views/watch.ecr
+++ b/src/invidious/views/watch.ecr
@@ -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>