Skip to content

Instantly share code, notes, and snippets.

@labocho
Created September 5, 2014 02:02
Show Gist options
  • Select an option

  • Save labocho/b9cdf72e54439fccaaab to your computer and use it in GitHub Desktop.

Select an option

Save labocho/b9cdf72e54439fccaaab to your computer and use it in GitHub Desktop.
引数で指定したユーザの twitpic のメタデータと画像を ./twitpic に保存
#!/usr/bin/env ruby
gem "rest-client", "~> 1.6.6"
require "rest-client"
require "json"
require "time"
unless username = ARGV.first
STDERR.puts "Usage: backup-twitpic username"
exit 1
end
Dir.mkdir("twitpic") unless Dir.exists?("twitpic")
Dir.chdir("twitpic") do
user_json = RestClient.get("http://api.twitpic.com/2/users/show.json?username=#{username}")
File.write("user.json", user_json)
user = JSON.parse(user_json)
user["images"].each do |i|
timestamp = Time.parse(i["timestamp"]).strftime("%Y%m%d%H%M%S")
message_escaped = i["message"].gsub("/", "_")
filename = "#{timestamp}-#{i["short_id"]}-#{message_escaped}.#{i["type"]}"
url = "http://twitpic.com/show/full/#{i["short_id"]}"
STDERR.puts "Download #{url}"
File.write(filename, RestClient.get(url))
sleep 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment