fotofetch 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d0af691137a1f90911465307669bdc22ec978018
4
+ data.tar.gz: 9fdfcebcdd775ec9d1c8c9fd0e5248326a2edbd9
5
+ SHA512:
6
+ metadata.gz: fdb59a2834c51cff47448a555378261724ffa7fc7e877b42af3c0b0134586edf38381fc771bd4d314bd07d679904324a40afe7faad56fe15a517ed8a29c72457
7
+ data.tar.gz: f6f976ff5b0857d5567ba2e64024eb39406f85051690fa08e98d0682926c108961aa17de51a1f125587e0d780527e54ad880b2b02ef90a4139ca4e66ef0ff1a0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fotofetch.gemspec
4
+ gemspec
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fotofetch (0.1.0)
5
+ fastimage
6
+ mechanize
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.4.0)
12
+ byebug (8.2.1)
13
+ domain_name (0.5.25)
14
+ unf (>= 0.0.5, < 1.0.0)
15
+ fastimage (1.8.1)
16
+ addressable (~> 2.3, >= 2.3.5)
17
+ http-cookie (1.0.2)
18
+ domain_name (~> 0.5)
19
+ mechanize (2.7.4)
20
+ domain_name (~> 0.5, >= 0.5.1)
21
+ http-cookie (~> 1.0)
22
+ mime-types (>= 1.17.2, < 3)
23
+ net-http-digest_auth (~> 1.1, >= 1.1.1)
24
+ net-http-persistent (~> 2.5, >= 2.5.2)
25
+ nokogiri (~> 1.6)
26
+ ntlm-http (~> 0.1, >= 0.1.1)
27
+ webrobots (>= 0.0.9, < 0.2)
28
+ mime-types (2.99)
29
+ mini_portile2 (2.0.0)
30
+ minitest (5.8.3)
31
+ net-http-digest_auth (1.4)
32
+ net-http-persistent (2.9.4)
33
+ nokogiri (1.6.7.2)
34
+ mini_portile2 (~> 2.0.0.rc2)
35
+ ntlm-http (0.1.1)
36
+ rake (10.4.2)
37
+ unf (0.1.4)
38
+ unf_ext
39
+ unf_ext (0.0.7.1)
40
+ webrobots (0.1.2)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ bundler (~> 1.10)
47
+ byebug
48
+ fotofetch!
49
+ minitest
50
+ rake (~> 10.0)
51
+
52
+ BUNDLED WITH
53
+ 1.10.6
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Steven Olson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # Fotofetch
2
+
3
+ Arguments for fetch_links method are: search value, optional number of links returned,
4
+ and optional dimension restrictions (width, height).
5
+ If a dimension argument is positive, it will look for pictures larger than that,
6
+ and if the number is negative, results will be restricted to those smaller than that.
7
+ Initiate an instance to use: @fetcher = Fotofetch::Fetch.new
8
+ The fetch_links method will return results like: http:example.com/image.jpg)
9
+ To find a 1 small photo of Jupiter, call @fetcher.fetch_links("jupiter", 1, -500, -500).
10
+ To find 3 large photos of Jupiter, call @fetcher.fetch_links("jupiter", 3, 1500, 1500).
11
+ To grab just the first provided photo, call @fetcher.fetch_links("jupiter").
12
+ If a small or large enough image is not found, dimension restrictions will be disregarded.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'fotofetch'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install fotofetch
29
+
30
+ ## Usage
31
+
32
+ TODO: Write usage instructions here
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/steveoscar/fotofetch.
43
+
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fotofetch"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fotofetch/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fotofetch"
8
+ spec.version = Fotofetch::VERSION
9
+ spec.authors = ["Steven Olson"]
10
+ spec.email = ["steveoscaro@gmail.com"]
11
+
12
+ spec.summary = %q{Fotofetch searches for and brings in images.}
13
+ spec.description = %q{Fotofetch searches for and brings in images. Fotofetch searches for and brings in images. Fotofetch searches for and brings in images.}
14
+ spec.homepage = "https://github.com/SteveOscar/fotofetch"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = Dir["**/*"].select { |f| File.file? f }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency "mechanize"
31
+ spec.add_runtime_dependency "fastimage"
32
+ spec.add_development_dependency "bundler", "~> 1.10"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "minitest"
35
+ spec.add_development_dependency "byebug"
36
+ end
@@ -0,0 +1,85 @@
1
+ require 'fotofetch/version'
2
+ require 'mechanize'
3
+ require 'open-uri'
4
+ require 'fastimage'
5
+ require 'byebug'
6
+
7
+ module Fotofetch
8
+ class Fetch
9
+
10
+ def fetch_links(topic, amount=1, width= +9999, height= +9999)
11
+ @results = []
12
+ scrape(topic, amount, width, height)
13
+ end
14
+
15
+ def scrape(topic, amount, width, height)
16
+ agent = Mechanize.new
17
+ page = agent.get("http://www.bing.com/images/search?q=#{topic}")
18
+ pluck_urls(page, amount, width, height)
19
+ end
20
+
21
+ def pluck_urls(page, amount, width, height)
22
+ urls = []
23
+ page.links.each { |link| urls << link.href } # gathers all urls.
24
+ pluck_imgs(urls, amount, width, height)
25
+ end
26
+
27
+ def pluck_imgs(urls, amount, width, height)
28
+ urls = (urls.select { |link| link.include?(".jpg") || link.include?(".png") })
29
+ restrict_dimensions(urls, width, height, amount) if restrictions?(width, height)
30
+ @results = urls if @results.empty?
31
+ urls = @results[0..(amount-1)] # selects only number of links desired, default is 1.
32
+ add_sources(urls)
33
+ end
34
+
35
+ def restrictions?(width, height)
36
+ (width != 9999 || height!= 9999) ? true : false
37
+ end
38
+
39
+ def restrict_dimensions(urls, width, height, amount)
40
+ urls.each do |link|
41
+ select_links(link, width, height) unless @results.length >= amount
42
+ end
43
+ end
44
+
45
+ def select_links(link, width, height)
46
+ # Two arrays: first is dimension restrictions, 2nd is link dimensions.
47
+ sizes = [[width, height], link_dimensions(link)]
48
+ unless link_dimensions(link) == [0, 0] # Throws out non-direct-image links
49
+ @results << link if width_ok?(sizes) && height_ok?(sizes)
50
+ end
51
+ end
52
+
53
+ def width_ok?(sizes)
54
+ width = sizes[1][0] - sizes[0][0]
55
+ (0 < width && width < sizes[1][0]) || width > (sizes[1][0] * 2)
56
+ end
57
+
58
+ def height_ok?(sizes)
59
+ height = sizes[1][1] - sizes[0][1]
60
+ (0 < height && height < sizes[1][1]) || height > (sizes[1][1] * 2)
61
+ end
62
+
63
+ # Adds root urls as hash keys
64
+ def add_sources(urls)
65
+ results = {}.compare_by_identity
66
+ urls.each { |link| results[link.split("/")[2]] = link}
67
+ results
68
+ end
69
+
70
+ # takes an array of links
71
+ def save_images(urls, file_path)
72
+ urls.each_with_index do |url, i|
73
+ open("image_#{i}.jpg", 'wb') do |file|
74
+ file << open(url).read
75
+ end
76
+ end
77
+ end
78
+
79
+ def link_dimensions(link)
80
+ size = FastImage.size(link)
81
+ size.nil? ? [0, 0] : size
82
+ end
83
+
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module Fotofetch
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ rake for tests
2
+ uses fastimage to check for image size
3
+
4
+ methods:
5
+
6
+ fetch_links(topic, amount?, optional dimension restrictions) returns an array of relevant photo links
7
+ links = fetch_links('tesla', 2, true)
8
+
9
+ save_images(urls, filepath)
10
+ save_images(links, './')
Binary file
@@ -0,0 +1,83 @@
1
+ require 'test_helper'
2
+ require 'byebug'
3
+
4
+ class FotofetchTest < Minitest::Test
5
+
6
+ def setup
7
+ @ff = Fotofetch::Fetch.new
8
+ end
9
+
10
+ def test_that_it_has_a_version_number
11
+ refute_nil ::Fotofetch::VERSION
12
+ end
13
+
14
+ def test_it_finds_image_links
15
+ links = @ff.fetch_links("tesla model s", 5)
16
+
17
+ assert_equal 5, links.count
18
+ assert links.first[1].include?('.jpg') || links.first[1].include?('.png')
19
+ end
20
+
21
+ def test_it_saves_sources_for_links
22
+ link = @ff.fetch_links("mars", 1)
23
+ source = link.values.first.split("/")[2]
24
+
25
+ assert_equal source, link.keys.first
26
+ end
27
+
28
+ def test_it_can_save_an_image
29
+ link = @ff.fetch_links("tesla", 1).values
30
+ @ff.save_images(link, './')
31
+
32
+ assert File.exist?('image_0.jpg')
33
+ refute File.zero?('image_0.jpg')
34
+ File.delete('image_0.jpg')
35
+ end
36
+
37
+ def test_it_checks_image_size
38
+ link = @ff.fetch_links("mars", 1).values
39
+ dimensions = @ff.link_dimensions(link.first)
40
+
41
+ assert_equal 2, dimensions.count
42
+ end
43
+
44
+ def test_it_validates_dimensions
45
+ sizes = [[-900, 900], [1000, 1000]]
46
+ assert_equal false, @ff.width_ok?(sizes)
47
+ assert_equal true, @ff.height_ok?(sizes)
48
+
49
+ sizes = [[-900, 900], [950, 800]]
50
+ assert_equal false, @ff.width_ok?(sizes)
51
+ assert_equal false, @ff.height_ok?(sizes)
52
+
53
+ sizes = [[900, -900], [1000, 800]]
54
+ assert_equal true, @ff.width_ok?(sizes)
55
+ assert_equal true, @ff.height_ok?(sizes)
56
+
57
+ sizes = [[-900, 900], [800, 800]]
58
+ assert_equal true, @ff.width_ok?(sizes)
59
+ assert_equal false, @ff.height_ok?(sizes)
60
+
61
+ sizes = [[-900, -900], [800, 800]]
62
+ assert_equal true, @ff.width_ok?(sizes)
63
+ assert_equal true, @ff.height_ok?(sizes)
64
+ end
65
+
66
+ def test_it_can_restrict_image_dimensions_integration
67
+ link = @ff.fetch_links("mars", 1, 1000, 1000).values
68
+ big = @ff.link_dimensions(link.first)[0]
69
+
70
+ restricted_link = @ff.fetch_links("tesla", 1, -900, -900).values
71
+ small = @ff.link_dimensions(restricted_link.first)[0]
72
+
73
+ difference = big - small
74
+ assert (difference > 100)
75
+
76
+ restricted_link = @ff.fetch_links("tesla", 1, -900, -900).values
77
+ size = @ff.link_dimensions(restricted_link.first)
78
+
79
+ assert size[0] < 900
80
+ assert size[1] < 900
81
+ end
82
+
83
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'fotofetch'
3
+
4
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fotofetch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Steven Olson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mechanize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fastimage
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Fotofetch searches for and brings in images. Fotofetch searches for and
98
+ brings in images. Fotofetch searches for and brings in images.
99
+ email:
100
+ - steveoscaro@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - fotofetch.gemspec
113
+ - lib/fotofetch.rb
114
+ - lib/fotofetch/version.rb
115
+ - notes.markdown
116
+ - pkg/fotofetch-0.1.0.gem
117
+ - test/fotofetch_test.rb
118
+ - test/test_helper.rb
119
+ homepage: https://github.com/SteveOscar/fotofetch
120
+ licenses:
121
+ - MIT
122
+ metadata:
123
+ allowed_push_host: https://rubygems.org
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.4.8
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Fotofetch searches for and brings in images.
144
+ test_files: []