1 ;;; nnrss.el --- interfacing with RSS
2 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
44 (eval '(require 'xml))
48 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
49 "Where nnrss will save its files.")
51 ;; (group max rss-url)
52 (defvoo nnrss-server-data nil)
54 ;; (num timestamp url subject author date extra)
55 (defvoo nnrss-group-data nil)
56 (defvoo nnrss-group-max 0)
57 (defvoo nnrss-group-min 1)
58 (defvoo nnrss-group nil)
59 (defvoo nnrss-group-hashtb (make-hash-table :test 'equal))
60 (defvoo nnrss-status-string "")
62 (defconst nnrss-version "nnrss 1.0")
64 (defvar nnrss-group-alist '()
65 "List of RSS addresses.")
67 (defvar nnrss-use-local nil)
69 (defvar nnrss-description-field 'X-Gnus-Description
70 "Field name used for DESCRIPTION.
71 To use the description in headers, put this name into `nnmail-extra-headers'.")
73 (defvar nnrss-url-field 'X-Gnus-Url
74 "Field name used for URL.
75 To use the description in headers, put this name into `nnmail-extra-headers'.")
77 (defvar nnrss-content-function nil
78 "A function which is called in `nnrss-request-article'.
79 The arguments are (ENTRY GROUP ARTICLE).
80 ENTRY is the record of the current headline. GROUP is the group name.
81 ARTICLE is the article number of the current headline.")
83 (defvar nnrss-file-coding-system mm-universal-coding-system
84 "Coding system used when reading and writing files.")
86 (defvar nnrss-compatible-encoding-alist '((iso-8859-1 . windows-1252))
87 "Alist of encodings and those supersets.
88 The cdr of each element is used to decode data if it is available when
89 the car is what the data specify as the encoding. Or, the car is used
90 for decoding when the cdr that the data specify is not available.")
92 (nnoo-define-basics nnrss)
94 ;;; Interface functions
96 (defsubst nnrss-format-string (string)
97 (gnus-replace-in-string string " *\n *" " "))
99 (defun nnrss-decode-group-name (group)
100 (if (and group (mm-coding-system-p 'utf-8))
101 (setq group (mm-decode-coding-string group 'utf-8))
104 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
105 (setq group (nnrss-decode-group-name group))
106 (nnrss-possibly-change-group group server)
109 (set-buffer nntp-server-buffer)
111 (dolist (article articles)
112 (if (setq e (assq article nnrss-group-data))
113 (insert (number-to-string (car e)) "\t" ;; number
118 (or (nth 4 e) "(nobody)")
124 (format "<%d@%s.nnrss>" (car e) group)
135 (memq nnrss-description-field
136 nnmail-extra-headers))
137 (concat (symbol-name nnrss-description-field)
139 (nnrss-format-string (nth 6 e))
143 (memq nnrss-url-field
144 nnmail-extra-headers))
145 (concat (symbol-name nnrss-url-field)
147 (nnrss-format-string (nth 2 e))
153 (deffoo nnrss-request-group (group &optional server dont-check)
154 (setq group (nnrss-decode-group-name group))
155 (nnheader-message 6 "nnrss: Requesting %s..." group)
156 (nnrss-possibly-change-group group server)
160 (nnrss-check-group group server)
161 (nnheader-report 'nnrss "Opened group %s" group)
163 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
164 (prin1-to-string group)
166 (nnheader-message 6 "nnrss: Requesting %s...done" group)))
168 (deffoo nnrss-close-group (group &optional server)
171 (deffoo nnrss-request-article (article &optional group server buffer)
172 (setq group (nnrss-decode-group-name group))
173 (when (stringp article)
174 (setq article (if (string-match "\\`<\\([0-9]+\\)@" article)
175 (string-to-number (match-string 1 article))
177 (nnrss-possibly-change-group group server)
178 (let ((e (assq article nnrss-group-data))
179 (nntp-server-buffer (or buffer nntp-server-buffer))
182 (with-current-buffer nntp-server-buffer
185 (insert "Newsgroups: " group "\n"))
187 (insert "Subject: " (nth 3 e) "\n"))
189 (insert "From: " (nth 4 e) "\n"))
191 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
192 (let ((header (buffer-string))
195 (delete "" (split-string (nth 6 e) "\n+"))
198 ;; Enable encoding of Newsgroups header in XEmacs.
199 (default-enable-multibyte-characters t)
200 (rfc2047-header-encoding-alist
201 (if (mm-coding-system-p 'utf-8)
202 (cons '("Newsgroups" . utf-8)
203 rfc2047-header-encoding-alist)
204 rfc2047-header-encoding-alist))
205 rfc2047-encode-encoded-words body)
208 (insert "<#multipart type=alternative>\n"
209 "<#part type=\"text/plain\">\n")
215 (insert "\n" link "\n")))
218 (setq body (buffer-substring body (point)))
220 "<#part type=\"text/html\">\n"
221 "<html><head></head><body>\n")
225 (insert "<p><a href=\"" link "\">link</a></p>\n"))
226 (insert "</body></html>\n"
234 "Content-Type: text/plain; charset=gnus-decoded\n"
235 "Content-Transfer-Encoding: 8bit\n\n"
238 3 "Warning - there might be invalid characters"))))
239 (goto-char (point-min))
240 (search-forward "\n\n")
242 (insert (format "Message-ID: <%d@%s.nnrss>\n"
244 (let ((rfc2047-encoding-type 'mime)
245 rfc2047-encode-max-chars)
246 (rfc2047-encode-string
247 (gnus-replace-in-string group "[\t\n ]+" "_")))))
248 (when nnrss-content-function
249 (funcall nnrss-content-function e group article))))
252 (nnheader-report 'nnrss err))
254 (nnheader-report 'nnrss "no such id: %d" article))
256 (nnheader-report 'nnrss "article %s retrieved" (car e))
257 ;; we return the article number.
258 (cons nnrss-group (car e))))))
260 (deffoo nnrss-request-list (&optional server)
261 (nnrss-possibly-change-group nil server)
262 (nnrss-generate-active)
265 (deffoo nnrss-open-server (server &optional defs connectionless)
266 (nnrss-read-server-data server)
267 (nnoo-change-server 'nnrss server defs)
270 (deffoo nnrss-request-expire-articles
271 (articles group &optional server force)
272 (setq group (nnrss-decode-group-name group))
273 (nnrss-possibly-change-group group server)
274 (let (e days not-expirable changed)
275 (dolist (art articles)
276 (if (and (setq e (assq art nnrss-group-data))
277 (nnmail-expired-article-p
279 (if (listp (setq days (nth 1 e))) days
280 (days-to-time (- days (time-to-days '(0 0)))))
282 (setq nnrss-group-data (delq e nnrss-group-data)
284 (push art not-expirable)))
286 (nnrss-save-group-data group server))
289 (deffoo nnrss-request-delete-group (group &optional force server)
290 (setq group (nnrss-decode-group-name group))
291 (nnrss-possibly-change-group group server)
293 ;; There may be two or more entries in `nnrss-group-alist' since
294 ;; this function didn't delete them formerly.
295 (while (setq elem (assoc group nnrss-group-alist))
296 (setq nnrss-group-alist (delq elem nnrss-group-alist))))
297 (setq nnrss-server-data
298 (delq (assoc group nnrss-server-data) nnrss-server-data))
299 (nnrss-save-server-data server)
301 (delete-file (nnrss-make-filename group server)))
304 (deffoo nnrss-request-list-newsgroups (&optional server)
305 (nnrss-possibly-change-group nil server)
307 (set-buffer nntp-server-buffer)
309 (dolist (elem nnrss-group-alist)
311 (insert (car elem) "\t" (third elem) "\n"))))
314 (nnoo-define-skeleton nnrss)
316 ;;; Internal functions
317 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
319 (defun nnrss-get-encoding ()
320 "Return an encoding attribute specified in the current xml contents.
321 If `nnrss-compatible-encoding-alist' specifies the compatible encoding,
322 it is used instead. If the xml contents doesn't specify the encoding,
323 return `utf-8' which is the default encoding for xml if it is available,
324 otherwise return nil."
325 (goto-char (point-min))
326 (if (re-search-forward
327 "<\\?[^>]*encoding=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
329 (let ((encoding (intern (downcase (or (match-string 1)
330 (match-string 2))))))
332 (mm-coding-system-p (cdr (assq encoding
333 nnrss-compatible-encoding-alist)))
334 (mm-coding-system-p encoding)
335 (mm-coding-system-p (car (rassq encoding
336 nnrss-compatible-encoding-alist)))))
337 (mm-coding-system-p 'utf-8)))
339 (defun nnrss-fetch (url &optional local)
340 "Fetch URL and put it in a the expected Lisp structure."
341 (mm-with-unibyte-buffer
342 ;;some CVS versions of url.el need this to close the connection quickly
343 (let (cs xmlform htmlform)
344 ;; bit o' work necessary for w3 pre-cvs and post-cvs
346 (let ((coding-system-for-read 'binary))
347 (insert-file-contents url))
348 ;; FIXME: shouldn't binding `coding-system-for-read' be moved
349 ;; to `mm-url-insert'?
350 (let ((coding-system-for-read 'binary))
351 (mm-url-insert url)))
352 (nnheader-remove-cr-followed-by-lf)
353 ;; Decode text according to the encoding attribute.
354 (when (setq cs (nnrss-get-encoding))
355 (mm-decode-coding-region (point-min) (point-max) cs)
356 (mm-enable-multibyte))
357 (goto-char (point-min))
359 ;; Because xml-parse-region can't deal with anything that isn't
360 ;; xml and w3-parse-buffer can't deal with some xml, we have to
361 ;; parse with xml-parse-region first and, if that fails, parse
362 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
363 ;; why w3-parse-buffer fails to parse some well-formed xml and
367 (setq xmlform (xml-parse-region (point-min) (point-max)))
370 (setq htmlform (caddar (w3-parse-buffer
374 nnrss: %s: Not valid XML %s and w3-parse doesn't work %s"
380 (defun nnrss-possibly-change-group (&optional group server)
382 (not (nnrss-server-opened server)))
383 (nnrss-open-server server))
384 (when (and group (not (equal group nnrss-group)))
385 (nnrss-read-group-data group server)
386 (setq nnrss-group group)))
388 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
390 (defun nnrss-generate-active ()
391 (when (y-or-n-p "Fetch extra categories? ")
392 (mapc 'funcall nnrss-extra-categories))
394 (set-buffer nntp-server-buffer)
396 (dolist (elem nnrss-group-alist)
397 (insert (prin1-to-string (car elem)) " 0 1 y\n"))
398 (dolist (elem nnrss-server-data)
399 (unless (assoc (car elem) nnrss-group-alist)
400 (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
404 (defun nnrss-read-server-data (server)
405 (setq nnrss-server-data nil)
406 (let ((file (nnrss-make-filename "nnrss" server)))
407 (when (file-exists-p file)
408 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
409 ;; file names. So, we use `insert-file-contents' instead.
410 (mm-with-multibyte-buffer
411 (let ((coding-system-for-read nnrss-file-coding-system)
412 (file-name-coding-system nnmail-pathname-coding-system))
413 (insert-file-contents file)
414 (eval-region (point-min) (point-max)))))))
416 (defun nnrss-save-server-data (server)
417 (gnus-make-directory nnrss-directory)
418 (let ((coding-system-for-write nnrss-file-coding-system)
419 (file-name-coding-system nnmail-pathname-coding-system))
420 (with-temp-file (nnrss-make-filename "nnrss" server)
421 (insert (format ";; -*- coding: %s; -*-\n"
422 nnrss-file-coding-system))
423 (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
425 (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
427 (defun nnrss-read-group-data (group server)
428 (setq nnrss-group-data nil)
429 (if (hash-table-p nnrss-group-hashtb)
430 (clrhash nnrss-group-hashtb)
431 (setq nnrss-group-hashtb (make-hash-table :test 'equal)))
432 (let ((pair (assoc group nnrss-server-data)))
433 (setq nnrss-group-max (or (cadr pair) 0))
434 (setq nnrss-group-min (+ nnrss-group-max 1)))
435 (let ((file (nnrss-make-filename group server)))
436 (when (file-exists-p file)
437 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
438 ;; file names. So, we use `insert-file-contents' instead.
439 (mm-with-multibyte-buffer
440 (let ((coding-system-for-read nnrss-file-coding-system)
441 (file-name-coding-system nnmail-pathname-coding-system))
442 (insert-file-contents file)
443 (eval-region (point-min) (point-max))))
444 (dolist (e nnrss-group-data)
445 (puthash (or (nth 2 e) (nth 6 e)) t nnrss-group-hashtb)
446 (when (and (car e) (> nnrss-group-min (car e)))
447 (setq nnrss-group-min (car e)))
448 (when (and (car e) (< nnrss-group-max (car e)))
449 (setq nnrss-group-max (car e)))))))
451 (defun nnrss-save-group-data (group server)
452 (gnus-make-directory nnrss-directory)
453 (let ((coding-system-for-write nnrss-file-coding-system)
454 (file-name-coding-system nnmail-pathname-coding-system))
455 (with-temp-file (nnrss-make-filename group server)
456 (insert (format ";; -*- coding: %s; -*-\n"
457 nnrss-file-coding-system))
458 (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
460 (defun nnrss-make-filename (name server)
462 (nnrss-translate-file-chars
465 (not (equal server ""))
471 (gnus-add-shutdown 'nnrss-close 'gnus)
473 (defun nnrss-close ()
474 "Clear internal nnrss variables."
475 (setq nnrss-group-data nil
476 nnrss-server-data nil
477 nnrss-group-hashtb nil
478 nnrss-group-alist nil))
482 (defun nnrss-no-cache (url)
485 (defun nnrss-insert-w3 (url)
486 (mm-with-unibyte-current-buffer
487 (mm-url-insert url)))
489 (defun nnrss-decode-entities-string (string)
491 (mm-with-multibyte-buffer
493 (mm-url-decode-entities-nbsp)
496 (defalias 'nnrss-insert 'nnrss-insert-w3)
498 (defun nnrss-mime-encode-string (string)
499 (mm-with-multibyte-buffer
501 (mm-url-decode-entities-nbsp)
502 (goto-char (point-min))
503 (while (re-search-forward "[\t\n ]+" nil t)
505 (goto-char (point-min))
506 (skip-chars-forward " ")
507 (delete-region (point-min) (point))
508 (goto-char (point-max))
509 (skip-chars-forward " ")
510 (delete-region (point) (point-max))
511 (let ((rfc2047-encoding-type 'mime)
512 rfc2047-encode-max-chars)
513 (rfc2047-encode-region (point-min) (point-max)))
514 (goto-char (point-min))
515 (while (search-forward "\n" nil t)
516 (delete-backward-char 1))
521 (defun nnrss-check-group (group server)
522 (let (file xml subject url extra changed author
523 date rss-ns rdf-ns content-ns dc-ns)
524 (if (and nnrss-use-local
525 (file-exists-p (setq file (expand-file-name
526 (nnrss-translate-file-chars
527 (concat group ".xml"))
529 (setq xml (nnrss-fetch file t))
530 (setq url (or (nth 2 (assoc group nnrss-server-data))
531 (second (assoc group nnrss-group-alist))))
538 (format "URL to search for %s: " group) "http://")))))
539 (let ((pair (assoc group nnrss-server-data)))
541 (setcdr (cdr pair) (list url))
542 (push (list group nnrss-group-max url) nnrss-server-data)))
544 (setq xml (nnrss-fetch url)))
546 ;; http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
547 ;; for more RSS namespaces.
548 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
549 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
550 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
551 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
552 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
553 (when (and (listp item)
554 (string= (concat rss-ns "item") (car item))
555 (if (setq url (nnrss-decode-entities-string
556 (nnrss-node-text rss-ns 'link (cddr item))))
557 (not (gethash url nnrss-group-hashtb))
558 (setq extra (or (nnrss-node-text content-ns 'encoded item)
559 (nnrss-node-text rss-ns 'description item)))
560 (not (gethash extra nnrss-group-hashtb))))
561 (setq subject (nnrss-node-text rss-ns 'title item))
562 (setq extra (or extra
563 (nnrss-node-text content-ns 'encoded item)
564 (nnrss-node-text rss-ns 'description item)))
565 (setq author (or (nnrss-node-text rss-ns 'author item)
566 (nnrss-node-text dc-ns 'creator item)
567 (nnrss-node-text dc-ns 'contributor item)))
568 (setq date (or (nnrss-node-text dc-ns 'date item)
569 (nnrss-node-text rss-ns 'pubDate item)
570 (message-make-date)))
573 (incf nnrss-group-max)
576 (and subject (nnrss-mime-encode-string subject))
577 (and author (nnrss-mime-encode-string author))
579 (and extra (nnrss-decode-entities-string extra)))
581 (puthash (or url extra) t nnrss-group-hashtb)
585 (nnrss-save-group-data group server)
586 (let ((pair (assoc group nnrss-server-data)))
588 (setcar (cdr pair) nnrss-group-max)
589 (push (list group nnrss-group-max) nnrss-server-data)))
590 (nnrss-save-server-data server))))
592 (defun nnrss-opml-import (opml-file)
593 "OPML subscriptions import.
594 Read the file and attempt to subscribe to each Feed in the file."
595 (interactive "fImport file: ")
597 (lambda (node) (gnus-group-make-rss-group
598 (cdr (assq 'xmlUrl (cadr node)))))
599 (nnrss-find-el 'outline
601 (find-file opml-file)
602 (xml-parse-region (point-min)
605 (defun nnrss-opml-export ()
606 "OPML subscription export.
607 Export subscriptions to a buffer in OPML Format."
609 (with-current-buffer (get-buffer-create "*OPML Export*")
610 (mm-set-buffer-file-coding-system 'utf-8)
611 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
612 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
613 "<opml version=\"1.1\">\n"
615 " <title>mySubscriptions</title>\n"
616 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
618 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
619 " <ownerName>" (user-full-name) "</ownerName>\n"
622 (dolist (sub nnrss-group-alist)
623 (insert " <outline text=\"" (car sub)
624 "\" xmlUrl=\"" (cadr sub) "\"/>\n"))
627 (pop-to-buffer "*OPML Export*")
628 (when (fboundp 'sgml-mode)
631 (defun nnrss-generate-download-script ()
632 "Generate a download script in the current buffer.
633 It is useful when `(setq nnrss-use-local t)'."
635 (insert "#!/bin/sh\n")
636 (insert "WGET=wget\n")
637 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
638 (dolist (elem nnrss-server-data)
639 (let ((url (or (nth 2 elem)
640 (second (assoc (car elem) nnrss-group-alist)))))
641 (insert "$WGET -q -O \"$RSSDIR\"/'"
642 (nnrss-translate-file-chars (concat (car elem) ".xml"))
645 (defun nnrss-translate-file-chars (name)
646 (let ((nnheader-file-name-translation-alist
647 (append nnheader-file-name-translation-alist '((?' . ?_)))))
648 (nnheader-translate-file-chars name)))
650 (defvar nnrss-moreover-url
651 "http://w.moreover.com/categories/category_list_rss.html"
652 "The url of moreover.com categories.")
654 (defun nnrss-snarf-moreover-categories ()
655 "Snarf RSS links from moreover.com."
657 (let (category name url changed)
659 (nnrss-insert nnrss-moreover-url)
660 (goto-char (point-min))
661 (while (re-search-forward
662 "<a name=\"\\([^\"]+\\)\">\\|<a href=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
664 (setq category (match-string 1))
665 (setq url (match-string 2)
666 name (mm-url-decode-entities-string
667 (rfc2231-decode-encoded-string
670 (setq name (concat category "." name)))
671 (unless (assoc name nnrss-server-data)
673 (push (list name 0 url) nnrss-server-data)))))
675 (nnrss-save-server-data ""))))
677 (defun nnrss-node-text (namespace local-name element)
678 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
680 (text (if (and node (listp node))
681 (nnrss-node-just-text node)
683 (cleaned-text (if text
684 (gnus-replace-in-string
685 (gnus-replace-in-string
686 text "^[\000-\037\177]+\\|^ +\\| +$" "")
688 (if (string-equal "" cleaned-text)
692 (defun nnrss-node-just-text (node)
693 (if (and node (listp node))
694 (mapconcat 'nnrss-node-just-text (cddr node) " ")
697 (defun nnrss-find-el (tag data &optional found-list)
698 "Find the all matching elements in the data.
699 Careful with this on large documents!"
703 (when (equal tag (car bit))
704 ;; Old xml.el may return a list of string.
705 (when (and (consp (caddr bit))
706 (stringp (caaddr bit)))
707 (setcar (cddr bit) (caaddr bit)))
711 (if (and (consp (car-safe (caddr bit)))
712 (not (stringp (caddr bit))))
720 tag (cddr bit))))))))
723 (defun nnrss-rsslink-p (el)
724 "Test if the element we are handed is an RSS autodiscovery link."
725 (and (eq (car-safe el) 'link)
726 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
727 (or (string-equal (cdr (assoc 'type (cadr el)))
728 "application/rss+xml")
729 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
731 (defun nnrss-get-rsslinks (data)
732 "Extract the <link> elements that are links to RSS from the parsed data."
735 (if (nnrss-rsslink-p el) el))
736 (nnrss-find-el 'link data))))
738 (defun nnrss-extract-hrefs (data)
739 "Recursively extract hrefs from a page's source.
740 DATA should be the output of `xml-parse-region' or
742 (mapcar (lambda (ahref)
743 (cdr (assoc 'href (cadr ahref))))
744 (nnrss-find-el 'a data)))
746 (defmacro nnrss-match-macro (base-uri item onsite-list offsite-list)
747 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
748 (not (string-match "://" ,item)))
749 (setq ,onsite-list (append ,onsite-list (list ,item))))
750 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
752 (defun nnrss-order-hrefs (base-uri hrefs)
753 "Given a list of hrefs, sort them using the following priorities:
754 1. links ending in .rss
755 2. links ending in .rdf
756 3. links ending in .xml
757 4. links containing the above
760 BASE-URI is used to determine the location of the links and
761 whether they are `offsite' or `onsite'."
762 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
763 rss-onsite-in rdf-onsite-in xml-onsite-in
764 rss-offsite-end rdf-offsite-end xml-offsite-end
765 rss-offsite-in rdf-offsite-in xml-offsite-in)
768 ((string-match "\\.rss$" href)
770 base-uri href rss-onsite-end rss-offsite-end))
771 ((string-match "\\.rdf$" href)
773 base-uri href rdf-onsite-end rdf-offsite-end))
774 ((string-match "\\.xml$" href)
776 base-uri href xml-onsite-end xml-offsite-end))
777 ((string-match "rss" href)
779 base-uri href rss-onsite-in rss-offsite-in))
780 ((string-match "rdf" href)
782 base-uri href rdf-onsite-in rdf-offsite-in))
783 ((string-match "xml" href)
785 base-uri href xml-onsite-in xml-offsite-in))))
787 rss-onsite-end rdf-onsite-end xml-onsite-end
788 rss-onsite-in rdf-onsite-in xml-onsite-in
789 rss-offsite-end rdf-offsite-end xml-offsite-end
790 rss-offsite-in rdf-offsite-in xml-offsite-in)))
792 (defun nnrss-discover-feed (url)
793 "Given a page, find an RSS feed using Mark Pilgrim's
794 `ultra-liberal rss locator' (http://diveintomark.org/2002/08/15.html)."
796 (let ((parsed-page (nnrss-fetch url)))
798 ;; 1. if this url is the rss, use it.
799 (if (nnrss-rss-p parsed-page)
800 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
801 (nnrss-rss-title-description rss-ns parsed-page url))
803 ;; 2. look for the <link rel="alternate"
804 ;; type="application/rss+xml" and use that if it is there.
805 (let ((links (nnrss-get-rsslinks parsed-page)))
807 (let* ((xml (nnrss-fetch
808 (cdr (assoc 'href (cadar links)))))
809 (rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")))
810 (nnrss-rss-title-description rss-ns xml (cdr (assoc 'href (cadar links)))))
812 ;; 3. look for links on the site in the following order:
813 ;; - onsite links ending in .rss, .rdf, or .xml
814 ;; - onsite links containing any of the above
815 ;; - offsite links ending in .rss, .rdf, or .xml
816 ;; - offsite links containing any of the above
817 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
818 (match-string 0 url)))
819 (hrefs (nnrss-order-hrefs
820 base-uri (nnrss-extract-hrefs parsed-page)))
822 (while (and (eq rss-link nil) (not (eq hrefs nil)))
823 (let ((href-data (nnrss-fetch (car hrefs))))
824 (if (nnrss-rss-p href-data)
825 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
826 (setq rss-link (nnrss-rss-title-description
827 rss-ns href-data (car hrefs))))
828 (setq hrefs (cdr hrefs)))))
829 (if rss-link rss-link
832 (nnrss-find-rss-via-syndic8 url))))))))
834 (defun nnrss-find-rss-via-syndic8 (url)
835 "Query syndic8 for the rss feeds it has for URL."
836 (if (not (locate-library "xml-rpc"))
838 (message "XML-RPC is not available... not checking Syndic8.")
841 (let ((feedid (xml-rpc-method-call
842 "http://www.syndic8.com/xmlrpc.php"
846 (let* ((feedinfo (xml-rpc-method-call
847 "http://www.syndic8.com/xmlrpc.php"
855 (cdr (assoc "status" listinfo))
858 (cdr (assoc "sitename" listinfo))
862 "sitename" listinfo)))
865 "dataurl" listinfo)))))))
867 (if (not (> (length urllist) 1))
869 (let ((completion-ignore-case t)
871 (mapcar (lambda (listinfo)
872 (cons (cdr (assoc "sitename" listinfo))
874 (cdr (assoc "feedid" listinfo)))))
878 "Multiple feeds found. Select one: "
879 selection nil t) urllist)))))))))
881 (defun nnrss-rss-p (data)
882 "Test if DATA is an RSS feed.
883 Simply ensures that the first element is rss or rdf."
884 (or (eq (caar data) 'rss)
885 (eq (caar data) 'rdf:RDF)))
887 (defun nnrss-rss-title-description (rss-namespace data url)
888 "Return the title of an RSS feed."
889 (if (nnrss-rss-p data)
890 (let ((description (intern (concat rss-namespace "description")))
891 (title (intern (concat rss-namespace "title")))
892 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
895 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
896 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
899 (defun nnrss-get-namespace-prefix (el uri)
900 "Given EL (containing a parsed element) and URI (containing a string
901 that gives the URI for which you want to retrieve the namespace
902 prefix), return the prefix."
903 (let* ((prefix (car (rassoc uri (cadar el))))
905 (split-string (symbol-name prefix) ":")))
906 (ns (cond ((eq (length nslist) 1) ; no prefix given
908 ((eq (length nslist) 2) ; extract prefix
910 (if (and ns (not (string= ns "")))
917 ;;; nnrss.el ends here
919 ;;; arch-tag: 12910c07-0cdf-44fb-8d2c-416ded64c267