Skip to content
Permalink
Browse files

hardcode youtube

  • Loading branch information
hugogameiro committed Feb 16, 2020
1 parent 67ce3e1 commit a4f38b8ed517aaa1326fecac38c031fa7426ed98
Showing with 16 additions and 1 deletion.
  1. +16 −1 app/services/fetch_link_card_service.rb
@@ -92,7 +92,22 @@ def skip_link?(a)
def attempt_oembed
service = FetchOEmbedService.new
url_domain = Addressable::URI.parse(@url).normalized_host
cached_endpoint = Rails.cache.read("oembed_endpoint:#{url_domain}")
if url_domain == 'youtube.com' || url_domain == 'www.youtube.com' || url_domain == 'youtu.be'
youtube_formats = [
%r(https?://youtu\.be/(.+)),
%r(https?://www\.youtube\.com/watch\?v=(.*?)(&|#|$)),
%r(https?://www\.youtube\.com/embed/(.*?)(\?|$)),
%r(https?://www\.youtube\.com/v/(.*?)(#|\?|$)),
%r(https?://www\.youtube\.com/user/.*?#\w/\w/\w/\w/(.+)\b)
]
youtube_formats.find { |format| @url =~ format } and $1
video_id = $1
if video_id && (video_id.length == 11)
cached_endpoint = {:endpoint=>"https://www.youtube.com/oembed?url=https%3A//youtube.com/watch%3Fv%3D#{$1}&format=json", :format=>:json}
end
else
cached_endpoint = Rails.cache.read("oembed_endpoint:#{url_domain}")
end

embed = service.call(@url, cached_endpoint: cached_endpoint) unless cached_endpoint.nil?
embed ||= service.call(@url, html: html) unless html.nil?

0 comments on commit a4f38b8

Please sign in to comment.
You can’t perform that action at this time.