|
Revision 6184, 1.0 kB
(checked in by kawauchi, 4 years ago)
|
|
lang/ruby/misc/tumblr_getimages.rb: 進度表示は一箇所で行うよう変更
|
| Line | |
|---|
| 1 | # |
|---|
| 2 | # 指定したIDのTumblrの全画像をカレントディレクトリに保存する |
|---|
| 3 | # |
|---|
| 4 | |
|---|
| 5 | require 'open-uri' |
|---|
| 6 | require 'rexml/document' |
|---|
| 7 | |
|---|
| 8 | if ARGV.size != 1 |
|---|
| 9 | $stderr.puts "Usage: #{$0} id " |
|---|
| 10 | exit 64 |
|---|
| 11 | end |
|---|
| 12 | |
|---|
| 13 | id = ARGV[0] |
|---|
| 14 | |
|---|
| 15 | api = URI.parse("http://#{id}.tumblr.com/api/read") |
|---|
| 16 | api.query = "type=photo" |
|---|
| 17 | doc = REXML::Document.new(api.read) |
|---|
| 18 | total = REXML::XPath.first(doc, "/tumblr/posts").attributes['total'].to_i |
|---|
| 19 | |
|---|
| 20 | $stdout.sync = true |
|---|
| 21 | |
|---|
| 22 | num = 50 |
|---|
| 23 | index = 0 |
|---|
| 24 | while index < total |
|---|
| 25 | num = [num, total - index].min |
|---|
| 26 | api.query = "type=photo&num=#{num}&start=#{index}" |
|---|
| 27 | doc = REXML::Document.new(api.read) |
|---|
| 28 | posts = REXML::XPath.match(doc, "/tumblr/posts/post") |
|---|
| 29 | photo_list = posts.map {|post| URI.parse(post.elements['photo-url'].text)} |
|---|
| 30 | limit = index + num |
|---|
| 31 | while index < limit |
|---|
| 32 | print "\r#{index + 1}/#{total}" |
|---|
| 33 | begin |
|---|
| 34 | photo_url = photo_list.shift |
|---|
| 35 | open(photo_url.path.split(/\//).last, "wb") do |image_file| |
|---|
| 36 | image_file.write(photo_url.read) |
|---|
| 37 | end |
|---|
| 38 | rescue |
|---|
| 39 | $stderr.puts "Error: #{photo_url}" |
|---|
| 40 | end |
|---|
| 41 | index += 1 |
|---|
| 42 | end |
|---|
| 43 | end |
|---|
| 44 | puts |
|---|
| 45 | |
|---|