Danbooru

Aug 14
Permalink

source code (08/14/07)

#!/usr/bin/env ruby
# MIT Licence.

require ‘net/http’
require ‘uri’
require ‘open-uri’

require ‘rubygems’ rescue LoadError
require ‘hpricot’
require ‘cgi’

module Tumbler
class TumblerWriteError < StandardError
def initialize(*args)
super
@body = nil
@code = nil
end
attr_accessor :body, :code
end

class Writer
TUMBLR_API_WRITE = URI.parse(‘http://www.tumblr.com/api/write’)

def initialize(email, password)
@email = email
@password = password
end

# post(‘photo’, :source => ‘http://example.com’, :caption => ‘foo’)
def post(type, post_data)
d = {
:type => type,
:email => @email,
:password => @password
}
res = Net::HTTP.post_form(TUMBLR_API_WRITE, post_data.merge(d))

unless res.code.to_s == ‘200’ || res.code.to_s == ‘201’
e = TumblerWriteError.new(‘response code error’ )
e.code = res.code
e.body = res.body
raise e
end

res.body
end
end
end

module Danbooru
class DanbooruScraper
def caption_generate(title, url)
“<a href=’#{CGI::escapeHTML(url)}’>#{CGI::escapeHTML(title)}</a>”
end

# return Array {:source => url, :caption => link_html_text }
def image_list
[]
end
end

class HpricotScraper < DanbooruScraper
def initialize
setting
end

def setting(url, elements_xpath)
@source = Hpricot(open(url).read)
@elements_xpath = elements_xpath
end

def image_list
list = []
@source.search(@elements_xpath).each do |el|
l = select(el)
list « l if l
end
list
end

def select(el)
nil
end
end

class AtomScraper < HpricotScraper
def setting(url)
super(url, ‘//entry’)
end

def select(el)
img = el.search(‘//img’)[0]
src = img[‘src’].sub(‘preview/’, ”)
t = el.search(‘//title’)[0].inner_text
href = img.parent[‘href’]
caption = caption_generate(t, href)
{:source => src, :caption => caption}
end
end

# MoeImouto must request referrer.
# class MoeImouto < AtomScraper
# def setting
# super(‘http://moe.imouto.org/post/atom’)
# end
# end

class NekoBooru < AtomScraper
def setting
super(‘http://nekobooru.net/post/atom/tags/’)
end
end

class MikoMiko < AtomScraper
def setting
super(‘http://mikomiko.org/post/atom/tags/’)
end
end

class Gunbooru < HpricotScraper
def setting
super(‘http://gelbooru.com/index.php?page=post&s=list’, ‘//a/img’)
end

def select(el)
if el[‘title’]
t = el[‘title’].split(‘score’)[0].strip
src = src_generate(t, el[‘src’])
caption = caption_generate(t, ‘http://gelbooru.com/’ + el.parent[‘href’])
{:source => src, :caption => caption}
end
end

def src_generate(title, src)
src.sub(‘thumbnails’,’images’).sub(‘thumbnail_’, ”).sub(‘.png’,’.jpg’)
end
end

class KonaChan < HpricotScraper
def setting
super(‘http://konachan.com/post/list’, ‘//tr/td/a/img’)
end

def select(el)
if el[‘title’]
t = el[‘title’].split(’ //’)[0]
src = src_generate(t, el[‘src’])
caption = caption_generate(t, el.parent[‘href’])
{:source => src, :caption => caption}
end
end

def src_generate(title, src)
source = src.sub(‘/_thumbs/’, ‘/_images/’).sub(‘.png’,’.jpg’)
source.sub(‘FileView’, URI::escape(title)).gsub(’ ‘, ‘%20’)
end
end

class AnimeMahou < HpricotScraper
def setting
super(‘http://www.animemahou.com/index’, ‘//a/img’)
end

def select(el)
if el[‘title’]
t = el[‘title’].split(’ //’)[0]
src = src_generate(t, el[‘src’])
caption = caption_generate(t, el.parent[‘href’])
{:source => src, :caption => caption}
end
end

def src_generate(title, src)
src.sub(‘/thumbs/’, ‘/images/’).sub(‘.png’,’.jpg’)
end
end

class LoliSuki < HpricotScraper
def setting
super(‘http://img.lolisuki.com/’, ‘//a/img’)
end

def select(el)
if el[‘title’]
t = el[‘title’].split(‘score’)[0].strip
caption = caption_generate(t, el.parent[‘href’])
src = src_generate(t, el[‘src’], el.parent[‘href’])
{:source => src, :caption => caption}
end
end

def src_generate(title, src, href)
num = href.match(%r{/(\d+$)})[1]
‘http://img.lolisuki.com’ + src.sub(‘thumb_’,’image_’).sub(%r{\/[^\/.]+.jpg$}, “/#{num}.jpg”)
end
end

end

if __FILE__ == $0
targets = %w[
NekoBooru
MikoMiko
Gunbooru
KonaChan
AnimeMahou
LoliSuki
]
def test(target)
raise ‘don”t get image_list’ if Danbooru.const_get(target).new.image_list.empty?
end

puts ‘test start’
errors = []
targets.each do |target|
puts “now testing: #{target}”
begin
test(target)
rescue => e
require ‘pp’
puts ‘error!’
pp e
errors « e
end
end
if errors.empty?
puts ‘all test success!’
else
puts “#{errors.length} error(s)”
end
end

  1. danbooru posted this