class RubyChan::Scraper

Public Class Methods

new(uri) click to toggle source
# File lib/scraper.rb, line 7
def initialize(uri)
  @uri = uri
end

Public Instance Methods

scrape() click to toggle source
# 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