From 2fe8f0d89e0601c2527807855391213fea2a6745 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Sat, 12 Nov 2022 16:37:52 +0100 Subject: Add ability to link to a timecode on youtube --- lib/recording.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'lib') 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 -- cgit v1.2.3