Skip to content

Commit

Permalink
Support YouTube /shorts/ URLs.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Jan 2, 2022
1 parent 1bc1e50 commit 0b3ba1d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@
# there is no way to resolve these accurately through the API, the best way is to look for the channelId meta tag in the website HTML
# note that slug != username, e.g. https://www.youtube.com/c/kawaiiguy and https://www.youtube.com/user/kawaiiguy are two different channels
user = "#{type}/#{slug}"
elsif /(?:youtu\.be|youtube\.com\/(?:embed|v|shorts))\/(?<video_id>[^?#]+)/ =~ params[:q]
# https://youtu.be/vVXbgbMp0oY?t=1s
# https://www.youtube.com/embed/vVXbgbMp0oY
# https://www.youtube.com/v/vVXbgbMp0oY
# https://www.youtube.com/shorts/QHEG3OB14GA
elsif /youtube\.com\/tv#\/watch\/video\/.*[?&]v=(?<video_id>[^&]+)/ =~ params[:q]
# https://www.youtube.com/tv#/zylon-detail-surface?c=UCK5eBtuoj_HkdXKHNmBLAXg&resume
# https://www.youtube.com/tv#/watch/video/idle?v=uYMD4elmVIE&resume
Expand All @@ -294,10 +299,6 @@
# https://www.youtube.com/playlist?list=PL0QrZvg7QIgpoLdNFnEePRrU-YJfr9Be7
elsif /youtube\.com\/(?<user>[^\/?#]+)/ =~ params[:q]
# https://www.youtube.com/khanacademy
elsif /(?:youtu\.be|youtube\.com\/(?:embed|v))\/(?<video_id>[^?#]+)/ =~ params[:q]
# https://youtu.be/vVXbgbMp0oY?t=1s
# https://www.youtube.com/embed/vVXbgbMp0oY
# https://www.youtube.com/v/vVXbgbMp0oY
elsif /\b(?<channel_id>(?:UC[^\/?#]{22,}|S[^\/?#]{12,}))/ =~ params[:q]
# it's a channel id
else
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/05-string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def embed_html(request=nil)
<iframe width="1280" height="720" src="https://www.facebook.com/video/embed?video_id=#{id}" frameborder="0" scrolling="no" allowfullscreen referrerpolicy="no-referrer"></iframe>
<a href="https://www.facebook.com/video/embed?video_id=#{id}" rel="noreferrer">Open embed</a>
EOF
elsif %r{^https?://(?:www\.|m\.)?youtube\.com/(?:.*?[?&#](v=(?<id>[^&#]+)|list=(?<list>[^&#]+)|(?:t|time_continue)=(?<t>[^&#]+)))+} =~ self || %r{^https?://(?:youtu\.be|(?:www\.|m\.)?youtube\.com/(?:embed|v))/(?<id>[^?&#]+)(?:.*?[?&#](?:list=(?<list>[^&#]+)|(?:t|time_continue)=(?<t>[^&#]+)))*} =~ self
elsif %r{^https?://(?:www\.|m\.)?youtube\.com/(?:.*?[?&#](v=(?<id>[^&#]+)|list=(?<list>[^&#]+)|(?:t|time_continue)=(?<t>[^&#]+)))+} =~ self || %r{^https?://(?:youtu\.be|(?:www\.|m\.)?youtube\.com/(?:embed|v|shorts))/(?<id>[^?&#]+)(?:.*?[?&#](?:list=(?<list>[^&#]+)|(?:t|time_continue)=(?<t>[^&#]+)))*} =~ self
# https://www.youtube.com/watch?v=z5OGD5_9cA0&list=PL0QrZvg7QIgpoLdNFnEePRrU-YJfr9Be7&index=3&t=30s
url = "https://www.youtube.com/embed/#{id}?rel=0"
url += "&list=#{list}" if list
Expand Down

0 comments on commit 0b3ba1d

Please sign in to comment.