link_thumbnailer 3.3.1 → 3.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/Gemfile +5 -3
- data/Rakefile +2 -0
- data/lib/generators/link_thumbnailer/install_generator.rb +2 -0
- data/lib/generators/templates/initializer.rb +2 -0
- data/lib/link_thumbnailer.rb +2 -0
- data/lib/link_thumbnailer/configuration.rb +72 -70
- data/lib/link_thumbnailer/exceptions.rb +2 -0
- data/lib/link_thumbnailer/grader.rb +2 -0
- data/lib/link_thumbnailer/graders/base.rb +2 -0
- data/lib/link_thumbnailer/graders/html_attribute.rb +2 -0
- data/lib/link_thumbnailer/graders/length.rb +2 -0
- data/lib/link_thumbnailer/graders/link_density.rb +2 -0
- data/lib/link_thumbnailer/graders/position.rb +2 -0
- data/lib/link_thumbnailer/image_comparator.rb +2 -0
- data/lib/link_thumbnailer/image_comparators/base.rb +2 -0
- data/lib/link_thumbnailer/image_comparators/size.rb +2 -0
- data/lib/link_thumbnailer/image_parser.rb +13 -1
- data/lib/link_thumbnailer/image_validator.rb +2 -0
- data/lib/link_thumbnailer/model.rb +20 -17
- data/lib/link_thumbnailer/models/description.rb +2 -0
- data/lib/link_thumbnailer/models/favicon.rb +2 -0
- data/lib/link_thumbnailer/models/image.rb +56 -54
- data/lib/link_thumbnailer/models/title.rb +2 -0
- data/lib/link_thumbnailer/models/video.rb +2 -0
- data/lib/link_thumbnailer/models/website.rb +54 -52
- data/lib/link_thumbnailer/page.rb +2 -0
- data/lib/link_thumbnailer/parser.rb +2 -0
- data/lib/link_thumbnailer/processor.rb +2 -0
- data/lib/link_thumbnailer/railtie.rb +2 -0
- data/lib/link_thumbnailer/response.rb +2 -0
- data/lib/link_thumbnailer/scraper.rb +62 -60
- data/lib/link_thumbnailer/scrapers/base.rb +69 -67
- data/lib/link_thumbnailer/scrapers/default/base.rb +2 -0
- data/lib/link_thumbnailer/scrapers/default/description.rb +2 -0
- data/lib/link_thumbnailer/scrapers/default/favicon.rb +2 -0
- data/lib/link_thumbnailer/scrapers/default/images.rb +5 -1
- data/lib/link_thumbnailer/scrapers/default/title.rb +2 -0
- data/lib/link_thumbnailer/scrapers/default/videos.rb +2 -0
- data/lib/link_thumbnailer/scrapers/opengraph/base.rb +2 -0
- data/lib/link_thumbnailer/scrapers/opengraph/description.rb +2 -0
- data/lib/link_thumbnailer/scrapers/opengraph/favicon.rb +2 -0
- data/lib/link_thumbnailer/scrapers/opengraph/image.rb +2 -0
- data/lib/link_thumbnailer/scrapers/opengraph/images.rb +2 -0
- data/lib/link_thumbnailer/scrapers/opengraph/title.rb +2 -0
- data/lib/link_thumbnailer/scrapers/opengraph/video.rb +2 -0
- data/lib/link_thumbnailer/scrapers/opengraph/videos.rb +2 -0
- data/lib/link_thumbnailer/uri.rb +2 -0
- data/lib/link_thumbnailer/version.rb +3 -1
- data/lib/link_thumbnailer/video_parser.rb +3 -1
- data/link_thumbnailer.gemspec +7 -5
- data/spec/configuration_spec.rb +2 -0
- data/spec/fixture_spec.rb +2 -0
- data/spec/grader_spec.rb +2 -0
- data/spec/graders/base_spec.rb +2 -0
- data/spec/graders/html_attribute_spec.rb +2 -0
- data/spec/graders/length_spec.rb +2 -0
- data/spec/graders/link_density_spec.rb +2 -0
- data/spec/graders/position_spec.rb +2 -0
- data/spec/image_comparators/size_spec.rb +2 -0
- data/spec/image_validator_spec.rb +2 -0
- data/spec/model_spec.rb +2 -0
- data/spec/models/description_spec.rb +2 -0
- data/spec/models/favicon_spec.rb +2 -0
- data/spec/models/image_spec.rb +2 -0
- data/spec/models/title_spec.rb +2 -0
- data/spec/models/video_spec.rb +2 -0
- data/spec/models/website_spec.rb +2 -0
- data/spec/page_spec.rb +2 -0
- data/spec/processor_spec.rb +2 -0
- data/spec/response_spec.rb +2 -0
- data/spec/scraper_spec.rb +2 -0
- data/spec/scrapers/base_spec.rb +2 -0
- data/spec/scrapers/opengraph/base_spec.rb +2 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/uri_spec.rb +2 -0
- data/spec/video_parser_spec.rb +2 -0
- metadata +23 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 281992a6403788c75541c08d69586ab9b85c097e
|
4
|
+
data.tar.gz: 7cd32584b4c3a96b6a54185576597115fdb84764
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d77d1ae2545a92f79363a1e90d19edd61c610080d5497207cf22bdb9afe0d0acb5569aa95d7c16485dc9aa3ff0beac5c900b7cea24278d87d5a0812900f7aa91
|
7
|
+
data.tar.gz: 69c55be441d1581c50e54b3f3652130d8c4f9220047714607d978a993c6be581b28b70cb19a62ad3f27dc09c91455d67f4e83bceff753b451a4d3b3a1e532763
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
source 'https://rubygems.org'
|
2
4
|
|
3
5
|
# Specify your gem's dependencies in link_thumbnailer.gemspec
|
4
6
|
gemspec
|
5
7
|
|
6
8
|
group :development, :test do
|
7
|
-
gem 'rspec', '
|
8
|
-
gem 'webmock', '
|
9
|
-
gem 'pry', '
|
9
|
+
gem 'rspec', '>= 2.14'
|
10
|
+
gem 'webmock', '>= 1.14'
|
11
|
+
gem 'pry', '>= 0.9'
|
10
12
|
end
|
data/Rakefile
CHANGED
data/lib/link_thumbnailer.rb
CHANGED
@@ -1,70 +1,72 @@
|
|
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
|
-
%r{^http://
|
47
|
-
%r{^http://
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
->(description) { ::LinkThumbnailer::Graders::
|
54
|
-
->(description) { ::LinkThumbnailer::Graders::
|
55
|
-
->(description) { ::LinkThumbnailer::Graders::
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
@
|
60
|
-
@
|
61
|
-
@
|
62
|
-
@
|
63
|
-
@
|
64
|
-
@
|
65
|
-
@
|
66
|
-
@
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LinkThumbnailer
|
4
|
+
|
5
|
+
# Access point for the gem configurations.
|
6
|
+
#
|
7
|
+
# @return [LinkThumbnailer::Configuration] a configuration instance.
|
8
|
+
def self.config
|
9
|
+
@config ||= Configuration.new
|
10
|
+
end
|
11
|
+
|
12
|
+
# Configure hook used in the gem initializer. Convinient way to set all the
|
13
|
+
# gem configurations.
|
14
|
+
#
|
15
|
+
# @example inside config/initializers/link_thumbnaler.rb
|
16
|
+
# LinkThumbnailer.configure do |config|
|
17
|
+
# config.user_agent = 'link_thumbnailer'
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# @return [void]
|
21
|
+
def self.configure
|
22
|
+
yield config if block_given?
|
23
|
+
end
|
24
|
+
|
25
|
+
class Configuration
|
26
|
+
|
27
|
+
attr_accessor :redirect_limit, :blacklist_urls, :user_agent,
|
28
|
+
:verify_ssl, :http_open_timeout, :http_read_timeout, :attributes,
|
29
|
+
:graders, :description_min_length, :positive_regex, :negative_regex,
|
30
|
+
:image_limit, :image_stats, :raise_on_invalid_format, :max_concurrency,
|
31
|
+
:scrapers, :http_override_headers, :encoding
|
32
|
+
|
33
|
+
alias_method :http_timeout, :http_open_timeout
|
34
|
+
alias_method :http_timeout=, :http_open_timeout=
|
35
|
+
|
36
|
+
# Create a new instance.
|
37
|
+
#
|
38
|
+
# @return [LinkThumbnailer::Configuration]
|
39
|
+
def initialize
|
40
|
+
@redirect_limit = 3
|
41
|
+
@user_agent = 'link_thumbnailer'
|
42
|
+
@verify_ssl = true
|
43
|
+
@http_open_timeout = 5
|
44
|
+
@http_read_timeout = 5
|
45
|
+
@blacklist_urls = [
|
46
|
+
%r{^http://ad\.doubleclick\.net/},
|
47
|
+
%r{^http://b\.scorecardresearch\.com/},
|
48
|
+
%r{^http://pixel\.quantserve\.com/},
|
49
|
+
%r{^http://s7\.addthis\.com/}
|
50
|
+
]
|
51
|
+
@attributes = [:title, :images, :description, :videos, :favicon]
|
52
|
+
@graders = [
|
53
|
+
->(description) { ::LinkThumbnailer::Graders::Length.new(description) },
|
54
|
+
->(description) { ::LinkThumbnailer::Graders::HtmlAttribute.new(description, :class) },
|
55
|
+
->(description) { ::LinkThumbnailer::Graders::HtmlAttribute.new(description, :id) },
|
56
|
+
->(description) { ::LinkThumbnailer::Graders::Position.new(description, weigth: 3) },
|
57
|
+
->(description) { ::LinkThumbnailer::Graders::LinkDensity.new(description) },
|
58
|
+
]
|
59
|
+
@description_min_length = 50
|
60
|
+
@positive_regex = /article|body|content|entry|hentry|main|page|pagination|post|text|blog|story/i
|
61
|
+
@negative_regex = /combx|comment|com-|contact|foot|footer|footnote|masthead|media|meta|outbrain|promo|related|scroll|shoutbox|sidebar|sponsor|shopping|tags|tool|widget|modal/i
|
62
|
+
@image_limit = 5
|
63
|
+
@image_stats = true
|
64
|
+
@raise_on_invalid_format = false
|
65
|
+
@max_concurrency = 20
|
66
|
+
@scrapers = [:opengraph, :default]
|
67
|
+
@http_override_headers = { 'Accept-Encoding' => 'none' }
|
68
|
+
@encoding = 'utf-8'
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'image_info'
|
2
4
|
|
3
5
|
module LinkThumbnailer
|
@@ -6,7 +8,7 @@ module LinkThumbnailer
|
|
6
8
|
attr_reader :images
|
7
9
|
|
8
10
|
def initialize(urls)
|
9
|
-
@images = perform? ?
|
11
|
+
@images = perform? ? image_info(urls) : default_images(urls)
|
10
12
|
end
|
11
13
|
|
12
14
|
def size
|
@@ -19,6 +21,10 @@ module LinkThumbnailer
|
|
19
21
|
|
20
22
|
private
|
21
23
|
|
24
|
+
def default_images(urls)
|
25
|
+
Array(urls).compact.map(&method(:build_default_image))
|
26
|
+
end
|
27
|
+
|
22
28
|
def build_default_image(uri)
|
23
29
|
NullImage.new(uri)
|
24
30
|
end
|
@@ -31,6 +37,12 @@ module LinkThumbnailer
|
|
31
37
|
::LinkThumbnailer.page.config.max_concurrency
|
32
38
|
end
|
33
39
|
|
40
|
+
def image_info(urls)
|
41
|
+
::ImageInfo.from(urls, max_concurrency: max_concurrency)
|
42
|
+
rescue
|
43
|
+
default_images(urls)
|
44
|
+
end
|
45
|
+
|
34
46
|
class NullImage
|
35
47
|
attr_reader :uri
|
36
48
|
|
@@ -1,17 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LinkThumbnailer
|
4
|
+
class Model
|
5
|
+
|
6
|
+
def to_json(*args)
|
7
|
+
as_json.to_json(*args)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def sanitize(str)
|
13
|
+
return unless str
|
14
|
+
|
15
|
+
str = str.dup
|
16
|
+
str.encode!("UTF-16", "UTF-8", invalid: :replace, undef: :replace, replace: "")
|
17
|
+
str.encode!("UTF-8", "UTF-16").strip.gsub(/[\r\n\f]+/, "\n")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -1,54 +1,56 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'link_thumbnailer/
|
4
|
-
require 'link_thumbnailer/
|
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
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'link_thumbnailer/model'
|
4
|
+
require 'link_thumbnailer/image_parser'
|
5
|
+
require 'link_thumbnailer/image_comparator'
|
6
|
+
require 'link_thumbnailer/image_validator'
|
7
|
+
|
8
|
+
module LinkThumbnailer
|
9
|
+
module Models
|
10
|
+
class Image < ::LinkThumbnailer::Model
|
11
|
+
|
12
|
+
attr_reader :src, :type, :size
|
13
|
+
|
14
|
+
def initialize(src, size = nil, type = nil)
|
15
|
+
@src = src
|
16
|
+
@size = size || parser.size
|
17
|
+
@type = type || parser.type
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
src.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def <=>(other)
|
25
|
+
comparator.call(other)
|
26
|
+
end
|
27
|
+
|
28
|
+
def valid?
|
29
|
+
validator.call
|
30
|
+
end
|
31
|
+
|
32
|
+
def as_json(*)
|
33
|
+
{
|
34
|
+
src: src.to_s,
|
35
|
+
size: size,
|
36
|
+
type: type
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def parser
|
43
|
+
@parser ||= ::LinkThumbnailer::ImageParser.new(src)
|
44
|
+
end
|
45
|
+
|
46
|
+
def validator
|
47
|
+
::LinkThumbnailer::ImageValidator.new(self)
|
48
|
+
end
|
49
|
+
|
50
|
+
def comparator
|
51
|
+
::LinkThumbnailer::ImageComparator.new(self)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|