-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiscuz.rb
More file actions
69 lines (55 loc) · 1.96 KB
/
Copy pathdiscuz.rb
File metadata and controls
69 lines (55 loc) · 1.96 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
# encoding: UTF-8
require_relative "./helper"
require "nokogiri"
logger = Logger.new(STDOUT)
DEBUG = true
download_interval = 3
xiangceUrl = ARGV[0]
# Get topic ID
if xiangceUrl =~ /([^\/]*)\/thread-(\d+)-\d+-(\d+)\.html/
siteURL = Regexp.last_match[1]
topicID = Regexp.last_match[2]
magicID = Regexp.last_match[3]
logger.info("Got topic ID : #{topicID}") if DEBUG
else
logger.error("Could not get topic ID, exiting...")
exit
end
# Get total page number
page = Nokogiri::HTML(open(tidy_url(xiangceUrl)))
if page.css("div#pgt a.last").text =~ /(\d+)/
topicPageNumber = Regexp.last_match[1].to_i
logger.info("Found #{topicPageNumber} pages in current topic")
else
logger.error("Could not get topic pages number, exiting...")
end
# Get topic name and create directory
topicName = page.css("a#thread_subject").text
if Dir.exist?(topicName)
logger.info("Using existing local directory [#{topicName}] to store photos")
else
Dir.mkdir(topicName)
logger.info("Creating local directory [#{topicName}] to store photos")
end
photo_folder = topicName + "/photos"
Dir.mkdir(photo_folder) unless Dir.exist?(photo_folder)
# Save album Url to file
File.open(topicName + "/topicURL.txt", "w") do |f|
f << xiangceUrl
end
# Process each topic page
(1 .. topicPageNumber).each do |page_number|
page_url = siteURL + "/thread-#{topicID}-#{page_number.to_s}-#{magicID}.html"
logger.info("Process page #{page_number} : #{page_url}")
page = Nokogiri::HTML(open(tidy_url(page_url)))
page.css("div.t_fsz img.zoom").each do |photo|
next if photo['zoomfile'] == nil
photo_src = siteURL + '/' + photo['zoomfile']
photo_filename = photo_src.split("/").last
logger.info("Saving #{photo_src}")
unless save_photo(photo_folder + "/#{photo_filename}", tidy_url(photo_src))
logger.error("Failed to save #{photo_filename}!")
end
sleep download_interval
end
end