This is my first Ruby script to fetch photos from TaipeiLink.
#!/usr/local/bin/ruby require "net/http"if ARGV.length == 0 puts "tplink.rb <account>" exit end
ARGV.delete("tplink.rb") ARGV.each { |account| puts "Fetching photos from #{account}..." Dir.mkdir(account) if !FileTest.exist?(account) Net::HTTP.start("photo.taipeilink.net", 80) { |http| http.get("/#{account}").body.scan(/<a href="http://photo.taipeilink.net/#{account}?st=album&pg=(d*)"/) { |pg| Dir.mkdir("#{account}/#{pg}") if !FileTest.exist?("#{account}/#{pg}") http.get("/#{account}?st=album&pg=#{pg}").body.scan(/<a href="http://photo.taipeilink.net/#{account}/(d*-d*-d*-d*)?m=0&pg=(d*)"/) { |fileName, pg| url = "/#{account}/#{fileName}?m=1&pg=#{pg}" response = http.head(url) if response["content-type"] == "image/gif" fileName += ".gif" else fileName += ".jpg" end fileName = "#{account}/#{pg}/#{fileName}" if FileTest.exist?(fileName) puts "Skip #{fileName}" else puts "Fetch #{fileName}" response = http.get(url) file = File.new(fileName, "w") file.write(response.body) file.close() end } } } }