-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdanbooru.rb
130 lines (119 loc) · 3.5 KB
/
danbooru.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Author: Ivan "Xeron" Larionov
# E-mail: [email protected]
# Homepage: http://blog.xeron.me
# Version: 2.7
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'lib')
require 'booru'
require 'danbooru'
require 'testbooru'
require 'konachan'
require 'behoimi'
require 'yandere'
options = {}
options[:limits] = {}
optparse = OptionParser.new do |opts| # rubocop:disable Metrics/BlockLength
opts.banner = 'Usage: danbooru.rb [options] "tags"'
opts.separator("\nTarget:")
opts.on(
'-b', '--board BOARDNAME',
'Target board. Supported options: danbooru (default), konachan, behoimi, yandere'
) do |board|
options[:board] = board.to_sym
end
opts.on(
'-P', '--pool POOL_ID',
'Pool ID (tags will be ignored)'
) do |pool|
if /^[1-9][0-9]*/.match?(pool)
options[:pool] = pool.to_i
else
warn "Wrong pool id: #{pool}. It should be a number greater than 0."
exit 1
end
end
opts.separator("\nStorage options:")
opts.on(
'-s', '--storage DIR',
'Storage mode (all images in one dir and symlinks in tagged dirs)'
) do |dir|
options[:storage] = dir
end
opts.on(
'-d', '--directory BASE_DIR',
'Base directory to save images. By default it uses the same location as script'
) do |base_path|
options[:base_path] = base_path
end
opts.on(
'-f', '--filename PATTERN',
'Filename pattern. Supported options: id (default), md5, tags, url (old default)'
) do |filename|
options[:filename] = filename.to_sym
end
opts.separator("\tNote: `-f tags` could miss some files due to filesystems' filename length limitation.")
opts.separator("\nAuthentication:")
opts.separator(' This is optional, but recommended since some boards block access without authentication.')
opts.on('-u', '--user USERNAME', 'Username') do |user|
options[:user] = user
end
opts.on('-p', '--password PASSWORD', 'Password (API Key for danbooru)') do |pass|
options[:password] = pass
end
opts.separator("\nTools:")
opts.separator(
" Ruby's file saver is used by default. You can change it using this options. " \
'`wget` or `curl` binaries should be available.'
)
opts.on('-w', '--wget', 'Download using wget') do
options[:downloader] = :wget
end
opts.on('-c', '--curl', 'Download using curl') do
options[:downloader] = :curl
end
opts.separator("\nLimits:")
opts.separator(' This option could be used multiple times with different limiters.')
opts.on(
'-l', '--limit LIMITER',
'Limiters in the following format: limiter=number. Supported limiters: pages, posts, per_page'
) do |limiter|
if limiter =~ /(pages|posts|per_page)=([1-9][0-9]*)/
options[:limits][Regexp.last_match[1].to_sym] = Regexp.last_match[2].to_i
else
warn \
"Wrong limiter: #{limiter}. It should be pages, posts or per_page and value should be a number greater than 0."
exit 1
end
end
opts.separator("\nHelp:")
opts.on('-h', '--help', 'Print a help message') do
puts opts
exit
end
end
begin
optparse.parse!
rescue StandardError => e
puts e
end
if !options[:pool] && (ARGV.empty? || ARGV[0].empty?)
puts optparse.help
else
board =
case options[:board]
when :konachan
Konachan.new(options)
when :behoimi
Behoimi.new(options)
when :yandere
Yandere.new(options)
when :testbooru
Testbooru.new(options)
else
Danbooru.new(options)
end
if options[:pool]
board.download_by_pool(options[:pool])
else
board.download_by_tags(ARGV[0])
end
end