(mixi-diary-comment-list-regexp): Follow the change of mixi.
[elisp/mixi.git] / sb-mixi.el
1 ;;; sb-mixi.el --- shimbun backend for mixi
2
3 ;; Copyright (C) 2006, 2007 OHASHI Akira
4
5 ;; Author: OHASHI Akira <bg66@koka-in.org>
6 ;; Keywords: news
7
8 ;; This file is *NOT* a part of shimbun.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; if not, you can either send email to this
22 ;; program's maintainer or write to: The Free Software Foundation,
23 ;; Inc.; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; If you have bug reports and/or suggestions for improvement, please
28 ;; send them via <URL:http://mixi.jp/view_community.pl?id=1596390>.
29
30 ;;; Code:
31
32 (require 'mixi)
33 (require 'mixi-utils)
34 (require 'shimbun)
35
36 (defconst shimbun-mixi-revision "$Revision: 1.57 $")
37
38 (eval-and-compile
39   (luna-define-class shimbun-mixi (shimbun) (comment-cache))
40   (luna-define-internal-accessors 'shimbun-mixi))
41
42 (defconst shimbun-mixi-default-group-alist
43   '(("new-diaries" . mixi-get-new-diaries)
44     ("new-comments" . mixi-get-new-comments)
45     ("new-bbses" . mixi-get-new-bbses)
46     ("messages" . mixi-get-messages)
47     ("messages.sent" .
48      (lambda (range)
49        (mixi-get-messages 'outbox range)))
50     ("logs" . mixi-get-logs)
51     ("my-diaries" . "/home.pl")
52     ("mixi-el" . "/view_community.pl?id=1596390")
53     ("news.newest.domestic" .
54      (lambda (range)
55        (mixi-get-news 'domestic 'newest range)))
56     ("news.newest.politics" .
57      (lambda (range)
58        (mixi-get-news 'politics 'newest range)))
59     ("news.newest.economy" .
60      (lambda (range)
61        (mixi-get-news 'economy 'newest range)))
62     ("news.newest.area" .
63      (lambda (range)
64        (mixi-get-news 'area 'newest range)))
65     ("news.newest.abroad" .
66      (lambda (range)
67        (mixi-get-news 'abroad 'newest range)))
68     ("news.newest.sports" .
69      (lambda (range)
70        (mixi-get-news 'sports 'newest range)))
71     ("news.newest.entertainment" .
72      (lambda (range)
73        (mixi-get-news 'entertainment 'newest range)))
74     ("news.newest.it" .
75      (lambda (range)
76        (mixi-get-news 'IT 'newest range)))
77     ("news.pickup.domestic" .
78      (lambda (range)
79        (mixi-get-news 'domestic 'pickup range)))
80     ("news.pickup.politics" .
81      (lambda (range)
82        (mixi-get-news 'politics 'pickup range)))
83     ("news.pickup.economy" .
84      (lambda (range)
85        (mixi-get-news 'economy 'pickup range)))
86     ("news.pickup.area" .
87      (lambda (range)
88        (mixi-get-news 'area 'pickup range)))
89     ("news.pickup.abroad" .
90      (lambda (range)
91        (mixi-get-news 'abroad 'pickup range)))
92     ("news.pickup.sports" .
93      (lambda (range)
94        (mixi-get-news 'sports 'pickup range)))
95     ("news.pickup.entertainment" .
96      (lambda (range)
97        (mixi-get-news 'entertainment 'pickup range)))
98     ("news.pickup.it" .
99      (lambda (range)
100        (mixi-get-news 'IT 'pickup range))))
101   "An alist of mixi shimbun group default definition.")
102
103 (defcustom shimbun-mixi-group-alist nil
104   "*An alist of mixi shimbun group definition.
105 Each element looks like (NAME . URL) or (NAME . FUNCTION).
106 NAME is a shimbun group name that you can name it as you like.
107 URL is the URL for mixi access point of the group.  If URL is friend's, get
108 his/her diaries as article.  If community's, get its BBSes.  If diary's or
109 BBS's, get its comments.
110 FUNCTION is the function which has one `range' argument and returns the list
111 of mixi object."
112   :group 'shimbun
113   :type '(repeat (cons :fromat "%v"
114                        (string :tag "Group name")
115                        (radio (string :tag "URL")
116                               (const :tag "New diaries" mixi-get-new-diaries)
117                               (const :tag "New comments" mixi-get-new-comments)
118                               (const :tag "New BBSes" mixi-get-new-bbses)
119                               (const :tag "Messages" mixi-get-messages)
120                               (const :tag "Logs" mixi-get-logs)
121                               (function :tag "Other function")))))
122
123 (defcustom shimbun-mixi-get-profile-with-diary t
124   "*If non-nil, get his/her profile together with diaries."
125   :group 'shimbun
126   :type 'boolean)
127
128 ;; FIXME: Don't use this user option.
129 (defcustom shimbun-mixi-page-articles 10
130   "*How many articles are there in one page."
131   :group 'shimbun
132   :type 'integer)
133
134 (defvar shimbun-mixi-x-face-alist
135   '(("default" . "X-Face: CY;j#FoBnpK^37`-IoJvN!J^u;GciiPmMQ@T)~RP1]t8iv?v)/bVI:I\"F!JfWJvhM5{zY!=
136  h.d+'g\\I{D>Ocy?Rc4uYUyOZj2%2Kl>,x-!MCSsyi3!L}psrrC1jlF,O?Ui>qf)X;sBz`/}\\066X%$
137  siG'|4K!2?==|oB&#E'5GGH\\#z[muyQ")))
138
139 (defun shimbun-mixi-group-alist ()
140   (append shimbun-mixi-default-group-alist
141           shimbun-mixi-group-alist))
142
143 (luna-define-method initialize-instance :after ((shimbun shimbun-mixi)
144                                                 &rest init-args)
145   (shimbun-mixi-set-comment-cache-internal shimbun
146                                            (make-hash-table :test 'equal))
147   shimbun)
148
149 (luna-define-method shimbun-close :after ((shimbun shimbun-mixi))
150   (shimbun-mixi-set-comment-cache-internal shimbun nil)
151   (mixi-logout))
152
153 (luna-define-method shimbun-groups ((shimbun shimbun-mixi))
154   (mapcar 'car (shimbun-mixi-group-alist)))
155
156 (luna-define-method shimbun-reply-to ((shimbun shimbun-mixi))
157   mixi-reply-to)
158
159 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
160   (let (headers)
161     (catch 'stop
162       (while objects
163         (let ((object (car objects)))
164           (when (and shimbun-mixi-get-profile-with-diary
165                      (mixi-diary-p object))
166             (let ((owner (mixi-diary-owner object)))
167               (unless (mixi-object-realized-p owner)
168                 (mixi-realize-friend owner))))
169           (when (mixi-parent-p object)
170             (let* ((comments (mixi-get-comments object range))
171                    (comment-headers (shimbun-mixi-get-headers shimbun
172                                                               comments)))
173               (while comment-headers
174                 (push (car comment-headers) headers)
175                 (setq comment-headers (cdr comment-headers)))))
176           (let ((class (mixi-object-class object))
177                 (id (mixi-make-message-id object)))
178             (when (and (eq class 'mixi-comment)
179                        (shimbun-search-id shimbun id))
180               (throw 'stop nil))
181             (push
182              (shimbun-create-header
183               0
184               (mixi-make-title object (string-match
185                                        "^new-"
186                                        (shimbun-current-group-internal
187                                         shimbun)))
188               (mixi-make-author object)
189               (mixi-make-date object)
190               id
191               (if (eq class 'mixi-comment)
192                   (mixi-make-message-id (mixi-comment-parent object))
193                 "")
194               0 0
195               (mixi-make-url object))
196              headers)
197             (when (eq class 'mixi-comment)
198               (puthash id (mixi-comment-content object)
199                        (shimbun-mixi-comment-cache-internal shimbun)))))
200         (setq objects (cdr objects))))
201     headers))
202
203 (luna-define-method shimbun-get-headers ((shimbun shimbun-mixi)
204                                          &optional range)
205   (let ((url-or-function (cdr (assoc (shimbun-current-group-internal shimbun)
206                                      (shimbun-mixi-group-alist))))
207         (range (shimbun-header-index-pages range)))
208     (when (integerp range)
209       (setq range (* range shimbun-mixi-page-articles)))
210     (shimbun-sort-headers
211      (shimbun-mixi-get-headers shimbun
212                                (mixi-make-objects url-or-function range)
213                                range))))
214
215 (defun shimbun-mixi-comment-article (url shimbun header)
216   (let* ((message-id (shimbun-header-id header))
217          (cache (shimbun-mixi-comment-cache-internal shimbun))
218          (article (gethash message-id cache)))
219     (unless (stringp article)
220       (let* ((parent (mixi-make-object-from-url url))
221              (comments (mixi-get-comments parent)))
222         (while comments
223           (let ((id (mixi-make-message-id (car comments)))
224                 (content (mixi-comment-content (car comments))))
225             (puthash id content cache)
226             (when (string= id message-id)
227               (setq article content)))
228           (setq comments (cdr comments)))))
229     article))
230
231 (luna-define-method shimbun-article ((shimbun shimbun-mixi)
232                                      header &optional outbuf)
233   (when (shimbun-current-group-internal shimbun)
234     (with-current-buffer (or outbuf (current-buffer))
235       (w3m-insert-string
236        (or (with-temp-buffer
237              (let* ((url (shimbun-article-url shimbun header))
238                     (object (mixi-make-object-from-url url))
239                     (article (if (string-match "#comment$" url)
240                                  (shimbun-mixi-comment-article
241                                   url shimbun header)
242                                (mixi-make-content object))))
243                (mixi-make-reply-to object)
244                (when (stringp article)
245                  (insert article)))
246              (shimbun-message shimbun "shimbun: Make contents...")
247              (goto-char (point-min))
248              (prog1 (shimbun-make-contents shimbun header)
249                (shimbun-message shimbun "shimbun: Make contents...done")))
250            "")))))
251
252 (provide 'sb-mixi)
253
254 ;;; sb-mixi.el ends here