metainspector 5.22.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +26 -21
- data/lib/meta_inspector/document.rb +76 -66
- data/lib/meta_inspector/errors.rb +5 -1
- data/lib/meta_inspector/html_encoding.rb +7 -1
- data/lib/meta_inspector/http_session.rb +51 -44
- data/lib/meta_inspector/image_downloader.rb +37 -26
- data/lib/meta_inspector/parser.rb +49 -50
- data/lib/meta_inspector/parsers/base.rb +7 -2
- data/lib/meta_inspector/parsers/head_links.rb +12 -27
- data/lib/meta_inspector/parsers/iframes.rb +2 -3
- data/lib/meta_inspector/parsers/images.rb +119 -70
- data/lib/meta_inspector/parsers/links.rb +10 -6
- data/lib/meta_inspector/parsers/meta_tags.rb +16 -29
- data/lib/meta_inspector/parsers/texts.rb +43 -33
- data/lib/meta_inspector/request.rb +42 -45
- data/lib/meta_inspector/url.rb +13 -16
- data/lib/meta_inspector/url_policy.rb +17 -10
- data/lib/meta_inspector/version.rb +1 -1
- data/lib/meta_inspector.rb +14 -13
- data/lib/metainspector.rb +1 -1
- data/spec/document_spec.rb +48 -68
- data/spec/meta_inspector/images_spec.rb +82 -1
- data/spec/meta_inspector/meta_inspector_spec.rb +3 -25
- data/spec/meta_inspector/texts_spec.rb +67 -12
- data/spec/parser_spec.rb +0 -10
- data/spec/request_errors_spec.rb +1 -1
- metadata +1 -1
|
@@ -14,60 +14,38 @@ module MetaInspector
|
|
|
14
14
|
MAX_NODE_BYTES = 10_000_000
|
|
15
15
|
|
|
16
16
|
def initialize(document, options = {})
|
|
17
|
-
@document
|
|
18
|
-
@max_html_bytes
|
|
19
|
-
@head_links_parser =
|
|
20
|
-
@meta_tag_parser
|
|
21
|
-
@links_parser
|
|
22
|
-
@
|
|
23
|
-
@
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
inline_image_data: options[:inline_image_data],
|
|
28
|
-
image_downloader: options[:image_downloader])
|
|
29
|
-
@iframes_parser = MetaInspector::Parsers::IframesParser.new(self)
|
|
30
|
-
@texts_parser = MetaInspector::Parsers::TextsParser.new(self)
|
|
31
|
-
|
|
32
|
-
parsed # parse early so we can fail early
|
|
17
|
+
@document = document
|
|
18
|
+
@max_html_bytes = options[:max_html_bytes]
|
|
19
|
+
@head_links_parser = Parsers::HeadLinksParser.new(self)
|
|
20
|
+
@meta_tag_parser = Parsers::MetaTagsParser.new(self)
|
|
21
|
+
@links_parser = Parsers::LinksParser.new(self)
|
|
22
|
+
@images_parser = Parsers::ImagesParser.new(self, options)
|
|
23
|
+
@iframes_parser = Parsers::IframesParser.new(self)
|
|
24
|
+
@texts_parser = Parsers::TextsParser.new(self)
|
|
25
|
+
|
|
26
|
+
parsed # parse early so we can fail early
|
|
33
27
|
end
|
|
34
28
|
|
|
35
29
|
extend Forwardable
|
|
36
|
-
delegate [
|
|
37
|
-
delegate [
|
|
38
|
-
delegate [
|
|
39
|
-
delegate [
|
|
40
|
-
delegate :images
|
|
41
|
-
delegate [
|
|
42
|
-
delegate [
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
# Deprecated compatibility shortcut; remove in 6.0.
|
|
46
|
-
def favicon
|
|
47
|
-
warn 'DEPRECATION WARNING: page.favicon is deprecated and will be removed in 6.0. ' \
|
|
48
|
-
'Use page.images.favicon instead.'
|
|
49
|
-
images.favicon
|
|
50
|
-
end
|
|
30
|
+
delegate %i[url scheme host] => :@document
|
|
31
|
+
delegate %i[meta_tags meta_tag meta charset] => :@meta_tag_parser
|
|
32
|
+
delegate %i[head_links stylesheets canonicals feeds] => :@head_links_parser
|
|
33
|
+
delegate %i[links base_url] => :@links_parser
|
|
34
|
+
delegate :images => :@images_parser
|
|
35
|
+
delegate %i[iframes iframe_sources] => :@iframes_parser
|
|
36
|
+
delegate %i[language title best_title author best_author description best_description
|
|
37
|
+
h1 h2 h3 h4 h5 h6] => :@texts_parser
|
|
51
38
|
|
|
52
39
|
# Returns the whole parsed document
|
|
40
|
+
#
|
|
41
|
+
# libxml2 (HTML4) parses first: it detects the encoding and enforces its
|
|
42
|
+
# resource limits. The DOM is then built with HTML5 rules, unless the HTML
|
|
43
|
+
# can't be converted to UTF-8, in which case libxml2's tree is kept.
|
|
53
44
|
def parsed
|
|
54
45
|
@parsed ||= begin
|
|
55
46
|
html = @document.to_s
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
end
|
|
59
|
-
html = HtmlEncoding.normalize(html).scrub
|
|
60
|
-
# Keep libxml2's resource-limit validation. HTML5 has different limits
|
|
61
|
-
# and would otherwise accept oversized text nodes.
|
|
62
|
-
document = Nokogiri::HTML(html, nil, HtmlEncoding.detect(html))
|
|
63
|
-
# For untagged bytes, use the selected declaration, then parse
|
|
64
|
-
# again after the same conversion used for tagged strings. libxml2's
|
|
65
|
-
# Windows-31J decoder still uses some strict Shift_JIS character mappings.
|
|
66
|
-
if html.encoding == Encoding::BINARY && HtmlEncoding.superset(document.encoding)
|
|
67
|
-
html = HtmlEncoding.normalize(html.dup.force_encoding(document.encoding))
|
|
68
|
-
document = Nokogiri::HTML(html)
|
|
69
|
-
end
|
|
70
|
-
raise_limit_error(document, /Resource limit exceeded|xmlSAX2Characters: huge text node/)
|
|
47
|
+
check_html_size!(html)
|
|
48
|
+
html, document = html4_document(html)
|
|
71
49
|
|
|
72
50
|
if (utf8 = html5_input(html, document))
|
|
73
51
|
# The HTML5 tree is the DOM and enforces its own depth limit, so
|
|
@@ -88,6 +66,30 @@ module MetaInspector
|
|
|
88
66
|
|
|
89
67
|
private
|
|
90
68
|
|
|
69
|
+
def check_html_size!(html)
|
|
70
|
+
return unless @max_html_bytes && html.bytesize > @max_html_bytes
|
|
71
|
+
|
|
72
|
+
raise MetaInspector::ParserError, "HTML is #{html.bytesize} bytes, exceeding the limit of #{@max_html_bytes} bytes"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Returns the HTML, converted if its encoding was detected from its bytes,
|
|
76
|
+
# and libxml2's tree for it.
|
|
77
|
+
def html4_document(html)
|
|
78
|
+
html = HtmlEncoding.normalize(html).scrub
|
|
79
|
+
# Keep libxml2's resource-limit validation. HTML5 has different limits
|
|
80
|
+
# and would otherwise accept oversized text nodes.
|
|
81
|
+
document = Nokogiri::HTML(html, nil, HtmlEncoding.detect(html))
|
|
82
|
+
# For untagged bytes, use the selected declaration, then parse
|
|
83
|
+
# again after the same conversion used for tagged strings. libxml2's
|
|
84
|
+
# Windows-31J decoder still uses some strict Shift_JIS character mappings.
|
|
85
|
+
if html.encoding == Encoding::BINARY && HtmlEncoding.superset(document.encoding)
|
|
86
|
+
html = HtmlEncoding.normalize(html.dup.force_encoding(document.encoding))
|
|
87
|
+
document = Nokogiri::HTML(html)
|
|
88
|
+
end
|
|
89
|
+
raise_limit_error(document, /Resource limit exceeded|xmlSAX2Characters: huge text node/)
|
|
90
|
+
[html, document]
|
|
91
|
+
end
|
|
92
|
+
|
|
91
93
|
def raise_limit_error(document, pattern)
|
|
92
94
|
return if non_html_content?
|
|
93
95
|
|
|
@@ -136,10 +138,7 @@ module MetaInspector
|
|
|
136
138
|
# The caller opted in with allow_non_html_content and the response is not
|
|
137
139
|
# HTML, so resource limits that guard against truncated HTML do not apply.
|
|
138
140
|
def non_html_content?
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
content_type = @document.content_type
|
|
142
|
-
!content_type.nil? && content_type != 'text/html'
|
|
141
|
+
@document.allow_non_html_content && !Request.html_content_type?(@document.content_type)
|
|
143
142
|
end
|
|
144
143
|
|
|
145
144
|
def restore_noscript_content(document)
|
|
@@ -13,18 +13,23 @@ module MetaInspector
|
|
|
13
13
|
# which, in turn, delegates it to the LinksParser.
|
|
14
14
|
#
|
|
15
15
|
class Base
|
|
16
|
+
extend Forwardable
|
|
17
|
+
|
|
16
18
|
def initialize(main_parser)
|
|
17
19
|
@main_parser = main_parser
|
|
18
20
|
end
|
|
19
21
|
|
|
20
|
-
extend Forwardable
|
|
21
|
-
|
|
22
22
|
private
|
|
23
23
|
|
|
24
24
|
# Cleans up nokogiri search results
|
|
25
25
|
def cleanup(results)
|
|
26
26
|
results.map { |r| r.value.strip }.reject(&:empty?).uniq
|
|
27
27
|
end
|
|
28
|
+
|
|
29
|
+
# Splits a rel attribute into its tokens, lowercased as HTML does: ASCII only.
|
|
30
|
+
def rel_tokens(rel)
|
|
31
|
+
rel.to_s.downcase(:ascii).split(/[ \t\n\f\r]+/)
|
|
32
|
+
end
|
|
28
33
|
end
|
|
29
34
|
end
|
|
30
35
|
end
|
|
@@ -10,14 +10,11 @@ module MetaInspector
|
|
|
10
10
|
|
|
11
11
|
def head_links
|
|
12
12
|
@head_links ||= parsed.css('head link').map do |tag|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
[keysym, val]
|
|
19
|
-
end
|
|
20
|
-
]
|
|
13
|
+
tag.attributes.to_h do |name, attribute|
|
|
14
|
+
value = attribute.value
|
|
15
|
+
value = URL.absolutify(value, base_url) if name == 'href'
|
|
16
|
+
[name.to_sym, value]
|
|
17
|
+
end
|
|
21
18
|
end
|
|
22
19
|
end
|
|
23
20
|
|
|
@@ -30,25 +27,13 @@ module MetaInspector
|
|
|
30
27
|
end
|
|
31
28
|
|
|
32
29
|
def feeds
|
|
33
|
-
@feeds ||=
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
@feeds ||= parsed.search('//link[@rel]').filter_map do |link|
|
|
31
|
+
next unless rel_includes?(link['rel'], 'alternate')
|
|
32
|
+
next unless KNOWN_FEED_TYPES.include?(media_type(link['type']))
|
|
33
|
+
next if link['href'].to_s.strip.empty?
|
|
37
34
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
href: URL.absolutify(link["href"], base_url),
|
|
41
|
-
type: link["type"]
|
|
42
|
-
}
|
|
43
|
-
end.compact
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
# Deprecated compatibility method; remove in 6.0.
|
|
47
|
-
def feed
|
|
48
|
-
warn 'DEPRECATION WARNING: page.feed is deprecated and will be removed in 6.0. ' \
|
|
49
|
-
'Use page.feeds instead.'
|
|
50
|
-
first_feed = feeds.find { |link| %w[application/rss+xml application/atom+xml].include?(media_type(link[:type])) }
|
|
51
|
-
first_feed && first_feed[:href]
|
|
35
|
+
{ title: link['title'], href: URL.absolutify(link['href'], base_url), type: link['type'] }
|
|
36
|
+
end
|
|
52
37
|
end
|
|
53
38
|
|
|
54
39
|
private
|
|
@@ -60,7 +45,7 @@ module MetaInspector
|
|
|
60
45
|
end
|
|
61
46
|
|
|
62
47
|
def rel_includes?(rel, keyword)
|
|
63
|
-
rel
|
|
48
|
+
rel_tokens(rel).include?(keyword)
|
|
64
49
|
end
|
|
65
50
|
end
|
|
66
51
|
end
|
|
@@ -2,11 +2,10 @@ module MetaInspector
|
|
|
2
2
|
module Parsers
|
|
3
3
|
class IframesParser < Base
|
|
4
4
|
delegate [:parsed, :base_url] => :@main_parser
|
|
5
|
+
|
|
5
6
|
def iframes
|
|
6
7
|
@iframes ||= parsed.search('//iframe').map do |iframe|
|
|
7
|
-
iframe.attributes.
|
|
8
|
-
result[name.to_sym] = attribute.value
|
|
9
|
-
end
|
|
8
|
+
iframe.attributes.to_h { |name, attribute| [name.to_sym, attribute.value] }
|
|
10
9
|
end
|
|
11
10
|
end
|
|
12
11
|
|
|
@@ -9,6 +9,22 @@ module MetaInspector
|
|
|
9
9
|
|
|
10
10
|
include Enumerable
|
|
11
11
|
|
|
12
|
+
# Meta tags that name the page's image, in order of preference.
|
|
13
|
+
SUGGESTED_IMAGE_META = %w[og:image twitter:image parsely-image-url].freeze
|
|
14
|
+
|
|
15
|
+
# A URL that an <img> or <picture> may show. group identifies that
|
|
16
|
+
# element, and descriptor is [:w, width] or [:x, density].
|
|
17
|
+
Candidate = Struct.new(:url, :width, :height, :group, :descriptor) do
|
|
18
|
+
def width_descriptor?
|
|
19
|
+
descriptor[0] == :w
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def descriptor_value
|
|
23
|
+
descriptor[1]
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
private_constant :SUGGESTED_IMAGE_META, :Candidate
|
|
27
|
+
|
|
12
28
|
def initialize(main_parser, options = {})
|
|
13
29
|
@image_downloader = options[:image_downloader]
|
|
14
30
|
@download_images = options[:download_images]
|
|
@@ -47,20 +63,18 @@ module MetaInspector
|
|
|
47
63
|
end
|
|
48
64
|
end
|
|
49
65
|
|
|
50
|
-
# Returns an array of [img_url, width, height] sorted by image area (width * height)
|
|
66
|
+
# Returns an array of [img_url, width, height] sorted by image area (width * height),
|
|
67
|
+
# ranking one candidate per <img> or <picture> (see ranked_candidates).
|
|
51
68
|
def with_size
|
|
52
69
|
@with_size ||= begin
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
width, height = image_size(url) || [0, 0]
|
|
60
|
-
end
|
|
61
|
-
[result_url(url), width.to_i, height.to_i]
|
|
70
|
+
candidates = unique_image_candidates(ranked_candidates)
|
|
71
|
+
candidates.reject! { |candidate| image_validity(candidate.url) == false } if rejecting_invalid_images?
|
|
72
|
+
images = candidates.map do |candidate|
|
|
73
|
+
width, height = candidate.width, candidate.height
|
|
74
|
+
width, height = image_size(candidate.url) || [0, 0] if width.nil? || height.nil?
|
|
75
|
+
[result_url(candidate.url), width.to_i, height.to_i]
|
|
62
76
|
end
|
|
63
|
-
|
|
77
|
+
images.sort_by { |_url, width, height| -(width * height) }
|
|
64
78
|
end
|
|
65
79
|
end
|
|
66
80
|
|
|
@@ -68,8 +82,9 @@ module MetaInspector
|
|
|
68
82
|
# With validation enabled, details reuse the validation lookups.
|
|
69
83
|
def with_details
|
|
70
84
|
@with_details ||= begin
|
|
71
|
-
|
|
72
|
-
images = unique_image_candidates.filter_map do |
|
|
85
|
+
downloads = 0
|
|
86
|
+
images = unique_image_candidates(image_candidates).filter_map do |candidate|
|
|
87
|
+
url = candidate.url
|
|
73
88
|
metadata = nil
|
|
74
89
|
if validating_images?
|
|
75
90
|
next if @reject_invalid_images && image_validity(url) == false
|
|
@@ -78,16 +93,17 @@ module MetaInspector
|
|
|
78
93
|
elsif @download_images
|
|
79
94
|
if data_image_url?(url)
|
|
80
95
|
metadata = image_metadata(url)
|
|
81
|
-
elsif http_image_url?(url) && (
|
|
82
|
-
|
|
96
|
+
elsif http_image_url?(url) && within_download_limit?(downloads)
|
|
97
|
+
downloads += 1
|
|
83
98
|
metadata = @image_downloader.metadata(encoded_url(url))
|
|
84
99
|
end
|
|
85
100
|
end
|
|
86
101
|
downloaded_width, downloaded_height = metadata && metadata[:dimensions]
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
type: metadata && metadata[:type],
|
|
102
|
+
{ url: result_url(url),
|
|
103
|
+
width: (candidate.width || downloaded_width).to_i,
|
|
104
|
+
height: (candidate.height || downloaded_height).to_i,
|
|
105
|
+
type: metadata && metadata[:type],
|
|
106
|
+
file_size: metadata && metadata[:file_size] }
|
|
91
107
|
end
|
|
92
108
|
images.sort_by { |image| -(image[:width] * image[:height]) }
|
|
93
109
|
end
|
|
@@ -96,26 +112,20 @@ module MetaInspector
|
|
|
96
112
|
# Returns the largest image from the image collection,
|
|
97
113
|
# filtered for images that are more square than 10:1 or 1:10
|
|
98
114
|
def largest
|
|
99
|
-
@largest_image ||=
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
(@inline_image_data || !data_image_url?(url)) && ratio > 0.1 && ratio < 10
|
|
104
|
-
end
|
|
105
|
-
url, width, height = imgs_with_size.first
|
|
106
|
-
url
|
|
107
|
-
end
|
|
115
|
+
@largest_image ||= with_size.find do |url, width, height|
|
|
116
|
+
ratio = width.to_f / height.to_f
|
|
117
|
+
(@inline_image_data || !data_image_url?(url)) && ratio > 0.1 && ratio < 10
|
|
118
|
+
end&.first
|
|
108
119
|
end
|
|
109
120
|
|
|
110
|
-
# Return favicon url if exist
|
|
121
|
+
# Return favicon url if exist. An href that can't be resolved returns nil.
|
|
111
122
|
def favicon
|
|
112
123
|
link = parsed.xpath('//link[@rel]').find do |node|
|
|
113
124
|
# Keep legacy shortcut-only links while matching whole, ASCII-insensitive tokens.
|
|
114
|
-
node['rel']
|
|
125
|
+
rel_tokens(node['rel']).any? { |rel| rel == 'icon' || rel == 'shortcut' }
|
|
115
126
|
end
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
rescue
|
|
127
|
+
result_url(URL.absolutify(link.attributes['href'].value, base_url, normalize: false))
|
|
128
|
+
rescue StandardError
|
|
119
129
|
nil
|
|
120
130
|
end
|
|
121
131
|
|
|
@@ -128,40 +138,68 @@ module MetaInspector
|
|
|
128
138
|
end
|
|
129
139
|
|
|
130
140
|
def suggested_image(skip_inline: false)
|
|
131
|
-
|
|
132
|
-
value =
|
|
133
|
-
next
|
|
141
|
+
SUGGESTED_IMAGE_META.each do |key|
|
|
142
|
+
value = meta[key]
|
|
143
|
+
next if value.nil? || value.strip.empty?
|
|
134
144
|
|
|
135
145
|
url = URL.absolutify(value, base_url, normalize: false)
|
|
136
146
|
next if url.nil? || (skip_inline && data_image_url?(url))
|
|
137
|
-
next if
|
|
147
|
+
next if rejecting_invalid_images? && image_validity(url) == false
|
|
138
148
|
|
|
139
149
|
return url
|
|
140
150
|
end
|
|
141
151
|
nil
|
|
142
152
|
end
|
|
143
153
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
154
|
+
# Keeps one candidate per URL: the first one, unless a later one has both
|
|
155
|
+
# dimensions in the HTML and the first doesn't.
|
|
156
|
+
def unique_image_candidates(candidates)
|
|
157
|
+
unique = {}
|
|
158
|
+
candidates.each do |candidate|
|
|
159
|
+
url = candidate.url
|
|
147
160
|
next if url.nil? || url.empty?
|
|
148
161
|
|
|
149
|
-
previous =
|
|
150
|
-
if previous.nil? || ((previous
|
|
151
|
-
|
|
162
|
+
previous = unique[url]
|
|
163
|
+
if previous.nil? || ((previous.width.nil? || previous.height.nil?) && candidate.width && candidate.height)
|
|
164
|
+
unique[url] = candidate
|
|
152
165
|
end
|
|
153
166
|
end
|
|
154
|
-
|
|
167
|
+
unique.values
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# The candidates of one <img> or <picture> are the same picture at different
|
|
171
|
+
# sizes, so only the largest one is downloaded for ranking: the largest width
|
|
172
|
+
# descriptor if any candidate has one, otherwise the highest density (src
|
|
173
|
+
# counts as 1x). Candidates that can't be measured are skipped while others
|
|
174
|
+
# remain. A src with both dimensions in the HTML is kept too, as it costs no
|
|
175
|
+
# download.
|
|
176
|
+
def ranked_candidates
|
|
177
|
+
image_candidates.group_by(&:group).flat_map do |_group, candidates|
|
|
178
|
+
candidates = candidates.reject { |candidate| candidate.url.nil? || candidate.url.empty? }
|
|
179
|
+
sized = candidates.select { |candidate| candidate.width && candidate.height }
|
|
180
|
+
pool = candidates.select { |candidate| measurable_image_url?(candidate.url) }
|
|
181
|
+
pool = candidates if pool.empty?
|
|
182
|
+
with_width = pool.select(&:width_descriptor?)
|
|
183
|
+
pool = with_width unless with_width.empty?
|
|
184
|
+
largest = pool.each_with_index.max_by { |candidate, index| [candidate.descriptor_value, -index] }&.first
|
|
185
|
+
largest ? [largest] + (sized - [largest]) : sized
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def measurable_image_url?(url)
|
|
190
|
+
data_image_url?(url) ? @inline_image_data : http_image_url?(url)
|
|
155
191
|
end
|
|
156
192
|
|
|
157
193
|
def images_collection
|
|
158
|
-
@images_collection ||=
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
194
|
+
@images_collection ||= begin
|
|
195
|
+
urls = absolutified_images
|
|
196
|
+
urls = urls.reject { |url| url && image_validity(url) == false } if rejecting_invalid_images?
|
|
197
|
+
urls.map { |url| result_url(url) }
|
|
162
198
|
end
|
|
163
199
|
end
|
|
164
200
|
|
|
201
|
+
# Returns [width, height], or nil if unknown. Each URL is looked up once,
|
|
202
|
+
# and only max_image_downloads HTTP lookups are made.
|
|
165
203
|
def image_size(url)
|
|
166
204
|
return @image_sizes[url] if @image_sizes.key?(url)
|
|
167
205
|
return nil unless @download_images
|
|
@@ -172,15 +210,12 @@ module MetaInspector
|
|
|
172
210
|
return @image_sizes[url] = metadata && metadata[:dimensions]
|
|
173
211
|
end
|
|
174
212
|
return @image_sizes[url] = nil unless http_image_url?(url)
|
|
175
|
-
return nil unless
|
|
176
|
-
|
|
177
|
-
unless validating_images?
|
|
178
|
-
@download_attempts += 1
|
|
179
|
-
return @image_sizes[url] = @image_downloader.size(encoded_url(url))
|
|
180
|
-
end
|
|
213
|
+
return nil unless within_download_limit?(@download_attempts)
|
|
181
214
|
|
|
182
|
-
request_url = encoded_url(url)
|
|
183
215
|
@download_attempts += 1
|
|
216
|
+
request_url = encoded_url(url)
|
|
217
|
+
return @image_sizes[url] = @image_downloader.size(request_url) unless validating_images?
|
|
218
|
+
|
|
184
219
|
# Metadata comes from the same download and lets with_details reuse it.
|
|
185
220
|
metadata = @image_downloader.metadata(request_url, reject_invalid_url: true)
|
|
186
221
|
@image_metadata[url] = metadata
|
|
@@ -192,10 +227,18 @@ module MetaInspector
|
|
|
192
227
|
@image_metadata[url]
|
|
193
228
|
end
|
|
194
229
|
|
|
230
|
+
def within_download_limit?(downloads)
|
|
231
|
+
@max_image_downloads.nil? || downloads < @max_image_downloads
|
|
232
|
+
end
|
|
233
|
+
|
|
195
234
|
def validating_images?
|
|
196
235
|
@reject_invalid_images || @fetch_all_image_meta
|
|
197
236
|
end
|
|
198
237
|
|
|
238
|
+
def rejecting_invalid_images?
|
|
239
|
+
@download_images && @reject_invalid_images
|
|
240
|
+
end
|
|
241
|
+
|
|
199
242
|
# Normalizes non-ASCII URLs for the request; results keep the original.
|
|
200
243
|
# A URL that cannot be normalized is returned as is, so URL validation
|
|
201
244
|
# rejects it instead of Addressable raising.
|
|
@@ -224,17 +267,20 @@ module MetaInspector
|
|
|
224
267
|
end
|
|
225
268
|
|
|
226
269
|
def absolutified_images
|
|
227
|
-
image_candidates.map(&:
|
|
270
|
+
image_candidates.map(&:url).uniq
|
|
228
271
|
end
|
|
229
272
|
|
|
273
|
+
# The src of each <img>, as 1x, and the srcset candidates of each <img>
|
|
274
|
+
# and <picture> source, in document order.
|
|
230
275
|
def image_candidates
|
|
231
276
|
@image_candidates ||= parsed.xpath('//img | //picture/source').flat_map do |node|
|
|
232
|
-
|
|
277
|
+
sources = []
|
|
233
278
|
src = node['src'].to_s.strip if node.name == 'img'
|
|
234
|
-
|
|
235
|
-
srcset_urls(node['srcset']).each { |url|
|
|
236
|
-
|
|
237
|
-
|
|
279
|
+
sources << [src, node['width'], node['height'], [:x, 1.0]] if src && !src.empty?
|
|
280
|
+
srcset_urls(node['srcset']).each { |url, descriptor| sources << [url, nil, nil, descriptor] }
|
|
281
|
+
group = node.parent&.name == 'picture' ? node.parent.pointer_id : node.pointer_id
|
|
282
|
+
sources.map do |url, width, height, descriptor|
|
|
283
|
+
Candidate.new(URL.absolutify(url, base_url, normalize: false), width, height, group, descriptor)
|
|
238
284
|
end
|
|
239
285
|
end
|
|
240
286
|
end
|
|
@@ -255,19 +301,22 @@ module MetaInspector
|
|
|
255
301
|
descriptor = scanner.scan(/(?:[^,(]|\([^)]*(?:\)|\z))*/).strip
|
|
256
302
|
scanner.skip(/,/)
|
|
257
303
|
end
|
|
258
|
-
|
|
304
|
+
parsed_descriptor = parse_srcset_descriptor(descriptor)
|
|
305
|
+
urls << [url, parsed_descriptor] if !url.empty? && parsed_descriptor
|
|
259
306
|
end
|
|
260
307
|
urls
|
|
261
308
|
end
|
|
262
309
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
310
|
+
# Returns [:w, width] or [:x, density], or nil for an invalid descriptor.
|
|
311
|
+
# A candidate without a descriptor is 1x.
|
|
312
|
+
def parse_srcset_descriptor(descriptor)
|
|
313
|
+
if descriptor.empty?
|
|
314
|
+
[:x, 1.0]
|
|
315
|
+
elsif descriptor.match?(/\A\d+w\z/) && descriptor.to_i > 0
|
|
316
|
+
[:w, descriptor.to_i]
|
|
317
|
+
elsif descriptor.match?(/\A(?:\d+(?:\.\d+)?|\.\d+)(?:[eE][+-]?\d+)?x\z/) && descriptor.to_f >= 0
|
|
318
|
+
[:x, descriptor.to_f]
|
|
319
|
+
end
|
|
271
320
|
end
|
|
272
321
|
end
|
|
273
322
|
end
|
|
@@ -3,6 +3,9 @@ module MetaInspector
|
|
|
3
3
|
class LinksParser < Base
|
|
4
4
|
delegate [:parsed, :url, :scheme, :host] => :@main_parser
|
|
5
5
|
|
|
6
|
+
HTTP_LINK = /^http(s)?:\/\//i
|
|
7
|
+
private_constant :HTTP_LINK
|
|
8
|
+
|
|
6
9
|
def links
|
|
7
10
|
self
|
|
8
11
|
end
|
|
@@ -14,19 +17,19 @@ module MetaInspector
|
|
|
14
17
|
@raw ||= cleanup(parsed.xpath('//*[local-name()="a"]/@href')).compact.uniq
|
|
15
18
|
end
|
|
16
19
|
|
|
17
|
-
# Returns all links found,
|
|
20
|
+
# Returns all links found, absolutified
|
|
18
21
|
def all
|
|
19
22
|
@all ||= raw.map { |link| URL.absolutify(link, base_url) }.compact.uniq
|
|
20
23
|
end
|
|
21
24
|
|
|
22
25
|
# Returns all HTTP links found
|
|
23
26
|
def http
|
|
24
|
-
@http ||= all.
|
|
27
|
+
@http ||= all.grep(HTTP_LINK)
|
|
25
28
|
end
|
|
26
29
|
|
|
27
30
|
# Returns all non-HTTP links found
|
|
28
31
|
def non_http
|
|
29
|
-
@non_http ||= all.
|
|
32
|
+
@non_http ||= all.grep_v(HTTP_LINK)
|
|
30
33
|
end
|
|
31
34
|
|
|
32
35
|
# Returns all internal HTTP links found
|
|
@@ -50,8 +53,9 @@ module MetaInspector
|
|
|
50
53
|
# or the url of the document if no <base> tag was found.
|
|
51
54
|
# Relative base hrefs are resolved against the document URL.
|
|
52
55
|
def base_url
|
|
53
|
-
|
|
54
|
-
|
|
56
|
+
return url if base_href.to_s.strip.empty?
|
|
57
|
+
|
|
58
|
+
URL.absolutify(base_href, url) || url
|
|
55
59
|
end
|
|
56
60
|
|
|
57
61
|
# Returns the value of the href attribute on the <base /> tag, if exists.
|
|
@@ -60,7 +64,7 @@ module MetaInspector
|
|
|
60
64
|
def base_href
|
|
61
65
|
return @base_href if defined?(@base_href)
|
|
62
66
|
|
|
63
|
-
@base_href = parsed.search('base').first
|
|
67
|
+
@base_href = parsed.search('base').first&.attributes&.[]('href')&.value
|
|
64
68
|
end
|
|
65
69
|
end
|
|
66
70
|
end
|
|
@@ -5,17 +5,21 @@ module MetaInspector
|
|
|
5
5
|
|
|
6
6
|
def meta_tags
|
|
7
7
|
{
|
|
8
|
-
'name'
|
|
9
|
-
'http-equiv'
|
|
10
|
-
'property'
|
|
11
|
-
'charset'
|
|
8
|
+
'name' => meta_tags_by('name'),
|
|
9
|
+
'http-equiv' => meta_tags_by('http-equiv'),
|
|
10
|
+
'property' => meta_tags_by('property'),
|
|
11
|
+
'charset' => [charset_from_meta_charset]
|
|
12
12
|
}
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
+
# Like meta_tags, with only the first value of each tag
|
|
15
16
|
def meta_tag
|
|
16
|
-
|
|
17
|
+
meta_tags.transform_values do |values|
|
|
18
|
+
values.is_a?(Hash) ? values.transform_values(&:first) : values.first
|
|
19
|
+
end
|
|
17
20
|
end
|
|
18
21
|
|
|
22
|
+
# meta_tag merged into one Hash: name, then http-equiv, then property
|
|
19
23
|
def meta
|
|
20
24
|
meta_tag['name']
|
|
21
25
|
.merge(meta_tag['http-equiv'])
|
|
@@ -33,7 +37,7 @@ module MetaInspector
|
|
|
33
37
|
private
|
|
34
38
|
|
|
35
39
|
def charset_from_meta_charset
|
|
36
|
-
parsed.
|
|
40
|
+
parsed.at_css('meta[charset]')&.attributes&.[]('charset')&.value
|
|
37
41
|
end
|
|
38
42
|
|
|
39
43
|
def charset_from_meta_content_type
|
|
@@ -45,32 +49,15 @@ module MetaInspector
|
|
|
45
49
|
tag['content'].to_s[/;\s*charset\s*=\s*["']?([^;\s"']+)/i, 1]
|
|
46
50
|
end
|
|
47
51
|
|
|
52
|
+
# Returns every content of the meta tags with that attribute, keyed by
|
|
53
|
+
# its lowercased value
|
|
48
54
|
def meta_tags_by(attribute)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if name && content
|
|
55
|
-
hash[name] ||= []
|
|
56
|
-
hash[name] << content
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
hash
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def convert_each_array_to_first_element_on(hash)
|
|
63
|
-
hash.each_pair do |k, v|
|
|
64
|
-
hash[k] = if v.is_a?(Hash)
|
|
65
|
-
convert_each_array_to_first_element_on(v)
|
|
66
|
-
elsif v.is_a?(Array)
|
|
67
|
-
v.first
|
|
68
|
-
else
|
|
69
|
-
v
|
|
70
|
-
end
|
|
55
|
+
parsed.css("meta[@#{attribute}]").each_with_object({}) do |tag, contents|
|
|
56
|
+
name = tag.attributes[attribute]&.value&.downcase
|
|
57
|
+
content = tag.attributes['content']&.value
|
|
58
|
+
(contents[name] ||= []) << content if name && content
|
|
71
59
|
end
|
|
72
60
|
end
|
|
73
|
-
|
|
74
61
|
end
|
|
75
62
|
end
|
|
76
63
|
end
|