best_image 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +37 -0
- data/Rakefile +2 -0
- data/best_image.gemspec +22 -0
- data/lib/best_image.rb +9 -0
- data/lib/best_image/image_finder.rb +70 -0
- data/lib/best_image/image_reference.rb +30 -0
- data/lib/best_image/image_selector.rb +11 -0
- data/lib/best_image/version.rb +3 -0
- data/spec/best_image/image_finder_spec.rb +70 -0
- data/spec/best_image/image_reference_spec.rb +39 -0
- data/spec/best_image/image_selector_spec.rb +13 -0
- data/spec/spec_helper.rb +6 -0
- metadata +128 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Scott Johnson
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# BestImage
|
2
|
+
|
3
|
+
This gem accepts a url and returns the *best image* found on that page. Best is determined by largest at this time, but in the future I would like to look for FaceBook meta tags that allow the publisher to set the best image.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'best_image'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install best_image
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
require 'best_image'
|
21
|
+
|
22
|
+
image_selector = BestImage::ImageSelector.new("http://www.path/to/page")
|
23
|
+
|
24
|
+
if image_selector.best_image
|
25
|
+
@best_image = image_selector.best_image
|
26
|
+
else
|
27
|
+
# do defalut action in case network is down etc.
|
28
|
+
image_selector.errors
|
29
|
+
end
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/best_image.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/best_image/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Scott Johnson"]
|
6
|
+
gem.email = ["7.scott.j@gmail.com"]
|
7
|
+
gem.description = %q{Returns the *Best Image* found from a passed url}
|
8
|
+
gem.summary = %q{This gem scrapes a passed web page and returns the url of the *best image* found on that page.}
|
9
|
+
gem.homepage = "https://github.com/itchy/best_image"
|
10
|
+
|
11
|
+
gem.add_development_dependency "rspec"
|
12
|
+
gem.add_dependency('image_size', '>= 1.1.1')
|
13
|
+
gem.add_dependency('nokogiri', '>= 1.5.2')
|
14
|
+
# gem.add_dependency('net/http')
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($\)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.name = "best_image"
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
gem.version = BestImage::VERSION
|
22
|
+
end
|
data/lib/best_image.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
module BestImage
|
2
|
+
class ImageFinder
|
3
|
+
attr_accessor :images, :errors, :messages
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@errors = []; @messages = []; @images = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def parse(path)
|
10
|
+
set_uri(path)
|
11
|
+
set_response
|
12
|
+
set_doc
|
13
|
+
set_images
|
14
|
+
rescue IOError => e
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def best_image
|
19
|
+
@best_image ||= calculate_best_image
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def calculate_best_image
|
25
|
+
return nil unless @errors.empty?
|
26
|
+
return nil if @images.empty?
|
27
|
+
best_image = self.images.sort.last
|
28
|
+
return nil unless best_image.size > 0
|
29
|
+
best_image.url
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_uri(path)
|
33
|
+
@uri = URI.parse(path)
|
34
|
+
end
|
35
|
+
|
36
|
+
def set_response
|
37
|
+
@response = Net::HTTP.get_response(@uri)
|
38
|
+
rescue Timeout::Error => e
|
39
|
+
self.errors << e
|
40
|
+
self.messages << "A Timeout error occured while trying to access #{@uri}"
|
41
|
+
raise IOError
|
42
|
+
rescue SocketError => e
|
43
|
+
self.errors << e
|
44
|
+
self.messages << "An error occured trying to access #{@uri}"
|
45
|
+
raise IOError
|
46
|
+
end
|
47
|
+
|
48
|
+
def set_doc
|
49
|
+
return unless @response
|
50
|
+
@doc = Nokogiri::HTML(@response.body)
|
51
|
+
end
|
52
|
+
|
53
|
+
def set_images
|
54
|
+
return unless @doc
|
55
|
+
image_links = @doc.css('img')
|
56
|
+
self.images = image_links.map do |link|
|
57
|
+
src = full_path(link[:src])
|
58
|
+
ImageReference.new(src)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def full_path(image)
|
63
|
+
if image[/^\//]
|
64
|
+
"http://#{@uri.host}#{image}"
|
65
|
+
else
|
66
|
+
image
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module BestImage
|
2
|
+
class ImageReference
|
3
|
+
attr_accessor :errors
|
4
|
+
attr_reader :url
|
5
|
+
|
6
|
+
def initialize(url)
|
7
|
+
@errors = []
|
8
|
+
@url = url
|
9
|
+
end
|
10
|
+
|
11
|
+
def size
|
12
|
+
@size ||= calculate_file_size
|
13
|
+
end
|
14
|
+
|
15
|
+
def <=>(other)
|
16
|
+
self.size <=> other.size
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def calculate_file_size
|
21
|
+
open(URI.encode(@url), 'rb') do |fh|
|
22
|
+
h,w = ImageSize.new(fh).size
|
23
|
+
h*w
|
24
|
+
end
|
25
|
+
rescue OpenURI::HTTPError => e
|
26
|
+
self.errors << e
|
27
|
+
0
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# This class is created as a convience class for creating and parsing at the same time
|
2
|
+
# seperating these functions in ImageFinder provided a conviencience for testing
|
3
|
+
|
4
|
+
module BestImage
|
5
|
+
class ImageSelector < ImageFinder
|
6
|
+
def initialize(path)
|
7
|
+
super()
|
8
|
+
parse(path)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module BestImage
|
4
|
+
describe ImageFinder do
|
5
|
+
let(:good_url) { "http://www.google.com" }
|
6
|
+
let(:bad_url) { "http://www.some-url-that-should-never-be-findable.com" }
|
7
|
+
let(:good_image_url) { "http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" }
|
8
|
+
|
9
|
+
it "should fail gracefully and set an error message if it cannot access url" do
|
10
|
+
best = BestImage::ImageFinder.new
|
11
|
+
best.parse(bad_url)
|
12
|
+
best.errors.should_not be_empty
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should parse the passed url into a uri" do
|
16
|
+
best = BestImage::ImageFinder.new
|
17
|
+
best.send :set_uri, good_url
|
18
|
+
best.send :set_response
|
19
|
+
best.errors.empty?.should be_true
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should collect an array of images found on the page" do
|
23
|
+
best = BestImage::ImageFinder.new
|
24
|
+
best.parse(good_url)
|
25
|
+
best.images.should_not be_empty
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return the best image on a page" do
|
29
|
+
test_url_match = "large"
|
30
|
+
small = ImageReference.new(bad_url)
|
31
|
+
small.stub!(:size).and_return(1)
|
32
|
+
small.stub!(:url).and_return("small")
|
33
|
+
medium = ImageReference.new(bad_url)
|
34
|
+
medium.stub!(:size).and_return(2)
|
35
|
+
medium.stub!(:url).and_return("medium")
|
36
|
+
large = ImageReference.new(bad_url)
|
37
|
+
large.stub!(:size).and_return(3)
|
38
|
+
large.stub!(:url).and_return(test_url_match)
|
39
|
+
|
40
|
+
best = BestImage::ImageFinder.new
|
41
|
+
best.images << small
|
42
|
+
best.images << large
|
43
|
+
best.images << medium
|
44
|
+
|
45
|
+
best.best_image.should eq(test_url_match)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return nil for best_image if it has errors" do
|
49
|
+
best = BestImage::ImageFinder.new
|
50
|
+
best.instance_variable_set("@errors", ["not being nil"])
|
51
|
+
best.best_image.should be_nil
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should return nil for best_image if it has no images" do
|
55
|
+
best = BestImage::ImageFinder.new
|
56
|
+
best.stub!(:images).and_return([])
|
57
|
+
best.best_image.should be_nil
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should return nil for best_image if the largest image is <= 0" do
|
61
|
+
small = ImageReference.new(bad_url)
|
62
|
+
small.stub!(:size).and_return(0)
|
63
|
+
|
64
|
+
best = BestImage::ImageFinder.new
|
65
|
+
best.stub!(:images).and_return([small])
|
66
|
+
best.best_image.should be_nil
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module BestImage
|
4
|
+
describe ImageReference do
|
5
|
+
let(:good_url) { "http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" }
|
6
|
+
let(:bad_url) { "http://www.google.com/an/image/that/dosent/exist/logo1w.png" }
|
7
|
+
|
8
|
+
it "should fail gracefully if it cannot get the image and return 0 for the size" do
|
9
|
+
image = ImageReference.new(bad_url)
|
10
|
+
image.size.should == 0
|
11
|
+
image.errors.should_not be_empty
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should calculate the size of the image and store it in the instance variable" do
|
15
|
+
image = ImageReference.new(good_url)
|
16
|
+
image.size.should_not == 0
|
17
|
+
image.instance_variable_get("@size").should_not == 0
|
18
|
+
image.errors.should be_empty, image.errors.inspect
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
it "should sort by size, smallest to largest" do
|
23
|
+
small = ImageReference.new(bad_url)
|
24
|
+
small.stub!(:size).and_return(1)
|
25
|
+
medium = ImageReference.new(bad_url)
|
26
|
+
medium.stub!(:size).and_return(2)
|
27
|
+
large = ImageReference.new(bad_url)
|
28
|
+
large.stub!(:size).and_return(3)
|
29
|
+
|
30
|
+
(2 <=> 1).should == 1
|
31
|
+
(medium <=> (small)).should == 1
|
32
|
+
(large <=> (small)).should == 1
|
33
|
+
(large <=> (medium)).should == 1
|
34
|
+
(large <=> (large)).should == 0
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module BestImage
|
4
|
+
describe ImageSelector do
|
5
|
+
let(:good_url) { "http://www.google.com" }
|
6
|
+
|
7
|
+
it "should create a new ImageFinder and call parse with the passed parameter" do
|
8
|
+
ImageFinder.should_receive(:new).with(good_url)
|
9
|
+
ImageSelector.new(good_url)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: best_image
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Scott Johnson
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-10-30 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: image_size
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 17
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 1
|
46
|
+
- 1
|
47
|
+
version: 1.1.1
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: nokogiri
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 7
|
59
|
+
segments:
|
60
|
+
- 1
|
61
|
+
- 5
|
62
|
+
- 2
|
63
|
+
version: 1.5.2
|
64
|
+
type: :runtime
|
65
|
+
version_requirements: *id003
|
66
|
+
description: Returns the *Best Image* found from a passed url
|
67
|
+
email:
|
68
|
+
- 7.scott.j@gmail.com
|
69
|
+
executables: []
|
70
|
+
|
71
|
+
extensions: []
|
72
|
+
|
73
|
+
extra_rdoc_files: []
|
74
|
+
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- best_image.gemspec
|
82
|
+
- lib/best_image.rb
|
83
|
+
- lib/best_image/image_finder.rb
|
84
|
+
- lib/best_image/image_reference.rb
|
85
|
+
- lib/best_image/image_selector.rb
|
86
|
+
- lib/best_image/version.rb
|
87
|
+
- spec/best_image/image_finder_spec.rb
|
88
|
+
- spec/best_image/image_reference_spec.rb
|
89
|
+
- spec/best_image/image_selector_spec.rb
|
90
|
+
- spec/spec_helper.rb
|
91
|
+
homepage: https://github.com/itchy/best_image
|
92
|
+
licenses: []
|
93
|
+
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
hash: 3
|
105
|
+
segments:
|
106
|
+
- 0
|
107
|
+
version: "0"
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
hash: 3
|
114
|
+
segments:
|
115
|
+
- 0
|
116
|
+
version: "0"
|
117
|
+
requirements: []
|
118
|
+
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 1.8.24
|
121
|
+
signing_key:
|
122
|
+
specification_version: 3
|
123
|
+
summary: This gem scrapes a passed web page and returns the url of the *best image* found on that page.
|
124
|
+
test_files:
|
125
|
+
- spec/best_image/image_finder_spec.rb
|
126
|
+
- spec/best_image/image_reference_spec.rb
|
127
|
+
- spec/best_image/image_selector_spec.rb
|
128
|
+
- spec/spec_helper.rb
|