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
|
# File 'lib/iloveptt.rb', line 6
def self.download(url)
page_uri = URI.parse(url)
agent = Mechanize.new
page = agent.get(page_uri)
title = page.title.split(" - ")[0]
post_id = page.uri.path.split("/").last.gsub('.html', '')
puts "Parsing #{title}"
image_urls = page.links.map(&:to_s).select{ |url| %(jpg png).include? url.split('.').last }
target_folder_path = File.expand_path("~/Pictures/iloveptt/#{post_id} - #{title}")
FileUtils.mkdir_p(target_folder_path) unless Dir.exists?(target_folder_path)
image_urls.each do |image_url|
image = agent.get(image_url)
filename = File.expand_path(File.basename(image.uri.path), target_folder_path)
File.delete(filename) if File.exists?(filename)
image.save(filename)
end
puts "Done!"
end
|