Class: Onebox::Engine::YoutubeOnebox

Inherits:
Object
  • Object
show all
Includes:
Onebox::Engine, StandardEmbed
Defined in:
lib/onebox/engine/youtube_onebox.rb

Constant Summary collapse

TIMESTR_REGEX =

Regex to parse strings like ā€œ1h3m2sā€. Also accepts bare numbers (which are seconds).

/(\d+h)?(\d+m)?(\d+s?)?/

Constants included from Onebox::Engine

DEFAULT

Instance Attribute Summary

Attributes included from Onebox::Engine

#cache, #timeout, #uri, #url

Instance Method Summary collapse

Methods included from StandardEmbed

add_oembed_provider, add_opengraph_provider, #always_https?, oembed_providers, opengraph_providers, #raw

Methods included from Onebox::Engine

engines, included, #initialize, #options, #options=

Instance Method Details

#embed_paramsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/onebox/engine/youtube_onebox.rb', line 76

def embed_params
  p = {'feature' => 'oembed', 'wmode' => 'opaque'}

  p['list'] = params['list'] if params['list']

  # Parse timestrings, and assign the result as a start= parameter
  start = nil
  if params['start']
    start = params['start']
  elsif params['t']
    start = params['t']
  elsif uri.fragment && uri.fragment.start_with?('t=')
    # referencing uri is safe here because any throws were already caught by video_id returning nil
    # remove the t= from the start
    start = uri.fragment[2..-1]
  end
  p['start'] = parse_timestring(start) if start
  p['end'] = parse_timestring params['end'] if params['end']

  # Official workaround for looping videos
  # https://developers.google.com/youtube/player_parameters#loop
  # use params.include? so that you can just add "&loop"
  if params.include? 'loop'
    p['loop'] = 1
    p['playlist'] = video_id
  end

  URI.encode_www_form(p)
end

#placeholder_htmlObject



36
37
38
39
40
41
42
# File 'lib/onebox/engine/youtube_onebox.rb', line 36

def placeholder_html
  if video_id
    "<img src='https://i1.ytimg.com/vi/#{video_id}/hqdefault.jpg' width='480' height='270'>"
  else
    to_html
  end
end

#to_htmlObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/onebox/engine/youtube_onebox.rb', line 44

def to_html
  if video_id
    # Avoid making HTTP requests if we are able to get the video ID from the
    # URL.
    html = "<iframe width=\"480\" height=\"270\" src=\"https://www.youtube.com/embed/#{video_id}?#{embed_params}\" frameborder=\"0\" allowfullscreen></iframe>"
  elsif params['list']
    # YouTube Playlist URL (https://www.youtube.com/playlist?list=PLBsP89CPrMeOwWHwmD6FzkKIca-GjAD_f)
    # in case of cast_sender.js console errors, see: http://stackoverflow.com/q/25814914
    html = "<iframe width=\"480\" height=\"270\" src=\"https://www.youtube.com/embed/videoseries?list=#{params['list']}&wmode=transparent&rel=0&autohide=1&showinfo=1&enablejsapi=1\" frameborder=\"0\" allowfullscreen></iframe>"
  else
    # for channel pages
    html = Onebox::Engine::WhitelistedGenericOnebox.new(@url, @cache, @timeout).to_html
    return nil unless html
    html = html.gsub /http:/, 'https:'
    html = html.gsub /"\/\//, '"https://'
    html = html.gsub /'\/\//, "'https://"
  end

  html
end

#video_idObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/onebox/engine/youtube_onebox.rb', line 14

def video_id
  if uri.host =~ /youtu.be/
    # A slash, then capture all non-slash characters remaining
    match = uri.path.match(/\/([^\/]+)/)
    return match[1] if match && match[1]
  end

  if uri.path =~ /\/embed\//
    # A slash, then embed, then another slash, then capture all remaining non-slash characters
    match = uri.path.match(/\/embed\/([^\/]+)/)
    return match[1] if match && match[1]
  end

  if params['v']
    return params['v']
  end

  nil
rescue
  return nil
end

#video_titleObject



65
66
67
68
69
70
71
# File 'lib/onebox/engine/youtube_onebox.rb', line 65

def video_title
  yt_oembed_url = "https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=#{video_id.split('?')[0]}"
  yt_oembed_data = Onebox::Helpers.symbolize_keys(::MultiJson.load(Onebox::Helpers.fetch_response(yt_oembed_url).body))
  yt_oembed_data[:title]
rescue
  return nil
end