* mixi.el (mixi-favorite-list-id-regexp): Abolish.
[elisp/mixi.git] / mixi-atom.el
1 ;; mixi-atom.el --- Atom Syndication Format
2
3 ;; Copyright (C) 2007 OHASHI Akira
4
5 ;; Author: OHASHI Akira <bg66@koka-in.org>
6 ;; Keywords: hypermedia
7
8 ;; This file is *NOT* a part of Emacs.
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 ;; Bug reports:
28 ;;
29 ;; If you have bug reports and/or suggestions for improvement, please
30 ;; send them via <URL:http://mixi.jp/view_community.pl?id=1596390>.
31
32 ;;; Code:
33
34 (require 'mixi)
35 (require 'mixi-utils)
36
37 (defcustom mixi-atom-coding-system 'utf-8
38   "*Coding system for Atom Syndication Format."
39   :type 'coding-system
40   :group 'mixi)
41
42 (defcustom mixi-atom-namespace "http://www.w3.org/2005/Atom"
43   "*Namespace for Atom Syndication Format."
44   :type 'string
45   :group 'mixi)
46
47 (defcustom mixi-atom-title "Mixi Feed"
48   "*Title for feed."
49   :type 'string
50   :group 'mixi)
51
52 (defcustom mixi-atom-syndication-list
53   '((mixi-get-diaries . 10))
54   "*A list of atom syndication definition.
55 Each element looks like (URL . RANGE) or (FUNCTION . RANGE).
56 URL is the URL for mixi access point.  If URL is friend's, get his/her diaries
57 as article.  If community's, get its BBSes.  If diary's or BBS's, get its
58 comments.
59 FUNCTION is the function which has one `range' argument and returns the list
60 of mixi object.
61 RANGE is the range for getting articles.  If RANGE is nil, get all articles."
62   :group 'mixi
63   :type '(repeat (cons
64                   (radio (string :tag "URL")
65                          (const :tag "New diaries" mixi-get-new-diaries)
66                          (const :tag "New comments" mixi-get-new-comments)
67                          (const :tag "New BBSes" mixi-get-new-bbses)
68                          (const :tag "Messages" mixi-get-messages)
69                          (const :tag "Logs" mixi-get-logs)
70                          (function :tag "Other function"))
71                   (radio (integer :tag "Range")
72                          (const :tag "All" nil)))))
73
74 (defmacro mixi-atom-make-date (time)
75   `(let ((date (format-time-string "%Y-%m-%dT%T%z" ,time)))
76      (if (string-match "[+-][0-9][0-9][0-9][0-9]$" date)
77          (let ((length (length date)))
78            (format "%s:%s"
79                    (substring date 0 (- length 2))
80                    (substring date (- length 2) length)))
81        date)))
82
83 (defmacro mixi-atom-now ()
84   `(mixi-atom-make-date (current-time)))
85
86 (defun mixi-make-atom-entry (object)
87   "Make Atom entry."
88   (concat "<entry>\n"
89           " <title>" (mixi-make-title object) "</title>\n"
90           " <link href=\"" (mixi-make-encoded-url object) "\"/>\n"
91           " <id>" (mixi-make-tag-uri object) "</id>\n"
92           " <updated>" (mixi-atom-make-date (mixi-object-time object))
93           "</updated>\n"
94           " <summary>" (mixi-remove-markup (mixi-make-content object))
95           "</summary>\n"
96           "</entry>\n"))
97
98 (defun mixi-make-atom-entries (objects &optional range)
99   "Make Atom entries."
100   (let (entries)
101     (mapcar (lambda (object)
102               (setq entries
103                     (concat entries (mixi-make-atom-entry object)))
104               (when (mixi-parent-p object)
105                 (let ((comments (mixi-get-comments object range)))
106                   (mapc (lambda (comment)
107                           (setq entries
108                                 (concat entries
109                                         (mixi-make-atom-entry comment))))
110                         comments))))
111           objects)
112     entries))
113
114 ;;;###autoload
115 (defun mixi-make-atom ()
116   "Make Atom Syndication Format"
117   (insert "<?xml version=\"1.0\" encoding=\""
118           (symbol-name mixi-atom-coding-system) "\"?>\n"
119           "<feed xmlns=\"" mixi-atom-namespace "\">\n"
120           "\n"
121           "<title>" mixi-atom-title "</title>\n"
122           "<link href=\"" mixi-url "\"/>\n"
123           "<updated>" (mixi-atom-now) "</updated>\n"
124           "<author>\n"
125           " <name>" (mixi-friend-nick (mixi-make-me)) "</name>\n"
126           "</author>\n"
127           "<id>tag:mixi.jp," (format-time-string "%Y-%m-%d"
128                                                  (current-time))
129           ":" (mixi-friend-nick (mixi-make-me)) "</id>\n"
130           "\n"
131           (mapconcat 'identity
132                      (mapcar (lambda (list)
133                                (let ((url-or-function (car list))
134                                      (range (cdr list)))
135                                  (mixi-make-atom-entries
136                                   (mixi-make-objects url-or-function
137                                                      range) range)))
138                              mixi-atom-syndication-list)
139                      "\n")
140           "\n"
141           "</feed>\n"))
142
143 ;;;###autoload
144 (defun mixi-atom-write-file (file)
145   (with-temp-buffer
146     (mixi-make-atom)
147     (let ((coding-system-for-write mixi-atom-coding-system)
148           (file (expand-file-name file)))
149       (write-region (point-min) (point-max) file))))
150
151 (provide 'mixi-atom)
152
153 ;;; mixi-atom.el ends here