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