Class: ImageScraper

Inherits:
Object
  • Object
show all
Defined in:
lib/image_scraper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log, location) ⇒ ImageScraper

Returns a new instance of ImageScraper.



10
11
12
13
# File 'lib/image_scraper.rb', line 10

def initialize log, location
	@log = log
	@location = location
end

Instance Attribute Details

#locationObject

Returns the value of attribute location.



8
9
10
# File 'lib/image_scraper.rb', line 8

def location
  @location
end

#logObject

Returns the value of attribute log.



8
9
10
# File 'lib/image_scraper.rb', line 8

def log
  @log
end

Instance Method Details

#create_dirObject



15
16
17
18
19
20
21
# File 'lib/image_scraper.rb', line 15

def create_dir
	if Dir.exists?("#{File.dirname(@location)}/images")
		puts "Exists!"
	else
		Dir.mkdir("#{File.dirname(@location)}/images")
	end
end

#scrapeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/image_scraper.rb', line 23

def scrape
	site_seeker = Mechanize.new { |a| a.log = Logger.new("#{File.dirname(@location)}/images/scrape_summary.log") }
	#For Wiki only
	@log.each do |fig, link|
			ext = link.scan(/\..{3,4}$/)
			begin
				page = site_seeker.get(link) 
				puts "Found #{fig} at #{link}"
				img_page = page.links_with(:text => "Original file").first.click
				puts "Located original image"
				img = img_page.save!("#{File.dirname(@location)}/images/#{fig}#{ext[0]}") 
				puts "#{fig} has been saved."
			rescue
				puts "Uh-oh, something went wrong with #{fig.upcase}"
				next
			end
	end
end