Ruby + scrot + cURL + フォト蔵API で Gyazo ごっこ(Linux用)

切実な必要性に駆られたわけではなく、思いついたのでなんとなく脊髄反射的に作って見ました。

scrot、cURLbeep、xclip に依存しています(beep は必須ではありません)。

エラー処理とかはほとんどやってないです。

Windows 向けに修正したいという場合は scrot の代わりに boxcutter を使うのが良さげ。

関連: 2010-01-25 その場でスクリーンショットを撮って挿入するマクロ(OpenOffice.org writer) | anobota

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require "rubygems"
require "pit"
require "tempfile"


## config

SCROT       = "/usr/bin/scrot"
BEEP        = "/usr/bin/beep" # path or nil
CURL        = "/usr/bin/curl"
CLIP        = "/usr/bin/xclip"
WEB_BROWSER = "/usr/bin/firefox" # path or nil
WAIT_SEC    = 5

config = Pit.get("photozou.jp", :require => {
                   "username" => "your email in Photozou",
                   "user_id"  => "your user id in Photozou (http://photozou.jp/user/top/{user_id})",
                   "password" => "your password in Photozou"
                 })


## functions

def capture(filename)
  system %Q! #{SCROT} --select "#{filename}" !
end

def post(config, album_id, filename)
  cmd =  %Q! #{CURL} !
  cmd << %Q! -X POST --silent !
  cmd << %Q! --user #{config["username"]}:#{config["password"]} !
  cmd << %Q! -F "album_id=#{album_id}" !
  cmd << %Q! -F "photo=@#{filename};type=image/png" !
  cmd << %Q! -F "photo_title=%s" ! % Time.now.strftime("%Y-%m-%d %T")
  cmd << %Q! --url http://api.photozou.jp/rest/photo_add !
  `#{cmd}`
end

def photo_id(response)
  /<photo_id>(\d+)<\/photo_id>/ =~ response
  $1
end


## main

if ARGV.empty?
  puts <<EOB
Usage: ruby #{__FILE__} album_id
EOB
  exit 1
end

album_id = ARGV[0] # http://photozou.jp/photo/list/{user_id}/{album_id}
file = Tempfile.new( [File.basename(__FILE__) + "-", ".png"] )

sleep WAIT_SEC
system BEEP if BEEP

response = nil
begin
  capture(file.path)
  response = post(config, album_id, file.path)
  system "#{BEEP} -f 880" if BEEP
ensure
  file.close
  file.unlink
end

photo_url = "http://photozou.jp/photo/show/#{ config['user_id'] }/#{ photo_id(response) }"
system "echo #{photo_url} | #{CLIP} -sel clip"
system %Q! #{WEB_BROWSER} "#{photo_url}" ! if WEB_BROWSER
puts photo_id(response)
      • -

2011年 1月 5日 水曜日 22:06:29 JST