root/lang/ruby/misc/tumblr_getimages.rb

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
5require 'open-uri'
6require 'rexml/document'
7
8if ARGV.size != 1
9  $stderr.puts "Usage: #{$0} id "
10  exit 64
11end
12
13id = ARGV[0]
14
15api = URI.parse("http://#{id}.tumblr.com/api/read")
16api.query = "type=photo"
17doc = REXML::Document.new(api.read)
18total = REXML::XPath.first(doc, "/tumblr/posts").attributes['total'].to_i
19
20$stdout.sync = true
21
22num = 50
23index = 0
24while 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
43end
44puts
45
Note: See TracBrowser for help on using the browser.