Skip to content

Instantly share code, notes, and snippets.

@guitarmasaki
Created December 14, 2012 05:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarmasaki/4282916 to your computer and use it in GitHub Desktop.
Save guitarmasaki/4282916 to your computer and use it in GitHub Desktop.
PQI Air CardからRubyでFlickrにポストするスクリプト。大部分はひとりぶろぐさんのデジカメ内部でRubyを動かす狂気!無線LAN内蔵SDカードアダプタPQI Air Cardの間違った使い方 http://hitoriblog.com/?p=12627を流用させていただいてます。
#!/usr/arm-linux/bin/ruby
# -*- coding: utf-8 -*-
require 'net/http'
require 'uri'
#require 'pp'
#require 'rubygems'
require 'flickraw'
#load('/mnt/sd/config.rb')
# ------------------------------
# user configuration
# ------------------------------
#AUTPRINT_MRK = "/mnt/sd/MISC/AUTPRINT.MRK"
#FTP_SERVER = "your_ftp_server"
#FTP_USER = "your_ftp_login"
#FTP_PASSWORD = "your_ftp_password"
#FTP_PATH = "your_ftp_dir"
# http://stewgate.appspot.com/sg1/
#STEW_GATE_TOKEN = "your_stew_gate_token"
#MENTION_TO = "@your_twitter_id"
# flickr for flickraw
#FlickRaw.api_key="your_api_key"
#FlickRaw.shared_secret="your_secret"
#flickr.access_token = "your_token"
#flickr.access_secret = "your_access_secret"
# ------------------------------
# constants
# ------------------------------
STEW_GATE_API = "http://stewgate.appspot.com/sg1/post/"
LINE_BREAK = "\r\n"
DELIMITER = "#{LINE_BREAK}#{LINE_BREAK}"
# ------------------------------
# methods
# ------------------------------
def generate_signature()
a = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
return " " + (
Array.new(4) do
a[rand(a.size)]
end
).join
end
def post_escape( string )
string.gsub(/([^ a-zA-Z0-9_.-]+)/) do
'%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
end.tr(' ', '+')
end
def post(msg)
params = {
"_t" => STEW_GATE_TOKEN,
"msg" => "#{MENTION_TO} " + msg + generate_signature,
}
begin
uri = URI::parse( STEW_GATE_API )
http = Net::HTTP.new( uri.host, uri.port )
request = Net::HTTP::Post.new( uri.request_uri )
request.content_type = "application/x-www-form-urlencoded"
query = params.map do |key, val| "#{key}=#{post_escape(val.to_s)}" end
res = http.request( request, query.join( '&' ) )
puts "Response: #{res.code}"
rescue
post("Error while Stewing: #{$!}")
end
end
def post_flickr(file)
login = flickr.test.login
res = flickr.upload_photo file,
:title => File.basename(file),
:description => "via PQI Air Card",
:is_public => "0",
:is_friend => "0",
:is_family => "0"
info = flickr.photos.getInfo(:photo_id => res)
url = FlickRaw.url_short(info) #短縮URLを返す。他にも画像サイズ指定など可能。
end
# ------------------------------
# main
# ------------------------------
# AUTPRINT.MRKは存在するか?
if !File.exists?(AUTPRINT_MRK)
# 無ければ終了
exit
else
begin
# MRKファイルの読み込み
MRK_FILE = File.read(AUTPRINT_MRK)
# RICOH用にCRLFを追加
if MRK_FILE.match("RICOH")
MRK_FILE.gsub!("\[JOB\]", "#{LINE_BREAK}\[JOB\]")
end
# あれば読み込みして改行二つでセクションごとにsplit
sections = MRK_FILE.split(DELIMITER)
sections_shadow = sections.dup
# セクションを走査
sections.each do |section|
# セクションの中身を改行でsplit
lines = section.split("{LINE_BREAK}")
# 最初の行が[JOB]だったら
if lines[0].match(/\[JOB\]/)
# 1行ごとに走査
lines.each do |l|
# 画像パスの行だったら
if md = l.match(/^<IMG SRC = "(.+)">/)
# ファイルパスをSDのマウントポイントからのフルパスにして取り出す
fname = md[1].sub("..", "/mnt/sd")
# フルパスの場所に画像が存在したら
if File.exists?(fname)
# Flickrに画像をアップロード
begin
flickr_res = post_flickr(fname)
post("#{File.basename(fname)} uploaded to #{flickr_res} .")
rescue
post("Error while Flickr put: #{$!}")
end
end
# 正常に終了したらこのJOBを削除
sections_shadow.delete(section)
# 終了したJOBを削除した状態でまだJOBが残っていたら
# JOBを一つ削除した形でAUTPRINT.MRKを書き込み
if (sections_shadow.length > 1)
File.write(AUTPRINT_MRK, sections_shadow.join(DELIMITER))
else
# 削除してJOBが残っていなかったらAUTPRINT.MRKを削除
File.unlink(AUTPRINT_MRK)
post("All uploads done.")
exit
end
end
end
end
end
rescue
post("Error while processing: #{$!}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment