Class: RubyChan::Scraper

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

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Scraper

Returns a new instance of Scraper.



7
8
9
# File 'lib/scraper.rb', line 7

def initialize(uri)
  @uri = uri
end

Instance Method Details

#scrapeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/scraper.rb', line 11

def scrape
  doc = Nokogiri::HTML(open(@uri))
  doc.xpath('//a').each do |link|
    if(link['href'] =~ /\/\/images.4chan.org\/.+\/src\/\d+\..../)
      uri = URI(link['href'])
      puts "Downloading #{uri}"
      Net::HTTP.start(uri.host) do |http|
        resp = http.get(uri.path)
        open(File.basename(uri.path), "wb") do |file|
          file.write(resp.body)
        end
      end
    end
  end
end