up2pb は、無料画像ホスティングサービス PhotoBucket の自分のアルバムに画像を投稿するためのスクリプトである。
PhotoBucket の無料画像ホスティングサービスは、次のような特徴がある。
本スクリプトにより、1) cron で定期的に自動ログインししてアカウント抹消を予防したり、2) ftp 同様にバッチ的に大量のファイルをアプロードしたり、3) emacs 上から、画面をキャプチャして、それを PhotoBucket にアプロードして、アプロード先の URL を image タグにしてカレントバッファに貼り付ける、とかいったことが可能になり、PhotoBucket の使い勝手が大幅に改善する。
画像貼付に制限が厳しい はてなダイアリー に気軽に多量の画像を、コストを気にせずに貼るためにこれを書いた。hatena-mode.el と組み合わせて使っている。
% up2pb -h
上述のように、PhotoBucket は、原則として、90日ログインのないアカウントを抹消するので、 crontab なんかで、下のコマンドを定期的に実行することを推奨。
% up2pb -s Id,Pass
僕は crontab に次のように書いている
# m h dom mon dow command # min hour day month day-of-week command #(0-59) (0-23) (1-31) (1-12) (0-6) #------------------------------------------------------------ 01 4 3 * 0 /home/amt/bin/up2pb -s Id,Pass
通常(-s オプションを付けなければ) 投稿画像への URL を出力する。
% up2pb Id,Pass http://i94.photobucket.com/albums/l110/itsumi3/tmpozawa-1.jpg % up2pb -a SomeAlbum Id,Pass http://i94.photobucket.com/albums/l110/itsumi3/SomeAlbum/tmpozawa-1.jpg
% up2pb Id,Pass SomeImage.jpg % up2pb -a SomeAlbum Id,Pass AnotherImage.jpg
% up2pb -a hatena -d ~/.hatena/image Id,Pass http://SomeHost/image.jpg
僕は、~/.emacs に下のように書いて、muse.el、hatena-mode.el、clmemo.el などに組み込んで使っている。
;;; 20060926 hatena-mode などへの へイメージタグ張り込み
;;; html の画像タグ <img src="URL">
;;; はてなの画像タグ [URL:image]
;;; clmemo の画像タグ (url: URL)
;;; muse の 画像タグ [[URL]]
(defconst photobucket-idpass "YourID,Pass")
(defconst hatena-image-dir "~/.hatena/image")
(defconst clmemo-image-dir "~/personal/memo")
(defun hatena-upload-and-insert-hatenaimagetag-from-file (img)
"入力画像ファイルを photobucket にアプロードし、それを指すhatenaタグを挿入する"
(interactive "fEnter Image file: ")
(unless (file-exists-p hatena-image-dir) ; image-dir が無ければ掘る
(call-process "mkdir" nil nil nil "-p" hatena-image-dir))
(insert
(concat "["
(shell-command-to-string
(concat "~/bin/up2pb -n -a hatena -d " hatena-image-dir " "
photobucket-idpass " " img))
":image]")))
(defun hatena-upload-and-insert-hatenaimagetag-from-url (url)
"画像を指すURLを photobucket にアプロードし、それを指すhatenaタグを挿入する"
(interactive "sEnter URL: ")
(unless (file-exists-p hatena-image-dir) ; image-dir が無ければ掘る
(call-process "mkdir" nil nil nil "-p" hatena-image-dir))
(insert
(concat "["
(shell-command-to-string
(concat "~/bin/up2pb -n -a hatena -d " hatena-image-dir " "
photobucket-idpass " " url))
":image]")))
(define-key hatena-mode-map [(control ?c)(shift ?p) ?f]
'hatena-upload-and-insert-hatenaimagetag-from-file)
(define-key hatena-mode-map [(control ?c)(shift ?p) ?u]
'hatena-upload-and-insert-hatenaimagetag-from-url)
(defun clmemo-upload-and-insert-hatenaimagetag-from-file (img)
"入力画像ファイルを photobucket にアプロードし、それを指すclmemoタグを挿入する"
(interactive "fEnter Image file: ")
(unless (file-exists-p clmemo-image-dir) ; image-dir が無ければ掘る
(call-process "mkdir" nil nil nil "-p" clmemo-image-dir))
(insert
(concat "(url: "
(shell-command-to-string
(concat "~/bin/up2pb -n -a clmemo -d " clmemo-image-dir " "
photobucket-idpass " " img))
")")))
(defun clmemo-upload-and-insert-hatenaimagetag-from-url (url)
"画像を指す入力URLを photobucket にアプロードし、それを指すclmemoタグを挿入する"
(interactive "sEnter URL: ")
(unless (file-exists-p clmemo-image-dir) ; image-dir が無ければ掘る
(call-process "mkdir" nil nil nil "-p" clmemo-image-dir))
(insert
(concat "(url: "
(shell-command-to-string
(concat "~/bin/up2pb -n -a clmemo -d " clmemo-image-dir " "
photobucket-idpass " " url))
")")))
(add-hook 'clmemo-mode-hook
'(lambda () (define-key clmemo-mode-map "\C-c\C-g" 'clgrep)
(define-key clmemo-mode-map [(control ?c)(shift ?p) ?f]
'clmemo-upload-and-insert-hatenaimagetag-from-file)
(define-key clmemo-mode-map [(control ?c)(shift ?p) ?u]
'clmemo-upload-and-insert-hatenaimagetag-from-url)
))
;変数 muse-current-project にカレントプロジェクトの値が下の形式で格納されている
; ("OpenNote"
; ("~/Pages/OpenNote" :default "index")
; (:base "html" :path "~/public_html/muse/OpenNote"))
;
; だから muse 側のパスは (caadr muse-current-project)
; publish 後のパスは (cadddr (caddr muse-current-project))
; photpbucket のバックアップは muse 側に置くものとする
(defun muse-upload-and-insert-hatenaimagetag-from-file (img)
"入力画像ファイルを photobucket にアプロードし、それを指すmuseタグを挿入する"
(interactive "fEnter Image file: ")
(let ((prj (car muse-current-project))
(image-dir (concat (caadr muse-current-project) "/image")))
(unless (file-exists-p image-dir) ; image-dir が無ければ掘る
(call-process "mkdir" nil nil nil "-p" image-dir))
(insert
(concat "[["
(shell-command-to-string
(concat "~/bin/up2pb -n -a " prj " -d " image-dir " "
photobucket-idpass " " img))
"]]"))
)
)
(defun muse-upload-and-insert-hatenaimagetag-from-url (url)
"画像を指す入力URLを photobucket にアプロードし、それを指すmuseタグを挿入する"
(interactive "sEnter URL: ")
(let ((prj (car muse-current-project))
(image-dir (concat (caadr muse-current-project) "/image")))
(unless (file-exists-p image-dir) ; image-dir が無ければ掘る
(call-process "mkdir" nil nil nil "-p" image-dir))
(insert
(concat "[["
(shell-command-to-string
(concat "~/bin/up2pb -n -a " prj " -d " image-dir " "
photobucket-idpass " " url))
"]]"))
)
)
(define-key muse-mode-map [(control ?c)(shift ?p) ?f]
'muse-upload-and-insert-hatenaimagetag-from-file)
(define-key muse-mode-map [(control ?c)(shift ?p) ?u]
'muse-upload-and-insert-hatenaimagetag-from-url)
複数画像を一度に投稿するモードを作ろうかと思ったけれど、僕は必要としないので止めておいた。そういう必要のあるヒトは shellscript でループして欲しい。
-c オプションで付けるコメントにマルチバイト文字を使うと文字化けする。
