Skip to content

Instantly share code, notes, and snippets.

@shouya
Created October 18, 2013 07:59
Show Gist options
  • Save shouya/7038020 to your computer and use it in GitHub Desktop.
Save shouya/7038020 to your computer and use it in GitHub Desktop.
konachan picture downloader
require 'nokogiri'
require 'open-uri'
require 'ap'
BASE_URI = 'http://konachan.com/post?tags=%s'
link_list = []
def save_list(list)
File.open('out.list', 'w') do |f|
list.each {|i| f.puts i }
end
puts 'finished, list outputed as "out.list"'
exit
end
trap(:INT) { save_list(link_list) }
tag = ARGV[0] or abort "run with '#{File.basename($0)} <tag_name>'"
load_page = BASE_URI % URI.encode(tag)
loop do
html = Nokogiri::HTML(open(load_page))
# html = Nokogiri::HTML(open('./test.html'))
link_list += html.css('a.directlink').map {|x| x['href'] }
next_page = html.css('a.next_page')[0]['href'] rescue break
load_page = URI.join(BASE_URI % 'yo', next_page) or break
puts "loading next page, number of links read: #{link_list.length}."
end
save_list(link_list)
# puts html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment