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