* mixi.el (mixi-news-title-regexp): Follow the change of mixi.
[elisp/mixi.git] / mixi.el
diff --git a/mixi.el b/mixi.el
index ed00996..dac2570 100644 (file)
--- a/mixi.el
+++ b/mixi.el
 ;; at run-time.
 (eval-when-compile
   (defvar w3m-use-cookies)
+  (defvar url-request-method)
+  (defvar url-request-data)
+  (defvar url-request-extra-headers)
   (autoload 'w3m-decode-buffer "w3m")
-  (autoload 'w3m-retrieve "w3m"))
+  (autoload 'w3m-retrieve "w3m")
+  (autoload 'url-retrieve-synchronously "url"))
 
 (defgroup mixi nil
   "API library for accessing to mixi."
@@ -220,6 +224,7 @@ Increase this value when unexpected error frequently occurs."
   :type 'boolean
   :group 'mixi)
 
+(defvar mixi-temp-buffer-name " *mixi temp*")
 (defvar mixi-me nil)
 
 ;; Utilities.
@@ -411,44 +416,50 @@ Increase this value when unexpected error frequently occurs."
   (mixi-retrieve "/logout.pl"))
 
 (defmacro with-mixi-retrieve (url &rest body)
-  `(when ,url
-     (with-temp-buffer
+  `(with-current-buffer (get-buffer-create mixi-temp-buffer-name)
+     (when ,url
+       (erase-buffer)
        (insert (mixi-retrieve ,url))
        (goto-char (point-min))
        (when (search-forward
              "<form action=\"login.pl\" method=\"post\">" nil t)
         (mixi-login)
         (erase-buffer)
-        (insert (mixi-retrieve ,url))
-        (goto-char (point-min)))
-       ,@body)))
+        (insert (mixi-retrieve ,url))))
+     (goto-char (point-min))
+     ,@body))
 (put 'with-mixi-retrieve 'lisp-indent-function 'defun)
 (put 'with-mixi-retrieve 'edebug-form-spec '(body))
 
 (defmacro with-mixi-post-form (url fields &rest body)
-  `(when ,url
-     (with-temp-buffer
+  `(with-current-buffer (get-buffer-create mixi-temp-buffer-name)
+     (when ,url
+       (erase-buffer)
        (insert (mixi-post-form ,url ,fields))
        (goto-char (point-min))
        (when (search-forward
              "<form action=\"login.pl\" method=\"post\">" nil t)
         (mixi-login)
         (erase-buffer)
-        (insert (mixi-post-form ,url ,fields))
-        (goto-char (point-min)))
-       ,@body)))
+        (insert (mixi-post-form ,url ,fields))))
+     (goto-char (point-min))
+     ,@body))
 (put 'with-mixi-post-form 'lisp-indent-function 'defun)
 (put 'with-mixi-post-form 'edebug-form-spec '(body))
 
-(defun mixi-get-matched-items (url regexp &optional range)
+(defun mixi-get-matched-items (url regexp &optional range reverse)
   "Get matched items to REGEXP in URL."
   (let ((page 1)
        ids)
     (catch 'end
       (while (or (null range) (< (length ids) range))
-       (with-mixi-retrieve (format url page)
-         (let (found)
-           (while (and (re-search-forward regexp nil t)
+       (with-mixi-retrieve (when url (format url page))
+         (let ((func (if reverse (progn
+                                   (goto-char (point-max))
+                                   're-search-backward)
+                       're-search-forward))
+               found)
+           (while (and (funcall func regexp nil t)
                        (or (null range) (< (length ids) range)))
              (let ((num 1)
                    list)
@@ -761,10 +772,10 @@ Increase this value when unexpected error frequently occurs."
   `(eq (mixi-object-class ,friend) 'mixi-friend))
 
 (defmacro mixi-friend-page (friend)
-  `(concat "/show_friend.pl?id=" (mixi-friend-id ,friend)))
+  `(concat "/show_profile.pl?id=" (mixi-friend-id ,friend)))
 
 (defconst mixi-friend-nick-regexp
-  "<img alt=\"\\*\" src=\"http://img\\.mixi\\.jp/img/dot0\\.gif\" width=\"1\" height=\"5\"><br>\r?
+  "<img \\(alt=\"\\*\" \\)?src=\"?http://img\\.mixi\\.jp/img/dot0\\.gif\"? \\(width\\|WIDTH\\)=\"?1\"? \\(height\\|HEIGHT\\)=\"?5\"?><br>\r?
 \\(.*\\)¤µ¤ó([0-9]+)")
 (defconst mixi-friend-name-regexp
   "<td BGCOLOR=#F2DDB7 WIDTH=80 NOWRAP><font COLOR=#996600>̾\\(&nbsp;\\| \\)Á°</font></td>
@@ -797,47 +808,35 @@ Increase this value when unexpected error frequently occurs."
   "Realize a FRIEND."
   ;; FIXME: Check a expiration of cache?
   (unless (mixi-object-realized-p friend)
-    (let (buf)
-      (with-mixi-retrieve (mixi-friend-page friend)
-       (setq buf (buffer-string)))
-      (if (string-match mixi-friend-nick-regexp buf)
-         (mixi-friend-set-nick friend (match-string 1 buf))
+    (with-mixi-retrieve (mixi-friend-page friend)
+      (if (re-search-forward mixi-friend-nick-regexp nil t)
+         (mixi-friend-set-nick friend (match-string 4))
        (mixi-realization-error 'cannot-find-nick friend))
-      ;; For getting my profile.
-      (unless (string-match mixi-friend-name-regexp buf)
-       (with-mixi-retrieve (concat "/show_profile.pl?id="
-                                   (mixi-friend-id friend))
-         (setq buf (buffer-string))))
-      (if (string-match mixi-friend-name-regexp buf)
-           (mixi-friend-set-name friend (match-string 2 buf))
-       (mixi-realization-error 'cannot-find-name friend))
-      (if (string-match mixi-friend-sex-regexp buf)
-         (mixi-friend-set-sex friend
-                              (if (string= (match-string 3 buf) "ÃË")
-                                  'male 'female))
-       (mixi-realization-error 'cannot-find-sex friend))
-      (when (string-match mixi-friend-address-regexp buf)
-       (mixi-friend-set-address friend (match-string 1 buf)))
-      (when (string-match mixi-friend-age-regexp buf)
-       (mixi-friend-set-age
-        friend (string-to-number (match-string 2 buf))))
-      (when (string-match mixi-friend-birthday-regexp buf)
-       (mixi-friend-set-birthday
-        friend (list (string-to-number (match-string 1 buf))
-                     (string-to-number (match-string 2 buf)))))
-      (when (string-match mixi-friend-blood-type-regexp buf)
-       (mixi-friend-set-blood-type friend (intern (match-string 1 buf))))
-      (when (string-match mixi-friend-birthplace-regexp buf)
-       (mixi-friend-set-birthplace friend (match-string 1 buf)))
-      (when (string-match mixi-friend-hobby-regexp buf)
-       (mixi-friend-set-hobby
-        friend (split-string (match-string 2 buf) ", ")))
-      (when (string-match mixi-friend-job-regexp buf)
-       (mixi-friend-set-job friend (match-string 2 buf)))
-      (when (string-match mixi-friend-organization-regexp buf)
-       (mixi-friend-set-organization friend (match-string 2 buf)))
-      (when (string-match mixi-friend-profile-regexp buf)
-       (mixi-friend-set-profile friend (match-string 1 buf))))
+      (when (re-search-forward mixi-friend-name-regexp nil t)
+       (mixi-friend-set-name friend (match-string 2)))
+      (when (re-search-forward mixi-friend-sex-regexp nil t)
+       (mixi-friend-set-sex friend (if (string= (match-string 3) "ÃË")
+                                       'male 'female)))
+      (when (re-search-forward mixi-friend-address-regexp nil t)
+       (mixi-friend-set-address friend (match-string 1)))
+      (when (re-search-forward mixi-friend-age-regexp nil t)
+       (mixi-friend-set-age friend (string-to-number (match-string 2))))
+      (when (re-search-forward mixi-friend-birthday-regexp nil t)
+       (mixi-friend-set-birthday friend
+                                 (list (string-to-number (match-string 1))
+                                       (string-to-number (match-string 2)))))
+      (when (re-search-forward mixi-friend-blood-type-regexp nil t)
+       (mixi-friend-set-blood-type friend (intern (match-string 1))))
+      (when (re-search-forward mixi-friend-birthplace-regexp nil t)
+       (mixi-friend-set-birthplace friend (match-string 1)))
+      (when (re-search-forward mixi-friend-hobby-regexp nil t)
+       (mixi-friend-set-hobby friend (split-string (match-string 2) ", ")))
+      (when (re-search-forward mixi-friend-job-regexp nil t)
+       (mixi-friend-set-job friend (match-string 2)))
+      (when (re-search-forward mixi-friend-organization-regexp nil t)
+       (mixi-friend-set-organization friend (match-string 2)))
+      (when (re-search-forward mixi-friend-profile-regexp nil t)
+       (mixi-friend-set-profile friend (match-string 1))))
     (mixi-object-touch friend)))
 
 (defun mixi-friend-id (friend)
@@ -1143,15 +1142,14 @@ Increase this value when unexpected error frequently occurs."
 (defconst mixi-diary-content-regexp
   "<td \\(class\\|CLASS\\)=\"?h12\"?>\\(.*\\)</td>")
 
-(defun mixi-realize-diary (diary)
+(defun mixi-realize-diary (diary &optional page)
   "Realize a DIARY."
   ;; FIXME: Check a expiration of cache?
   (unless (mixi-object-realized-p diary)
-    (with-mixi-retrieve (mixi-diary-page diary)
+    (with-mixi-retrieve (or page (mixi-diary-page diary))
       (unless (re-search-forward mixi-diary-closed-regexp nil t)
        (if (re-search-forward mixi-diary-owner-nick-regexp nil t)
-           (mixi-friend-set-nick (mixi-diary-owner diary)
-                                 (match-string 1))
+           (mixi-friend-set-nick (mixi-diary-owner diary) (match-string 1))
          (mixi-realization-error 'cannot-find-owner-nick diary))
        (if (re-search-forward mixi-diary-time-regexp nil t)
            (mixi-diary-set-time
@@ -1400,7 +1398,7 @@ Increase this value when unexpected error frequently occurs."
   "<td WIDTH=345>\\(.*\\)</td></tr>")
 (defconst mixi-community-birthday-regexp
   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>³«ÀßÆü</font></td>\r
-<td WIDTH=345>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü</td>")
+<td WIDTH=345>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü")
 ;; FIXME: Care when the owner has seceded.
 (defconst mixi-community-owner-regexp
   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>´ÉÍý¿Í</font></td>\r
@@ -1435,24 +1433,23 @@ Increase this value when unexpected error frequently occurs."
          (mixi-realization-error 'cannot-find-name community))
        (if (re-search-forward mixi-community-birthday-regexp nil t)
            (mixi-community-set-birthday
-            community
-            (encode-time 0 0 0 (string-to-number (match-string 3))
-                         (string-to-number (match-string 2))
-                         (string-to-number (match-string 1))))
+            community (encode-time 0 0 0 (string-to-number (match-string 3))
+                                   (string-to-number (match-string 2))
+                                   (string-to-number (match-string 1))))
          (mixi-realization-error 'cannot-find-birthday community))
        (if (re-search-forward mixi-community-owner-regexp nil t)
            (if (string= (match-string 1) "home.pl")
                (mixi-community-set-owner community (mixi-make-me))
-             (mixi-community-set-owner
-              community (mixi-make-friend (match-string 2)
-                                          (match-string 3))))
+             (mixi-community-set-owner community
+                                       (mixi-make-friend (match-string 2)
+                                                         (match-string 3))))
          (mixi-realization-error 'cannot-find-owner community))
        (if (re-search-forward mixi-community-category-regexp nil t)
            (mixi-community-set-category community (match-string 1))
          (mixi-realization-error 'cannot-find-category community))
        (if (re-search-forward mixi-community-members-regexp nil t)
-           (mixi-community-set-members
-            community (string-to-number (match-string 1)))
+           (mixi-community-set-members community
+                                       (string-to-number (match-string 1)))
          (mixi-realization-error 'cannot-find-members community))
        (if (re-search-forward mixi-community-open-level-regexp nil t)
            (mixi-community-set-open-level community (match-string 1))
@@ -1670,11 +1667,11 @@ Increase this value when unexpected error frequently occurs."
 (defconst mixi-topic-content-regexp
   "<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\"><tr><td class=\"h120\"><table><tr>\\(<td width=\"130\" height=\"140\" align=\"center\" valign=\"middle\"><a href=\"javascript:void(0)\" onClick=\"MM_openBrWindow('show_bbs_picture\\.pl\\?id=[0-9]+&comm_id=[0-9]+&number=[0-9]+','pict','width=680,height=660,toolbar=no,scrollbars=yes,left=5,top=5')\"><img src=\"http://ic[0-9]+\\.mixi\\.jp/[^.]+\\.jpg\" border=\"0\"></a></td>\n\\)*</tr></table>\\(.+\\)</td></tr></table>")
 
-(defun mixi-realize-topic (topic)
+(defun mixi-realize-topic (topic &optional page)
   "Realize a TOPIC."
   ;; FIXME: Check a expiration of cache?
   (unless (mixi-object-realized-p topic)
-    (with-mixi-retrieve (mixi-topic-page topic)
+    (with-mixi-retrieve (or page (mixi-topic-page topic))
       (if (re-search-forward mixi-topic-community-regexp nil t)
          (mixi-community-set-name (mixi-topic-community topic)
                                   (match-string 1))
@@ -1691,9 +1688,8 @@ Increase this value when unexpected error frequently occurs."
          (mixi-topic-set-title topic (match-string 1))
        (mixi-realization-error 'cannot-find-title topic))
       (if (re-search-forward mixi-topic-owner-regexp nil t)
-         (mixi-topic-set-owner topic
-                               (mixi-make-friend (match-string 1)
-                                                 (match-string 2)))
+         (mixi-topic-set-owner topic (mixi-make-friend (match-string 1)
+                                                       (match-string 2)))
        (mixi-realization-error 'cannot-find-owner topic))
       (if (re-search-forward mixi-topic-content-regexp nil t)
          (mixi-topic-set-content topic (match-string 2))
@@ -1859,11 +1855,11 @@ Increase this value when unexpected error frequently occurs."
 
 ?<td>&nbsp;\\(.+\\)</td>")
 
-(defun mixi-realize-event (event)
+(defun mixi-realize-event (event &optional page)
   "Realize a EVENT."
   ;; FIXME: Check a expiration of cache?
   (unless (mixi-object-realized-p event)
-    (with-mixi-retrieve (mixi-event-page event)
+    (with-mixi-retrieve (or page (mixi-event-page event))
       (if (re-search-forward mixi-event-community-regexp nil t)
          (mixi-community-set-name (mixi-event-community event)
                                   (match-string 1))
@@ -1880,9 +1876,8 @@ Increase this value when unexpected error frequently occurs."
          (mixi-event-set-title event (match-string 2))
        (mixi-realization-error 'cannot-find-title event))
       (if (re-search-forward mixi-event-owner-regexp nil t)
-         (mixi-event-set-owner event
-                               (mixi-make-friend (match-string 2)
-                                                 (match-string 3)))
+         (mixi-event-set-owner event (mixi-make-friend (match-string 2)
+                                                       (match-string 3)))
        (if (re-search-forward mixi-event-owner-seceded-regexp nil t)
            (mixi-event-set-owner event
                                  (mixi-make-friend nil (match-string 2)))
@@ -2108,6 +2103,19 @@ Increase this value when unexpected error frequently occurs."
                           (nth 1 item)))))
            items)))
 
+;; Parent object.
+(defmacro mixi-parent-p (object)
+  `(or (eq (mixi-object-class ,object) 'mixi-diary)
+       (mixi-bbs-p object)))
+
+(defun mixi-realize-parent (parent &optional page)
+  "Realize a PARENT."
+  (unless (mixi-parent-p parent)
+    (signal 'wrong-type-argument (list 'mixi-parent-p parent)))
+  (let ((func (intern (concat mixi-object-prefix "realize-"
+                             (mixi-object-name parent)))))
+    (funcall func parent page)))
+
 ;; Comment object.
 (defun mixi-make-comment (parent owner time content)
   "Return a comment object."
@@ -2241,24 +2249,18 @@ Increase this value when unexpected error frequently occurs."
 
 (defun mixi-get-comments (parent &optional range)
   "Get comments of PARENT."
-  (unless (mixi-object-p parent)
-    (signal 'wrong-type-argument (list 'mixi-object-p parent)))
+  (unless (mixi-parent-p parent)
+    (signal 'wrong-type-argument (list 'mixi-parent-p parent)))
   (let* ((name (mixi-object-name parent))
         (list-page (intern (concat mixi-object-prefix name
                                    "-comment-list-page")))
         (regexp (eval (intern (concat mixi-object-prefix name
-                                      "-comment-list-regexp")))))
-    (let ((items (mixi-get-matched-items
-                 (funcall list-page parent) regexp)))
-      (let (list)
-       (catch 'stop
-         (mapc (lambda (item)
-                 (when (and (numberp range)
-                            (>= (length list) range))
-                   (throw 'stop nil))
-                 (setq list (cons item list)))
-               (reverse items)))
-       (setq items (reverse list)))
+                                      "-comment-list-regexp"))))
+        (page (funcall list-page parent)))
+    (unless (mixi-object-realized-p parent)
+      (mixi-realize-parent parent page)
+      (setq page nil))
+    (let ((items (mixi-get-matched-items page regexp range t)))
       (mapcar (lambda (item)
                (mixi-make-comment parent (mixi-make-friend
                                           (nth 7 item) (nth 8 item))
@@ -2649,15 +2651,17 @@ Increase this value when unexpected error frequently occurs."
           "&media_id=" (mixi-news-media-id ,news)))
 
 (defconst mixi-news-title-regexp
-  "<td HEIGHT=\"46\" STYLE=\"font-weight: bold;font-size: 14px;\" CLASS=\"h130\">\\(.+\\)</td>")
+  "<td HEIGHT=\"46\" STYLE=\"font-weight: bold;font-size: 14px;\" CLASS=\"h130\">\\(.+\\)\\(\r
+\\)?</td>")
 (defconst mixi-news-media-time-regexp
   "<td COLSPAN=\"2\" ALIGN=\"right\">(\\(.+\\)&nbsp;-&nbsp;\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\))</td></tr>")
 (defconst mixi-news-content-regexp
   "<td CLASS=\"h150\">
 
 \\(.+\\)
-
-?
+<br>
+\\(.*
+\\)?
 
 \\(</td>\\|<br>\\)")