m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMarcin Chrzanowski <m@m-chrzan.xyz>2022-11-12 16:37:52 +0100
committerMarcin Chrzanowski <m@m-chrzan.xyz>2022-11-12 16:37:52 +0100
commit2fe8f0d89e0601c2527807855391213fea2a6745 (patch)
tree96f1c0c1d5245ca1ded47b59e5fe532360efcf14 /lib
parent4a9830fa21637c52b77c254f1baea7d1486c61a5 (diff)
Add ability to link to a timecode on youtube
Diffstat (limited to 'lib')
-rw-r--r--lib/recording.rb16
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/recording.rb b/lib/recording.rb
index 68b0428..f2ee251 100644
--- a/lib/recording.rb
+++ b/lib/recording.rb
@@ -13,10 +13,24 @@ class YouTubeRecording
@vid = parse_vid link
end
- def parse_vid link
+ def parse_id link
/\?v=(.{11})/.match(link)[1]
end
+ # Timecodes in links have to be specified in seconds (the "start" query for
+ # embeds doesn't understand "XmYs" like "t" on regular links).
+ def parse_timequery link
+ match = /&t=([0-9]+)/.match(link)
+
+ match ? "?start=#{match[1]}" : ""
+ end
+
+ def parse_vid link
+ id = parse_id link
+ timequery = parse_timequery link
+ "#{id}#{timequery}"
+ end
+
def render
template = ERB.new(File.read('templates/youtube.html.erb'), trim_mode: '-')
template.result binding