* mixi-el: Version 1.4.1 released.
[elisp/mixi.git] / sb-mixi.el
1 ;;; sb-mixi.el --- shimbun backend for mixi
2
3 ;; Copyright (C) 2006, 2007, 2008 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 GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; 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.63 $")
37
38 (eval-and-compile
39   (luna-define-class shimbun-mixi (shimbun) (comment-cache release-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     ("new-bbs-comments" . mixi-get-new-bbs-comments)
47     ("messages" . mixi-get-messages)
48     ("messages.sent" .
49      (lambda (range)
50        (mixi-get-messages 'outbox range)))
51     ("logs" . mixi-get-logs)
52     ("my-diaries" . "/home.pl")
53     ("mixi-el" . "/view_community.pl?id=1596390")
54     ("news.newest.domestic" .
55      (lambda (range)
56        (mixi-get-news 'domestic 'newest range)))
57     ("news.newest.politics" .
58      (lambda (range)
59        (mixi-get-news 'politics 'newest range)))
60     ("news.newest.economy" .
61      (lambda (range)
62        (mixi-get-news 'economy 'newest range)))
63     ("news.newest.area" .
64      (lambda (range)
65        (mixi-get-news 'area 'newest range)))
66     ("news.newest.abroad" .
67      (lambda (range)
68        (mixi-get-news 'abroad 'newest range)))
69     ("news.newest.sports" .
70      (lambda (range)
71        (mixi-get-news 'sports 'newest range)))
72     ("news.newest.entertainment" .
73      (lambda (range)
74        (mixi-get-news 'entertainment 'newest range)))
75     ("news.newest.it" .
76      (lambda (range)
77        (mixi-get-news 'IT 'newest range)))
78     ("news.newest.game-anime" .
79      (lambda (range)
80        (mixi-get-news 'game-anime 'newest range)))
81     ("news.newest.column" .
82      (lambda (range)
83        (mixi-get-news 'column 'newest range)))
84     ("news.pickup.domestic" .
85      (lambda (range)
86        (mixi-get-news 'domestic 'pickup range)))
87     ("news.pickup.politics" .
88      (lambda (range)
89        (mixi-get-news 'politics 'pickup range)))
90     ("news.pickup.economy" .
91      (lambda (range)
92        (mixi-get-news 'economy 'pickup range)))
93     ("news.pickup.area" .
94      (lambda (range)
95        (mixi-get-news 'area 'pickup range)))
96     ("news.pickup.abroad" .
97      (lambda (range)
98        (mixi-get-news 'abroad 'pickup range)))
99     ("news.pickup.sports" .
100      (lambda (range)
101        (mixi-get-news 'sports 'pickup range)))
102     ("news.pickup.entertainment" .
103      (lambda (range)
104        (mixi-get-news 'entertainment 'pickup range)))
105     ("news.pickup.it" .
106      (lambda (range)
107        (mixi-get-news 'IT 'pickup range)))
108     ("news.pickup.game-anime" .
109      (lambda (range)
110        (mixi-get-news 'game-anime 'pickup range)))
111     ("news.pickup.column" .
112      (lambda (range)
113        (mixi-get-news 'column 'pickup range)))
114     ("releases" . mixi-get-releases))
115   "An alist of mixi shimbun group default definition.")
116
117 (defcustom shimbun-mixi-group-alist nil
118   "*An alist of mixi shimbun group definition.
119 Each element looks like (NAME . URL) or (NAME . FUNCTION).
120 NAME is a shimbun group name that you can name it as you like.
121 URL is the URL for mixi access point of the group.  If URL is friend's, get
122 his/her diaries as article.  If community's, get its BBSes.  If diary's or
123 BBS's, get its comments.
124 FUNCTION is the function which has one `range' argument and returns the list
125 of mixi object."
126   :group 'shimbun
127   :type '(repeat (cons :fromat "%v"
128                        (string :tag "Group name")
129                        (radio (string :tag "URL")
130                               (function :tag "Function")))))
131
132 (defcustom shimbun-mixi-get-profile-with-diary t
133   "*If non-nil, get his/her profile together with diaries."
134   :group 'shimbun
135   :type 'boolean)
136
137 (defcustom shimbun-mixi-add-comment-count-to-author t
138   "*If non-nil, add comment count to author."
139   :group 'shimbun
140   :type 'boolean)
141
142 ;; FIXME: Don't use this user option.
143 (defcustom shimbun-mixi-page-articles 10
144   "*How many articles are there in one page."
145   :group 'shimbun
146   :type 'integer)
147
148 (defvar shimbun-mixi-x-face-alist
149   '(("default" . "X-Face: CY;j#FoBnpK^37`-IoJvN!J^u;GciiPmMQ@T)~RP1]t8iv?v)/bVI:I\"F!JfWJvhM5{zY!=
150  h.d+'g\\I{D>Ocy?Rc4uYUyOZj2%2Kl>,x-!MCSsyi3!L}psrrC1jlF,O?Ui>qf)X;sBz`/}\\066X%$
151  siG'|4K!2?==|oB&#E'5GGH\\#z[muyQ")))
152
153 (defun shimbun-mixi-group-alist ()
154   (append shimbun-mixi-default-group-alist
155           shimbun-mixi-group-alist))
156
157 (luna-define-method initialize-instance :after ((shimbun shimbun-mixi)
158                                                 &rest init-args)
159   (shimbun-mixi-set-comment-cache-internal shimbun
160                                            (make-hash-table :test 'equal))
161   (shimbun-mixi-set-release-cache-internal shimbun
162                                            (make-hash-table :test 'equal))
163   shimbun)
164
165 (luna-define-method shimbun-close :after ((shimbun shimbun-mixi))
166   (shimbun-mixi-set-comment-cache-internal shimbun nil)
167   (shimbun-mixi-set-release-cache-internal shimbun nil)
168   (mixi-logout))
169
170 (luna-define-method shimbun-groups ((shimbun shimbun-mixi))
171   (mapcar 'car (shimbun-mixi-group-alist)))
172
173 (luna-define-method shimbun-reply-to ((shimbun shimbun-mixi))
174   mixi-reply-to)
175
176 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
177   (let (headers)
178     (catch 'stop
179       (while objects
180         (let ((object (car objects)))
181           (when (and shimbun-mixi-get-profile-with-diary
182                      (mixi-diary-p object))
183             (let ((owner (mixi-diary-owner object)))
184               (unless (mixi-object-realized-p owner)
185                 (mixi-realize-friend owner))))
186           (when (mixi-parent-p object)
187             (let* ((comments (mixi-get-comments object range))
188                    (comment-headers (shimbun-mixi-get-headers shimbun
189                                                               comments)))
190               (while comment-headers
191                 (push (car comment-headers) headers)
192                 (setq comment-headers (cdr comment-headers)))))
193           (let ((class (mixi-object-class object))
194                 (id (mixi-make-message-id object)))
195             (when (and (eq class 'mixi-comment)
196                        (shimbun-search-id shimbun id))
197               (throw 'stop nil))
198             (push
199              (shimbun-create-header
200               0
201               (mixi-make-title object (string-match
202                                        "^new-"
203                                        (shimbun-current-group-internal
204                                         shimbun)))
205               (mixi-make-author object shimbun-mixi-add-comment-count-to-author)
206               (mixi-make-date object)
207               id
208               (if (eq class 'mixi-comment)
209                   (mixi-make-message-id (mixi-comment-parent object))
210                 "")
211               0 0
212               (mixi-make-url object))
213              headers)
214             (when (eq class 'mixi-comment)
215               (puthash id (mixi-comment-content object)
216                        (shimbun-mixi-comment-cache-internal shimbun)))))
217         (setq objects (cdr objects))))
218     headers))
219
220 (luna-define-method shimbun-get-headers ((shimbun shimbun-mixi)
221                                          &optional range)
222   (let ((url-or-function (cdr (assoc (shimbun-current-group-internal shimbun)
223                                      (shimbun-mixi-group-alist))))
224         (range (shimbun-header-index-pages range)))
225     (when (integerp range)
226       (setq range (* range shimbun-mixi-page-articles)))
227     (shimbun-sort-headers
228      (shimbun-mixi-get-headers shimbun
229                                (mixi-make-objects url-or-function range)
230                                range))))
231
232 (defun shimbun-mixi-comment-article (url shimbun header)
233   (let* ((message-id (shimbun-header-id header))
234          (cache (shimbun-mixi-comment-cache-internal shimbun))
235          (article (gethash message-id cache)))
236     (unless (stringp article)
237       (let* ((parent (mixi-make-object-from-url url))
238              (comments (mixi-get-comments parent)))
239         (while comments
240           (let ((id (mixi-make-message-id (car comments)))
241                 (content (mixi-comment-content (car comments))))
242             (puthash id content cache)
243             (when (string= id message-id)
244               (setq article content)))
245           (setq comments (cdr comments)))))
246     article))
247
248 (defun shimbun-mixi-release-article (shimbun header)
249   (let* ((message-id (shimbun-header-id header))
250          (cache (shimbun-mixi-release-cache-internal shimbun))
251          (article (gethash message-id cache)))
252     (unless (stringp article)
253       (let ((releases (mixi-get-releases)))
254         (while releases
255           (let ((id (mixi-make-message-id (car releases)))
256                 (content (mixi-release-content (car releases))))
257             (puthash id content cache)
258             (when (string= id message-id)
259               (setq article content)))
260           (setq releases (cdr releases)))))
261     article))
262
263 (luna-define-method shimbun-article ((shimbun shimbun-mixi)
264                                      header &optional outbuf)
265   (when (shimbun-current-group-internal shimbun)
266     (with-current-buffer (or outbuf (current-buffer))
267       (w3m-insert-string
268        (or (with-temp-buffer
269              (let* ((url (shimbun-article-url shimbun header))
270                     (object (mixi-make-object-from-url url))
271                     (article (cond ((string-match "#comment$" url)
272                                     (shimbun-mixi-comment-article
273                                      url shimbun header))
274                                    ((string-match "release_info.pl$" url)
275                                     (shimbun-mixi-release-article
276                                      shimbun header))
277                                    (t
278                                     (mixi-make-content object)))))
279                (mixi-make-reply-to object)
280                (when (stringp article)
281                  (insert article)))
282              (shimbun-message shimbun "shimbun: Make contents...")
283              (goto-char (point-min))
284              (prog1 (shimbun-make-contents shimbun header)
285                (shimbun-message shimbun "shimbun: Make contents...done")))
286            "")))))
287
288 (provide 'sb-mixi)
289
290 ;;; sb-mixi.el ends here