ここで得たalbum id を使って、そのアルバムに画像を追加したいとします。
bloggerに執筆するときにはそれらをひとまとまりにしたい
というのが良くある考え方だと思います。
その時、
get-direct-url-in-picasa.pyをつかって、picasa上にその画像の有無を調べます。
画像があれば、picasa上のファイルへのdirect linkが得られるはずです。
もしpicasa上にその画像が無いと判断したら、
upload-image-to-blogger-in-picasa.pyをつかって、
最初に得たalbum id を指定して、画像をアップロードします。
そしてその画像のpicasa上でのdirect linkをget-direct-url-in-picasa.pyで得ます。
まずはget-direct-url-in-picasa.py の内容です。
#!/usr/bin/python2.5
# return the direct link (URL) of picture in picasa.
import sys # for argv
import gdata.photos.service
import gdata.media
import gdata.geo
argvs = sys.argv
argc = len(argvs)
if (argc != 3 ):
print 'Usage: # python %s AlbumName FileName' % argvs[0]
quit()
albumname = argvs[1]
filename = argvs[2]
album_id="1234567890123456789" # the albm_id fo "Chemical-X" was given by get_albumid.py
gd_client = gdata.photos.service.PhotosService()
gd_client.email = 'changeme@gmail.com' # Set your Picasaweb e-mail address...
gd_client.password = 'secret word' # ... and password
gd_client.source = 'api-sample-google-com'
gd_client.ProgrammaticLogin()
album_url = '/data/feed/api/user/%s/albumid/%s' % ('default', album_id)
photos = gd_client.GetFeed('/data/feed/api/user/%s/albumid/%s?kind=photo' % ('default', album_id))
for photo in photos.entry:
if photo.title.text == filename :
print photo.content.src,
続いて、upload-image-to-blogger-in-picasa.py の内容です。
#!/usr/bin/python2.5
# return the direct link (URL) of picture in picasa.
import sys # for argv
import gdata.photos.service
import gdata.media
import gdata.geo
argvs = sys.argv
argc = len(argvs)
if (argc != 2 ):
print 'Usage: # python %s FileName' % argvs[0]
quit()
filename = argvs[1]
gd_client = gdata.photos.service.PhotosService()
gd_client.email = 'changeme@gmail.com' # Set your Picasaweb e-mail address...
gd_client.password = 'secret password' # ... and password
gd_client.source = 'upload-image-to-blogger-in-picasa'
gd_client.ProgrammaticLogin()
album_id="1234567890123456789" # the albm_id fo "Chemical-X" was given by get_albumid.py
album_url = '/data/feed/api/user/%s/albumid/%s' % ('default', album_id)
photo = gd_client.InsertPhotoSimple(album_url, filename, filename, filename, content_type='image/png')
print photo.content.src,
これで、画像がもともとpicasa上にあってもなくてもどちらの場合だったとしても、
処理後には、picasa上のurlを取得できたことになります。
それを使って、org2blogger.elでは、エクスポートしたhtml無いでの画像リンク先を変更するという作業を行います。
以下、org2blogger.el.
(setq get-url-script-name "pathto/org2blogger/get-direct-url-in-picasa.py")
(setq upload-image-script-name "pathto/elisp/org2blogger/upload-image-to-blogger-in-picasa.py")
(setq image-file-path "l:/0mine/org/images/")
(setq blog-name "Chemical-X")
(setq albumid "1234567890123456789") ; given by get_albumid.py for "Chemical-X")
(defun replace-links-with-upload-image-url ()
(interactive)
(while (re-search-forward "^<img src=\"images/\\(.*\.png\\)\" alt=\"\\(.*\.png\\)\" />" nil t)
(setq local-image-file-name (buffer-substring-no-properties (match-beginning 1) (match-end 1)))
;check the blog already has the image file or not.
(setq uploaded-img-url
(chomp (get-uploaded-img-url get-url-script-name blog-name local-image-file-name)))
(message "Match file name: %s" uploaded-img-url)
(if (string-equal uploaded-img-url "")
;if the blog does not have the image, upload the image firstly, and receive the URL.
;if the blog has the image, just receive the URL.
(progn
;upload image file-name
;get uploaded file name (direct URL) again.
(setq uploaded-img-url
(shell-command-to-string (concat upload-image-script-name " " image-file-path local-image-file-name)))
(message "uploaded-img-url:%s" uploaded-img-url)
; (setq uploaded-img-url
; (chomp (get-uploaded-img-url get-url-script-name blog-name local-image-file-name)))
)
)
;replace the line with htmlized-img-url string.
;Delete line
;The equivalent of ‘kill-line’ (‘C-k’) but without kill ring side effects:
(delete-region (line-beginning-position) (line-end-position))
(insert (htmlized-img-url uploaded-img-url))
)
; (message "matched string: %s" match-string)
)
(defun get-uploaded-img-url (script-name blog-name file-name)
(shell-command-to-string (concat script-name " "
blog-name " "
file-name)))
(defun chomp (str)
"Chomp leading and tailing whitespace from STR."
(let ((s (if (symbolp str) (symbol-name str) str)))
(replace-regexp-in-string "\\(^[[:space:]\n]*\\|[[:space:]\n]*$\\)" "" s)))
(defun htmlized-img-url (uploaded-img-url)
(concat "<a "
"onblur=\"try {parent.deselectBloggerImageGracefully();} catch(e) {}\" "
"href=\"" uploaded-img-url "\">"
"<img "
"style=\"display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px;\" "
"src=\"" uploaded-img-url "\" "
"border=\"0\" "
"alt=\"\" "
"/>"
"</a>"
)
)
置換し終わったhtmlをorg-googlecl.elのorg-googlecl-blog関数を使ってblogger.comへ投稿します。
org-modeで書いたテキスト内で、
M-x org-googlecl-blog
してみてください。
世界が変わって見えましたか?