Class: ImageScraper
- Inherits:
-
Object
- Object
- ImageScraper
- Defined in:
- lib/image_scraper.rb
Instance Attribute Summary collapse
-
#location ⇒ Object
Returns the value of attribute location.
-
#log ⇒ Object
Returns the value of attribute log.
Instance Method Summary collapse
- #create_dir ⇒ Object
-
#initialize(log, location) ⇒ ImageScraper
constructor
A new instance of ImageScraper.
- #scrape ⇒ Object
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
#location ⇒ Object
Returns the value of attribute location.
8 9 10 |
# File 'lib/image_scraper.rb', line 8 def location @location end |
#log ⇒ Object
Returns the value of attribute log.
8 9 10 |
# File 'lib/image_scraper.rb', line 8 def log @log end |
Instance Method Details
#create_dir ⇒ Object
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 |
#scrape ⇒ Object
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 |