いままで Twitter に画像を投稿する際には TwitPic を使っていたけどバックアップが大変なので、バックアップしやすそうな Lockerz に乗り換えてみた。
なので、バックアップ用スクリプトを Ruby で書いてみた。
#!/usr/bin/env ruby
# coding: utf-8
$KCODE='u'
require 'optparse'
require 'net/http'
require 'open-uri'
require "rexml/document"
Net::HTTP.version_1_2
# コマンド引数の取得
def get_params()
opts = {}
OptionParser.new {|opt|
opt.on('-f VAL', '--feedurl=VAL') {|v| opts[:feedurl] = v }
opt.on('-r VAL', '--repository=VAL') {|v| opts[:repository] = v }
opt.parse!(ARGV)
}
return opts
end
# RSSフィードにある画像ファイルの情報を取得
def get_items(feedurl)
result = []
uri = URI.parse(feedurl)
Net::HTTP.start(uri.host, uri.port){|http|
res = http.get(uri.request_uri)
doc = REXML::Document.new(res.body)
items = doc.get_elements('/rss/channel/item')
items.each{|item|
enclosure = item.elements['enclosure']
result << {
:link => item.text('link'),
:file => enclosure.attributes['url'],
:type => enclosure.attributes['type']
}
}
}
return result
end
# ダウンロードした画像ファイルの拡張子を決める
def get_ext(type)
case type
when 'image/jpeg'
return 'jpg'
when 'image/png'
return 'png'
else
raise 'illegal type'
end
end
# 画像ファイルをダウンロードして保存する
def save_file(url, filename)
open(filename, 'wb'){|file|
open(url){|data|
file.write(data.read)
}
}
end
# main
# コマンド引数の取得
params = get_params()
# バックアップ画像ファイル置場
repository_path = File.expand_path(params[:repository])
# RSSフィードにある画像ファイルの情報を取得
items = get_items(params[:feedurl])
# ひとつずつ画像を取得する
items.each{|item|
# http://lockerz.com/s/123456789 から 123456789 を抜き出す
basename = item[:link].split('/').last
ext = get_ext(item[:type])
filename = repository_path + '/' + basename + '.' + ext
if File.exist?(filename)
# ダウンロード済みならダウンロードしない
puts "The file #{filename} is exist."
else
# Lockerz Web API を使用してオリジナル画像を取得する場合のURL
url = "http://api.plixi.com/api/tpapi.svc/imagefromurl?url=#{item[:link]}&size=big"
# RSS フィードの enclosure@url を信じる場合のURL
#url = item[:file]
save_file(url, filename)
puts "Downloaded the file #{filename}."
# 連続アクセス負荷を与えないように2秒待つ
sleep 2
end
}
バックアップのスクリプトは自動で定期的に実行するのを想定。
実行結果例。
$ ls
132331476.jpg 220323042.jpg 220833896.jpg index.html
219839620.jpg 220323170.jpg 220856483.jpg
$ ruby ./lockerz_backup.rb --feedurl=http://api.plixi.com/api/tpapi.svc/rss/users/11432099/photos --repository=/home/hoge/lockerz
Downloaded the file /home/hoge/lockerz/221655695.jpg.
Downloaded the file /home/hoge/lockerz/221405823.jpg.
Downloaded the file /home/hoge/lockerz/221364228.jpg.
Downloaded the file /home/hoge/lockerz/221358386.jpg.
Downloaded the file /home/hoge/lockerz/221318771.jpg.
Downloaded the file /home/hoge/lockerz/221124701.jpg.
Downloaded the file /home/hoge/lockerz/221122984.jpg.
Downloaded the file /home/hoge/lockerz/221108619.jpg.
Downloaded the file /home/hoge/lockerz/221028027.jpg.
The file /home/hoge/lockerz/220856483.jpg is exist.
The file /home/hoge/lockerz/220833896.jpg is exist.
The file /home/hoge/lockerz/220323170.jpg is exist.
The file /home/hoge/lockerz/220323042.jpg is exist.
The file /home/hoge/lockerz/219839620.jpg is exist.
The file /home/hoge/lockerz/132331476.jpg is exist.
$ ls
132331476.jpg 220833896.jpg 221122984.jpg 221364228.jpg
219839620.jpg 220856483.jpg 221124701.jpg 221405823.jpg
220323042.jpg 221028027.jpg 221318771.jpg 221655695.jpg
220323170.jpg 221108619.jpg 221358386.jpg index.html
参考までに Lockerz の RSS フィードはこんな感じ。
<rss version="2.0" xmlns:a10="http://www.w3.org/2005/Atom">
<channel>
<title>Photos uploaded by Nick Labadie</title>
<description/>
<lastBuildDate>Mon, 02 Jul 2012 04:10:08 Z</lastBuildDate>
<item>
<guid isPermaLink="false">x2_d36328f</guid>
<link>http://lockerz.com/s/221655695</link>
<title>アルアビスのAランチ、パニーニにペッシェスパーダをチョイス。魚のフライだけどわりとくどくなくて食べやすい。</title>
<description><img src="http://c0014372.r32.cf1.rackcdn.com/x2_d36328f" alt="x2_d36328f" /></description>
<enclosure url="http://c0014369.r32.cf1.rackcdn.com/x2_d36328f" type="image/jpeg"/>
<a10:updated>2012-07-02T04:10:08Z</a10:updated>
</item>
<item>
<guid isPermaLink="false">x2_d32627f</guid>
<link>http://lockerz.com/s/221405823</link>
<title>おなかいぱーい(・∀・)</title>
<description><img src="http://c0014372.r32.cf1.rackcdn.com/x2_d32627f" alt="x2_d32627f" /></description>
<enclosure url="http://c0014369.r32.cf1.rackcdn.com/x2_d32627f" type="image/jpeg"/>
<a10:updated>2012-07-01T07:24:00Z</a10:updated>
</item>
-
Ref.
- Lockerz Help Desk : API Documentation
- Get User Feed : Lockerz Help Desk
- Image From URL : Lockerz Help Desk
- Lockerz の自分の写真ページ: Gallery | Lockerz
- Lockerz の自分の写真RSS: Photos uploaded by Nick Labadie
tags: ruby lockerz twitter
Posted by NI-Lab. (@nilab)