-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawler.rb
More file actions
260 lines (222 loc) · 9.03 KB
/
Copy pathcrawler.rb
File metadata and controls
260 lines (222 loc) · 9.03 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
require 'open-uri'
require 'digest'
require 'phash/image'
module Crawler
JUNK_IMAGE_PHASHES = Dir["resources/junk/*.jpg"].map do |jpg|
phash_data = nil
if m = jpg.match(/_(\d+)_/)
phash_data = Phash::ImageHash.new(m[1].to_i)
else
phash_data = Phash::Image.new(jpg).phash
`mv #{jpg} #{jpg.sub(".jpg", "_#{phash_data.data}_.jpg")}`
end
phash_data
end
JUNK_FOOTER_PHASHES = { }
Dir["resources/junk/_footers/*.png"].each do |png|
w, h, phash_data = nil
if tmp = png.match(/_(\d+)_(\d+)x(\d+)/)
phash_data = Phash::ImageHash.new(tmp[1].to_i)
w = tmp[2].to_i
h = tmp[3].to_i
else
tmp = `identify #{png}`.match(/(JPEG|PNG) (\d+)x(\d+)/).to_a
w = tmp[2].to_i
h = tmp[3].to_i
tmp = png.sub(".png", "_800x200")
`convert #{png} -crop 800x200+0+#{h-200}\! #{tmp}`
phash_data = Phash::Image.new(tmp).phash
`mv #{png} #{png.sub(".png", "_#{phash_data.data}_#{w}x#{h}.png")}`
end
JUNK_FOOTER_PHASHES[phash_data] = [w, h]
end
def self.parse(html, story, subdir, x, nextchap = true)
File.open("#{subdir}/index.tmp", "wt") do |f|
f.puts "{ \"title\": \"#{story}/chapter-#{x}\", \"weight\": #{x} }"
nextchap_link = "<a class=\"nextchap\" href=\"/#{story}/chapter-#{x + 1}\">chapter-#{x + 1}</a>"
m = html.match(/id=['"]content_chap['"](.+?)div_info_bottom/mi)
unless m
m = "" if html.include?("thử nghiệm hình thức đọc truyện trên video")
else
m = m[1]
end
names = []
matches = m.scan(/<img.+?src=['"]([^'"]+)/mi)
n = matches.size
# puts matches
matches.each_with_index do |m, i|
# skip cover page
next if i == 0 && x > 1
img_url = m[0]
if (m = img_url.match(/url=(http.+)$/i))
print "#{img_url} => "
img_url = URI.unescape(m[1])
end
name = Digest::MD5.hexdigest img_url
img_url = img_url.strip.gsub(" ", "")
prefix = sprintf("#{story}_%04d_%02d", x, i)
names << name
bpg = "#{subdir}/#{prefix}-#{name}.bpg"
webp = "#{subdir}/#{prefix}-#{name}.webp"
jpg = Dir["#{subdir}/*-#{name}.jpg"].first || "#{subdir}/#{prefix}-#{name}.jpg"
png = "#{subdir}/#{prefix}-#{name}.png"
`wget "#{img_url}" -O #{jpg}` unless File.exists?(jpg)
w = h = nil
if tmp = jpg.match(/(\d+)x(\d+)/)
w = tmp[1].to_i
h = tmp[2].to_i
# if w <= 200
# `wget "#{img_url}" -O #{jpg}`
# tmp = `identify #{jpg}`.match(/(JPEG|PNG) (\d+)x(\d+)/).to_a
# w = tmp[2].to_i
# h = tmp[3].to_i
# end
else
tmp = `identify #{jpg}`.match(/(JPEG|PNG) (\d+)x(\d+)/).to_a
w = tmp[2].to_i
h = tmp[3].to_i
end
puts "[#{i}] #{img_url} => #{name}"
# puts jpg
if w > 200
old_jpg = jpg
jpg = "#{subdir}/#{prefix}-#{w}x#{h}-#{name}.jpg"
`mv #{old_jpg} #{jpg}` if old_jpg != jpg
# whole image junk detection
if (i >= n - 2 || i <= 2 || w >= h) # two first/last images or horizontal images
phash_data = nil
if (m = Dir["#{subdir}/_*_#{name}.jpg"].first.to_s.match(/_(\d+)_#{name}/))
phash_data = Phash::ImageHash.new(m[1].to_i)
else
if phash_data = Phash::Image.new(jpg).phash
`cp #{jpg} #{subdir}/_#{phash_data.data}_#{name}.jpg`
end
end
if phash_data
junk_found = false
JUNK_IMAGE_PHASHES.each do |junk|
if junk.similarity(phash_data) > 0.85
junk_found = true
puts "Junk found: #{jpg}"
break
end
end
if junk_found
if File.size?(webp) ; puts cmd = "rm #{subdir}/*#{name}.webp" ; `#{cmd}` end
if File.size?(bpg) ; puts cmd = "rm #{subdir}/*#{name}.bpg" ; `#{cmd}` end
next
end
end
end
# footer image junk detection
footer_png = Dir["#{subdir}/*-#{name}_*.png"].first || "#{subdir}/#{prefix}-#{name}_.png"
fh, fphash = nil
if tmp = footer_png.match(/_(\d+)_(\d+)x(\d+)/)
fphash = Phash::ImageHash.new(tmp[1].to_i)
fh = tmp[3].to_i
else
fh = (200.0*w/800).round
`convert #{jpg} -crop #{w}x#{fh}+0+#{h-fh}\! #{footer_png}`
if fphash = Phash::Image.new(footer_png).phash
cmd = "mv #{footer_png} #{footer_png.sub(".png", "#{fphash.data}_#{w}x#{fh}.png")}"
`#{cmd}`
end
end
footer_junk_found = nil
if fphash
JUNK_FOOTER_PHASHES.each do |p, s|
sim = p.similarity(fphash)
if sim > 0.83
footer_junk_found = s
puts "Footer junk found #{sim}: #{jpg} => #{p.data}"
break
end
end
end
cmd = Dir["#{subdir}/*x*-#{name}.webp"]
if footer_junk_found
h -= ( footer_junk_found[1].to_f * w / footer_junk_found[0] ).round
`rm #{webp}` if File.exists?(webp)
`rm #{bpg}` if File.exists?(bpg)
webp = "#{subdir}/#{prefix}-#{w}x#{h}-#{name}.webp"
puts webp, "---\n"
cmd = cmd.select { |x| !x.include?("#{w}x#{h}") }
end
unless cmd.empty?
puts cmd = "rm #{cmd.join(" ")}"
`#{cmd}`
end
`rm #{bpg}` if File.exists?(bpg) # !!! REMOVE BPG IN-PREFER OF WEBP !!!
if !File.size?(webp) && !File.size?(bpg)
cmd = "convert #{jpg}"
cmd += " -crop #{w}x#{h}+0+0\!" if footer_junk_found
cmd += " -resize 680" if w > 700
cmd += " -quality 60 #{webp}"
puts cmd
`#{cmd}`
end
end # w > 200
img_src = img_url
img_src = "#{prefix}-#{name}.bpg" if File.size?(bpg)
img_src = webp.sub("#{subdir}/", "") if File.size?(webp)
f.puts "<img src=\"#{img_src}\" alt=\"page-#{i}\" origin=\"#{img_url}\"><br/>"
end # html scan
# raise "ERROR: this chap has only #{names.size} pages" if names.size <= 3
( Dir["#{subdir}/*.jpg"] +
Dir["#{subdir}/*.bpg"] +
Dir["#{subdir}/*.webp"] ).map { |name|
name.match(/([0-9a-f]+)\.(jpg|bpg|webp)$/)[1]
}.uniq.each do |name|
unless names.include?(name)
puts cmd = "rm #{subdir}/*#{name}.*"
`#{cmd}`
end
end
f.puts "<br/>#{nextchap_link}" if nextchap
end # finish building index.tmp
`mv #{subdir}/index.tmp #{subdir}/index.md`
end
def self.crawl(story, nextchap, start = nil, max = nil)
puts "ruby crawler.rb #{story} #{nextchap} #{start} #{max}"
base = "https://hamtruyen.com/doc-truyen"
dir = "./content/#{story}"
`mkdir #{dir}` unless File.exists?(dir)
start = (start || 1).to_i
max = (max || 9999).to_i
(start).upto(max) do |x|
break unless nextchap
subdir = "#{dir}/chapter-#{x}"
`mkdir #{subdir}` unless File.exists?(subdir)
url = "#{base}/#{nextchap}.html"
html_file = "#{subdir}/crawled.html"
puts "\n\n[#{subdir}]\n GET #{url} ..."
if File.exists?(html_file)
html = File.open(html_file, 'r').read
cached_url = html.match(/"og:url" content="([^"]+)/)[1]
puts "CACHED #{cached_url}"
raise "ERROR: cached html not matched" unless cached_url.include?(nextchap)
else
html = open(url).read
File.open(html_file, 'w').write(html)
end
m = html.match(/<option\s+value=['"]([^'"]+)['"].+?selected=/mi)
lastchap = m[1]
puts nextchap, lastchap
if lastchap == nextchap || start == max
puts "RE-GET lastchap ..."
html = open(url).read
# m = html.match(/<option\s+value=['"](.+?)['"].+?<option.+?selected=/mi)
m = html.match(/<option\s+value=['"]([^'"]+)['"].+?selected=/mi)
lastchap = m[1]
File.open(html_file, 'w').write(html) if nextchap != lastchap || start == max
puts nextchap, lastchap
end
if nextchap == lastchap
nextchap = nil
else
nextchap = m ? m[0].split(/value=['"]/i)[-2].match(/(.+?)['"]/)[1] : nil
end
parse(html, story, subdir, x, nextchap)
end # (start).upto(max)
end # def crawl
end # module Crawler