2011年6月2日木曜日

CL で Livedoor Blog へ投稿


このエントリーをはてなブックマークに追加


変数にID、API Key、自分のブログ用AtomPubエンドポイントを設定すれば使える(はず


(defvar *lduser1* "xxxxxx")
(defvar *ldkey1*  "xxxxxx")
(defvar *ldurl1*  "http://livedoor.blogcms.jp/atom/blog/xxxxxxx/article")

(setf *drakma-default-external-format* :utf-8)
(pushnew (cons "application" "atom+xml") *text-content-types* :test #'equal)

(defun make-x-wsse-header (user pass)
  "wsse認証用ヘッダを生成"
  (let* ((created (multiple-value-bind (sec min hr day mon year dow daylight-p zone)
       (decode-universal-time (get-universal-time))
     (declare (ignore daylight-p zone))
     (format nil "~4,'0d-~2,'0d-~2,'0dT~2,'0d:~2,'0d:~2,'0dZ" year mon day hr min sec)))
   (nonce   (cl-base64:usb8-array-to-base64-string
       (ironclad:digest-sequence :sha1 (ironclad:ascii-string-to-byte-array (kmrcl:random-string :length 40)))))
   (digest  (cl-base64:usb8-array-to-base64-string
       (ironclad:digest-sequence :sha1 (ironclad:ascii-string-to-byte-array 
             (concatenate 'string nonce created pass))))))
 (concatenate 'string
     "UsernameToken "
     "Username=\""   user "\", "
     "PasswordDigest=\"" digest  "\", "
     "Nonce=\""    nonce "\", "
     "Created=\""   created "\"")))

(defun post-ld-atompub (url user pass xml)
  "Livedoor Blogへ記事を投稿"
  (http-request url
    :content-type  "application/atom+xml"
    :method :post
    :content xml
    :content-length (length (jp:encode xml :utf-8))
    :additional-headers (list (cons "X-WSSE"  (make-x-wsse-header user pass)))))

(defun make-post-xml (title category main-a main-b)
  (concatenate 'string
      "<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:app=\"http://www.w3.org/2007/app\" xmlns:blogcms=\"http://blogcms.jp/-/spec/atompub/1.0/\">"
      "<title>" title "</title>"
      "<content type=\"text/html\" xml:lang=\"ja\"></content>"
      "<category scheme=\"http://livedoor.blogcms.jp/blog/techit2ch/category\" term=\""
      category
      "\" />"
      "<blogcms:source>"
      "<blogcms:body><![CDATA["
      main-a
      "]]></blogcms:body>"
      "<blogcms:more><![CDATA["
      main-b
      "]]></blogcms:more>"
      "</blogcms:source>"
      "</entry>"))

0 件のコメント:

コメントを投稿