Class: Onebox::Engine::YoutubeOnebox

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

Instance Attribute Summary

Attributes included from Onebox::Engine

#errors, #options, #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, 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
39
40
41
42
# 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 StandardError
    return
  end

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

#placeholder_htmlObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/onebox/engine/youtube_onebox.rb', line 44

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='#{CGI.escapeHTML(result[:title])}'>"
  else
    to_html
  end
end

#to_htmlObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/onebox/engine/youtube_onebox.rb', line 55

def to_html
  if video_id
    "<iframe\nsrc=\"https://www.youtube.com/embed/\#{video_id}?\#{embed_params}\"\nwidth=\"\#{WIDTH}\"\nheight=\"\#{HEIGHT}\"\nframeborder=\"0\"\nallowfullscreen\nclass=\"youtube-onebox\"\n></iframe>\n"
  elsif list_id
    "<iframe\nsrc=\"https://www.youtube.com/embed/videoseries?list=\#{list_id}&wmode=transparent&rel=0&autohide=1&showinfo=1&enablejsapi=1\"\nwidth=\"\#{WIDTH}\"\nheight=\"\#{HEIGHT}\"\nframeborder=\"0\"\nallowfullscreen\nclass=\"youtube-onebox\"\n></iframe>\n"
  else
    # for channel pages
    html = Onebox::Engine::AllowlistedGenericOnebox.new(@url, @timeout).to_html
    return if Onebox::Helpers.blank?(html)
    html.gsub!(%r{['"]//}, "https://")
    html
  end
end

#video_titleObject



87
88
89
90
91
92
93
# File 'lib/onebox/engine/youtube_onebox.rb', line 87

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