Class: Onebox::Engine::YoutubeOnebox
Constant Summary
DEFAULT
Instance Attribute Summary
#errors, #options, #timeout, #uri, #url
Instance Method Summary
collapse
add_oembed_provider, add_opengraph_provider, #always_https?, oembed_providers, opengraph_providers, #raw
all_iframe_origins, engines, included, #initialize, origins_to_regexes
Instance Method Details
#parse_embed_response ⇒ Object
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_html ⇒ Object
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_html ⇒ Object
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
78
79
|
# File 'lib/onebox/engine/youtube_onebox.rb', line 51
def to_html
if video_id
<<-HTML
<iframe
src="https://www.youtube.com/embed/#{video_id}?#{embed_params}"
width="#{WIDTH}"
height="#{HEIGHT}"
frameborder="0"
allowfullscreen
></iframe>
HTML
elsif list_id
<<-HTML
<iframe
src="https://www.youtube.com/embed/videoseries?list=#{list_id}&wmode=transparent&rel=0&autohide=1&showinfo=1&enablejsapi=1"
width="#{WIDTH}"
height="#{HEIGHT}"
frameborder="0"
allowfullscreen
></iframe>
HTML
else
html = Onebox::Engine::AllowlistedGenericOnebox.new(@url, @timeout).to_html
return if Onebox::Helpers.blank?(html)
html.gsub!(/['"]\/\//, "https://")
html
end
end
|
#video_title ⇒ Object
81
82
83
84
85
86
|
# File 'lib/onebox/engine/youtube_onebox.rb', line 81
def video_title
@video_title ||= begin
result = parse_embed_response || get_opengraph.data
result[:title]
end
end
|