/
allowlisted_generic_onebox.rb
375 lines (327 loc) · 10.8 KB
/
allowlisted_generic_onebox.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43
44
45
46
47
48
49
50
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# frozen_string_literal: true
require 'htmlentities'
module Onebox
module Engine
class AllowlistedGenericOnebox
include Engine
include StandardEmbed
include LayoutSupport
def self.allowed_domains=(list)
@allowed_domains = list
end
def self.allowed_domains
@allowed_domains ||= default_allowed_domains.dup
end
def self.default_allowed_domains
%w(
23hq.com
500px.com
8tracks.com
abc.net.au
about.com
answers.com
arstechnica.com
ask.com
battle.net
bbc.co.uk
bbs.boingboing.net
bestbuy.ca
bestbuy.com
blip.tv
bloomberg.com
businessinsider.com
change.org
clikthrough.com
cnet.com
cnn.com
codepen.io
collegehumor.com
consider.it
coursera.org
cracked.com
dailymail.co.uk
dailymotion.com
deadline.com
dell.com
deviantart.com
digg.com
dotsub.com
ebay.ca
ebay.co.uk
ebay.com
ehow.com
espn.go.com
etsy.com
facebook.com
findery.com
folksy.com
forbes.com
foxnews.com
funnyordie.com
gifs.com
groupon.com
howtogeek.com
huffingtonpost.ca
huffingtonpost.com
hulu.com
ign.com
ikea.com
imdb.com
indiatimes.com
itunes.apple.com
khanacademy.org
kickstarter.com
kinomap.com
lessonplanet.com
linkedin.com
liveleak.com
livestream.com
mashable.com
medium.com
meetup.com
mixcloud.com
mlb.com
myshopify.com
myspace.com
nba.com
npr.org
nytimes.com
photobucket.com
pinterest.com
reference.com
revision3.com
rottentomatoes.com
samsung.com
screenr.com
scribd.com
slideshare.net
sourceforge.net
speakerdeck.com
spotify.com
squidoo.com
streamable.com
techcrunch.com
ted.com
thefreedictionary.com
theglobeandmail.com
thenextweb.com
theonion.com
thestar.com
thesun.co.uk
thinkgeek.com
tmz.com
torontosun.com
tumblr.com
twitpic.com
usatoday.com
viddler.com
videojug.com
vine.co
walmart.com
washingtonpost.com
wi.st
wikia.com
wikihow.com
wired.com
wistia.com
wonderhowto.com
wsj.com
zappos.com
zillow.com
)
end
# Often using the `html` attribute is not what we want, like for some blogs that
# include the entire page HTML. However for some providers like Flickr it allows us
# to return gifv and galleries.
def self.default_html_providers
['Flickr', 'Meetup']
end
def self.html_providers
@html_providers ||= default_html_providers.dup
end
def self.html_providers=(new_provs)
@html_providers = new_provs
end
# A re-written URL converts http:// -> https://
def self.rewrites
@rewrites ||= https_hosts.dup
end
def self.rewrites=(new_list)
@rewrites = new_list
end
def self.https_hosts
%w(slideshare.net dailymotion.com livestream.com imgur.com flickr.com)
end
def self.host_matches(uri, list)
!!list.find { |h| %r((^|\.)#{Regexp.escape(h)}$).match(uri.host) }
end
def self.probable_discourse(uri)
!!(uri.path =~ /\/t\/[^\/]+\/\d+(\/\d+)?(\?.*)?$/)
end
def self.probable_wordpress(uri)
!!(uri.path =~ /\d{4}\/\d{2}\//)
end
def self.allowed_twitter_labels
['brand', 'price', 'usd', 'cad', 'reading time', 'likes']
end
def self.===(other)
other.kind_of?(URI) ?
host_matches(other, allowed_domains) || probable_wordpress(other) || probable_discourse(other) :
super
end
def to_html
rewrite_https(generic_html)
end
def placeholder_html
return article_html if is_article?
return image_html if is_image?
return Onebox::Helpers.video_placeholder_html if is_video? || is_card?
return Onebox::Helpers.generic_placeholder_html if is_embedded?
to_html
end
def data
@data ||= begin
html_entities = HTMLEntities.new
d = { link: link }.merge(raw)
if !Onebox::Helpers.blank?(d[:title])
d[:title] = html_entities.decode(Onebox::Helpers.truncate(d[:title], 80))
end
d[:description] ||= d[:summary]
if !Onebox::Helpers.blank?(d[:description])
d[:description] = html_entities.decode(Onebox::Helpers.truncate(d[:description], 250))
end
if !Onebox::Helpers.blank?(d[:site_name])
d[:domain] = html_entities.decode(Onebox::Helpers.truncate(d[:site_name], 80))
elsif !Onebox::Helpers.blank?(d[:domain])
d[:domain] = "http://#{d[:domain]}" unless d[:domain] =~ /^https?:\/\//
d[:domain] = URI(d[:domain]).host.to_s.sub(/^www\./, '') rescue nil
end
# prefer secure URLs
d[:image] = d[:image_secure_url] || d[:image_url] || d[:thumbnail_url] || d[:image]
d[:image] = Onebox::Helpers::get_absolute_image_url(d[:image], @url)
d[:image] = Onebox::Helpers::normalize_url_for_output(html_entities.decode(d[:image]))
d[:video] = d[:video_secure_url] || d[:video_url] || d[:video]
d[:published_time] = d[:article_published_time] unless Onebox::Helpers.blank?(d[:article_published_time])
if !Onebox::Helpers.blank?(d[:published_time])
d[:article_published_time] = Time.parse(d[:published_time]).strftime("%-d %b %y")
d[:article_published_time_title] = Time.parse(d[:published_time]).strftime("%I:%M%p - %d %B %Y")
end
# Twitter labels
if !Onebox::Helpers.blank?(d[:label1]) && !Onebox::Helpers.blank?(d[:data1]) && !!AllowlistedGenericOnebox.allowed_twitter_labels.find { |l| d[:label1] =~ /#{l}/i }
d[:label_1] = Onebox::Helpers.truncate(d[:label1])
d[:data_1] = Onebox::Helpers.truncate(d[:data1])
end
if !Onebox::Helpers.blank?(d[:label2]) && !Onebox::Helpers.blank?(d[:data2]) && !!AllowlistedGenericOnebox.allowed_twitter_labels.find { |l| d[:label2] =~ /#{l}/i }
unless Onebox::Helpers.blank?(d[:label_1])
d[:label_2] = Onebox::Helpers.truncate(d[:label2])
d[:data_2] = Onebox::Helpers.truncate(d[:data2])
else
d[:label_1] = Onebox::Helpers.truncate(d[:label2])
d[:data_1] = Onebox::Helpers.truncate(d[:data2])
end
end
if Onebox::Helpers.blank?(d[:label_1]) && !Onebox::Helpers.blank?(d[:price_amount]) && !Onebox::Helpers.blank?(d[:price_currency])
d[:label_1] = "Price"
d[:data_1] = Onebox::Helpers.truncate("#{d[:price_currency].strip} #{d[:price_amount].strip}")
end
d
end
end
private
def rewrite_https(html)
return unless html
uri = URI(@url)
if AllowlistedGenericOnebox.host_matches(uri, AllowlistedGenericOnebox.rewrites)
html = html.gsub("http://", "https://")
end
html
end
def generic_html
return article_html if is_article?
return video_html if is_video?
return image_html if is_image?
return embedded_html if is_embedded?
return card_html if is_card?
return article_html if has_text?
end
def is_card?
data[:card] == 'player' && data[:player] =~ URI::regexp
end
def is_article?
(data[:type] =~ /article/ || data[:asset_type] =~ /article/) &&
has_text?
end
def has_text?
!Onebox::Helpers.blank?(data[:title]) &&
!Onebox::Helpers.blank?(data[:description])
end
def is_image?
data[:type] =~ /photo|image/ &&
data[:type] !~ /photostream/ &&
has_image?
end
def has_image?
!Onebox::Helpers.blank?(data[:image])
end
def is_video?
data[:type] =~ /^video[\/\.]/ && !Onebox::Helpers.blank?(data[:video])
end
def is_embedded?
data[:html] &&
data[:height] &&
(
data[:html]["iframe"] ||
AllowlistedGenericOnebox.html_providers.include?(data[:provider_name])
)
end
def card_html
escaped_url = ::Onebox::Helpers.normalize_url_for_output(data[:player])
<<~RAW
<iframe src="#{escaped_url}"
width="#{data[:player_width] || "100%"}"
height="#{data[:player_height]}"
scrolling="no"
frameborder="0">
</iframe>
RAW
end
def article_html
layout.to_html
end
def image_html
return if Onebox::Helpers.blank?(data[:image])
escaped_src = ::Onebox::Helpers.normalize_url_for_output(data[:image])
alt = data[:description] || data[:title]
width = data[:image_width] || data[:thumbnail_width] || data[:width]
height = data[:image_height] || data[:thumbnail_height] || data[:height]
"<img src='#{escaped_src}' alt='#{alt}' width='#{width}' height='#{height}' class='onebox'>"
end
def video_html
escaped_video_src = ::Onebox::Helpers.normalize_url_for_output(data[:video])
escaped_image_src = ::Onebox::Helpers.normalize_url_for_output(data[:image])
<<-HTML
<video title='#{data[:title]}'
width='#{data[:video_width]}'
height='#{data[:video_height]}'
style='max-width:100%'
poster='#{escaped_image_src}'
controls=''>
<source src='#{escaped_video_src}'>
</video>
HTML
end
def embedded_html
fragment = Nokogiri::HTML::fragment(data[:html])
fragment.css("img").each { |img| img["class"] = "thumbnail" }
if iframe = fragment.at_css("iframe")
iframe.remove_attribute("style")
iframe["width"] = data[:width] || "100%"
iframe["height"] = data[:height]
iframe["scrolling"] = "no"
iframe["frameborder"] = "0"
end
fragment.to_html
end
end
end
end