Class: Fotofetch::Fetch
- Inherits:
-
Object
- Object
- Fotofetch::Fetch
- Defined in:
- lib/fotofetch.rb
Instance Method Summary collapse
-
#add_sources(urls) ⇒ Object
Adds root urls as hash keys.
- #fetch_links(topic, amount = 1, width = +9999, height = +9999) ⇒ Object
- #height_ok?(sizes) ⇒ Boolean
- #link_dimensions(link) ⇒ Object
- #pluck_imgs(urls, amount, width, height) ⇒ Object
- #pluck_urls(page, amount, width, height) ⇒ Object
- #restrict_dimensions(urls, width, height, amount) ⇒ Object
- #restrictions?(width, height) ⇒ Boolean
-
#save_images(urls, file_path) ⇒ Object
takes an array of links.
- #scrape(topic, amount, width, height) ⇒ Object
- #select_links(link, width, height) ⇒ Object
- #width_ok?(sizes) ⇒ Boolean
Instance Method Details
#add_sources(urls) ⇒ Object
Adds root urls as hash keys
63 64 65 66 67 |
# File 'lib/fotofetch.rb', line 63 def add_sources(urls) results = {}.compare_by_identity urls.each { |link| results[link.split("/")[2]] = link} results end |
#fetch_links(topic, amount = 1, width = +9999, height = +9999) ⇒ Object
8 9 10 11 12 |
# File 'lib/fotofetch.rb', line 8 def fetch_links(topic, amount=1, width= +9999, height= +9999) @query = topic @results = [] scrape(topic, amount, width, height) end |
#height_ok?(sizes) ⇒ Boolean
57 58 59 60 |
# File 'lib/fotofetch.rb', line 57 def height_ok?(sizes) height = sizes[1][1] - sizes[0][1] (0 < height && height < sizes[1][1]) || height > (sizes[1][1] * 2) end |
#link_dimensions(link) ⇒ Object
78 79 80 81 |
# File 'lib/fotofetch.rb', line 78 def link_dimensions(link) size = FastImage.size(link) size.nil? ? [0, 0] : size end |
#pluck_imgs(urls, amount, width, height) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/fotofetch.rb', line 26 def pluck_imgs(urls, amount, width, height) urls = (urls.select { |link| link.include?(".jpg") || link.include?(".png") }) restrict_dimensions(urls, width, height, amount) if restrictions?(width, height) @results = urls if @results.empty? urls = @results[0..(amount-1)] # selects only number of links desired, default is 1. add_sources(urls) end |
#pluck_urls(page, amount, width, height) ⇒ Object
20 21 22 23 24 |
# File 'lib/fotofetch.rb', line 20 def pluck_urls(page, amount, width, height) urls = [] page.links.each { |link| urls << link.href } # gathers all urls. pluck_imgs(urls, amount, width, height) end |
#restrict_dimensions(urls, width, height, amount) ⇒ Object
38 39 40 41 42 |
# File 'lib/fotofetch.rb', line 38 def restrict_dimensions(urls, width, height, amount) urls.each do |link| select_links(link, width, height) unless @results.length >= amount end end |
#restrictions?(width, height) ⇒ Boolean
34 35 36 |
# File 'lib/fotofetch.rb', line 34 def restrictions?(width, height) (width != 9999 || height!= 9999) ? true : false end |
#save_images(urls, file_path) ⇒ Object
takes an array of links
70 71 72 73 74 75 76 |
# File 'lib/fotofetch.rb', line 70 def save_images(urls, file_path) urls.each_with_index do |url, i| open("#{@query.gsub(' ', '-')}_#{i}.jpg", 'wb') do |file| file << open(url).read end end end |
#scrape(topic, amount, width, height) ⇒ Object
14 15 16 17 18 |
# File 'lib/fotofetch.rb', line 14 def scrape(topic, amount, width, height) agent = Mechanize.new page = agent.get("http://www.bing.com/images/search?q=#{topic}") pluck_urls(page, amount, width, height) end |
#select_links(link, width, height) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/fotofetch.rb', line 44 def select_links(link, width, height) # Two arrays: first is dimension restrictions, 2nd is link dimensions. sizes = [[width, height], link_dimensions(link)] unless link_dimensions(link) == [0, 0] # Throws out non-direct-image links @results << link if width_ok?(sizes) && height_ok?(sizes) end end |
#width_ok?(sizes) ⇒ Boolean
52 53 54 55 |
# File 'lib/fotofetch.rb', line 52 def width_ok?(sizes) width = sizes[1][0] - sizes[0][0] (0 < width && width < sizes[1][0]) || width > (sizes[1][0] * 2) end |