Class: Onebox::Engine::YoutubeOnebox

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

Constant Summary

Constants included from Onebox::Engine

DEFAULT

Instance Attribute Summary

Attributes included from Onebox::Engine

#errors, #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

all_iframe_origins, engines, included, #initialize, #options, #options=, origins_to_regexes

Instance Method Details

#parse_embed_responseObject



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

def parse_embed_response
  return unless video_id
  return @parse_embed_response if defined?(@parse_embed_response)

  embed_url = "https://www.youtube.com/embed/#{video_id}"
  @embed_doc ||= Onebox::Helpers.fetch_html_doc(embed_url)

  begin
    script_tag = @embed_doc.xpath('//script').find { |tag| tag.to_s.include?('ytcfg.set') }.to_s
    match = script_tag.to_s.match(/ytcfg\.set\((?<json>.*)\)/)

    yt_json = ::JSON.parse(match[:json])
    renderer = ::JSON.parse(yt_json['PLAYER_VARS']['embedded_player_response'])['embedPreview']['thumbnailPreviewRenderer']

    title = renderer['title']['runs'].first['text']

    image = "https://img.youtube.com/vi/#{video_id}/hqdefault.jpg"
  rescue
    return
  end

  @parse_embed_response = { image: image, title: title }
end

#placeholder_htmlObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/onebox/engine/youtube_onebox.rb', line 40

def placeholder_html
  if video_id || list_id
    result = parse_embed_response
    result ||= get_opengraph.data

    "<img src='#{result[:image]}' width='#{WIDTH}' height='#{HEIGHT}' title='#{result[:title]}'>"
  else
    to_html
  end
end

#to_htmlObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/onebox/engine/youtube_onebox.rb', line 51

def to_html
  if video_id
    <<-HTML
      <iframe width="#{WIDTH}"
              height="#{HEIGHT}"
              src="https://www.youtube.com/embed/#{video_id}?#{embed_params}"
              frameborder="0"
              allowfullscreen>
      </iframe>
    HTML
  elsif list_id
    <<-HTML
      <iframe width="#{WIDTH}"
              height="#{HEIGHT}"
              src="https://www.youtube.com/embed/videoseries?list=#{list_id}&wmode=transparent&rel=0&autohide=1&showinfo=1&enablejsapi=1"
              frameborder="0"
              allowfullscreen>
      </iframe>
    HTML
  else
    # for channel pages
    html = Onebox::Engine::AllowlistedGenericOnebox.new(@url, @timeout).to_html
    return if Onebox::Helpers.blank?(html)
    html.gsub!(/['"]\/\//, "https://")
    html
  end
end

#video_titleObject



79
80
81
82
83
84
# File 'lib/onebox/engine/youtube_onebox.rb', line 79

def video_title
  @video_title ||= begin
    result = parse_embed_response || get_opengraph.data
    result[:title]
  end
end