-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
143 lines (108 loc) · 3.17 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
require 'open-uri'
require 'selenium-webdriver'
require 'uri'
def xpath_url_chapter(id, i)
return "//*[@id='post-#{id}']/div[2]/ul/li[#{i}]/span[1]/a"
end
def xpath_image
return '//img[@class="alignnone size-full wp-image-72251"]'
end
def in_range(v, s, e)
return v.to_i.between?(s.to_i, e.to_i)
end
def parse_integer(s)
return s.delete('^0-9').to_i
end
def is_integer(s)
return s.to_i.to_s == s
end
def valid_kiryuu(s)
uri = URI.parse(s)
return uri.host == 'kiryuu.co'
end
HELP = <<ENDHELP
Usage:
ruby main.rb [-h] [-u] [-s] [-e] [-o]
-h, --help Show this help.
--url Manga url.
--start Start chapter.
--end End Chapter.
--output The folder output.
ENDHELP
args = Hash[ARGV.join(' ').scan(/--?([^=\s]+)(?:=(\S+))?/)]
ARGV.each do |arg|
case arg
when '-h','--help' then args["help"] = true
end
end
if args["help"]
puts HELP
return
end
manga_url = args["url"]
start_chapter = args["start"]
last_chapter = args["end"]
dest = Dir.pwd
if manga_url.nil? || start_chapter.nil? || last_chapter.nil?
raise 'required arg is missing. use -h or --help command to see args'
end
if valid_kiryuu(manga_url) == false
raise 'given url is invalid'
end
if is_integer(start_chapter) == false || is_integer(last_chapter) == false
raise 'given chapter is invalid'
end
if parse_integer(start_chapter) > parse_integer(last_chapter)
raise 'invalid chapter. start chapter must be lower than last chapter'
end
if args["output"].nil? == false
dest = args["output"]
if File.exists?(dest) == false
raise 'destination path must be valid path. or leave it empty'
end
end
options = {
args: ['headless', 'disable-gpu', 'disable-notifications', 'log-level=3'],
w3c: true,
mobileEmulation: {},
prefs: {
:protocol_handler => {
:excluded_schemes => {
tel: false
}
}
}
}
caps = Selenium::WebDriver::Chrome::Options.new(options: options)
driver = Selenium::WebDriver.for(:chrome, options: caps)
driver.get manga_url
id = parse_integer(driver.find_element(:xpath => '/html/head/link[9]').attribute('href'))
links = []
driver.find_elements(:class => 'lchx').each.with_index { |v, i|
element_chapter_url = driver.find_element(:xpath => xpath_url_chapter(id, i+=1))
chapter_url = element_chapter_url.attribute('href')
if (in_range(parse_integer(chapter_url), start_chapter, last_chapter))
links.push({
:chapter => element_chapter_url.text,
:chapter_url => chapter_url
})
end
}
links.reverse!
links.each { |l|
puts "downloading #{l[:chapter]} ..."
driver.navigate.to l[:chapter_url]
driver.find_elements(:xpath => xpath_image()).each.with_index{ |e, i|
src = e.attribute('src')
open(src) { |f|
ext = src.split('.').last()
chapter = l[:chapter]
path = File.join(dest, chapter)
Dir.mkdir(path) unless File.exists?(path)
File.open("#{path}/#{i}.#{ext}", 'wb') do |file|
file.puts f.read
end
}
}
}
puts 'success download'