1 ;;; nnrss.el --- interfacing with RSS
2 ;; Copyright (C) 2001, 2002, 2003, 2004 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))
39 (require 'eword-encode)
45 (eval '(require 'xml))
47 ;; Reload mm-util emulating macros for compiling.
49 (let ((features (delq 'mm-util (copy-sequence features))))
50 (load "nnheader" nil t)))
54 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
55 "Where nnrss will save its files.")
57 ;; (group max rss-url)
58 (defvoo nnrss-server-data nil)
60 ;; (num timestamp url subject author date extra)
61 (defvoo nnrss-group-data nil)
62 (defvoo nnrss-group-max 0)
63 (defvoo nnrss-group-min 1)
64 (defvoo nnrss-group nil)
65 (defvoo nnrss-group-hashtb (make-hash-table :test 'equal))
66 (defvoo nnrss-status-string "")
68 (defconst nnrss-version "nnrss 1.0")
70 (defvar nnrss-group-alist '()
71 "List of RSS addresses.")
73 (defvar nnrss-use-local nil)
75 (defvar nnrss-description-field 'X-Gnus-Description
76 "Field name used for DESCRIPTION.
77 To use the description in headers, put this name into `nnmail-extra-headers'.")
79 (defvar nnrss-url-field 'X-Gnus-Url
80 "Field name used for URL.
81 To use the description in headers, put this name into `nnmail-extra-headers'.")
83 (defvar nnrss-content-function nil
84 "A function which is called in `nnrss-request-article'.
85 The arguments are (ENTRY GROUP ARTICLE).
86 ENTRY is the record of the current headline. GROUP is the group name.
87 ARTICLE is the article number of the current headline.")
89 (defvar nnrss-file-coding-system nnheader-auto-save-coding-system
90 "Coding system used when reading and writing files.")
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 (nnrss-possibly-change-group group server)
158 (nnrss-check-group group server)
159 (nnheader-report 'nnrss "Opened group %s" group)
161 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
162 (prin1-to-string group)
165 (deffoo nnrss-close-group (group &optional server)
168 (defun nnrss-body-presentation-method ()
169 "Return a body presentation method used with MIME-View.
170 The return value will be `html' or `text'."
171 (in-calist-package 'mime-view)
172 (let ((default (cdr (assq 'body-presentation-method
173 (car (ctree-find-calist
174 mime-preview-condition
175 '((type . text) (subtype . t)))))))
176 (html (cdr (assq 'body-presentation-method
177 (car (ctree-find-calist
178 mime-preview-condition
179 '((type . text) (subtype . html))))))))
180 (if (or (not default)
186 (deffoo nnrss-request-article (article &optional group server buffer)
187 (setq group (nnrss-decode-group-name group))
188 (when (stringp article)
189 (setq article (if (string-match "\\`<\\([0-9]+\\)@" article)
190 (string-to-number (match-string 1 article))
192 (nnrss-possibly-change-group group server)
193 (let ((e (assq article nnrss-group-data))
194 (nntp-server-buffer (or buffer nntp-server-buffer))
198 (with-current-buffer nntp-server-buffer
201 (mm-with-unibyte-current-buffer
202 (insert "Newsgroups: "
203 (if (mm-coding-system-p 'utf-8)
204 (mm-encode-coding-string group 'utf-8)
208 (insert "Subject: " (nth 3 e) "\n"))
210 (insert "From: " (nth 4 e) "\n"))
212 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
213 (insert (format "Message-ID: <%d@%s.nnrss>\n"
215 (gnus-replace-in-string group "[\t\n ]+" "_")))
217 (let ((text (if (nth 6 e)
219 (delete "" (split-string (nth 6 e) "\n+"))
222 (mail-header-separator "")
223 mime-edit-insert-user-agent-field)
225 (if (eq 'html (nnrss-body-presentation-method))
227 (mime-edit-insert-text "html")
228 (insert "<html><head></head><body>\n")
232 (insert "<p><a href=\"" link "\">link</a></p>\n"))
233 (insert "</body></html>\n"))
234 (mime-edit-insert-text "plain")
239 (insert "\n" link "\n")))
241 (insert link "\n"))))
242 (mime-edit-translate-body)))
243 (when nnrss-content-function
244 (funcall nnrss-content-function e group article)))))
247 (nnheader-report 'nnrss err))
249 (nnheader-report 'nnrss "no such id: %d" article))
251 (nnheader-report 'nnrss "article %s retrieved" (car e))
252 ;; we return the article number.
253 (cons nnrss-group (car e))))))
255 (deffoo nnrss-request-list (&optional server)
256 (nnrss-possibly-change-group nil server)
257 (nnrss-generate-active)
260 (deffoo nnrss-open-server (server &optional defs connectionless)
261 (nnrss-read-server-data server)
262 (nnoo-change-server 'nnrss server defs)
265 (deffoo nnrss-request-expire-articles
266 (articles group &optional server force)
267 (setq group (nnrss-decode-group-name group))
268 (nnrss-possibly-change-group group server)
269 (let (e days not-expirable changed)
270 (dolist (art articles)
271 (if (and (setq e (assq art nnrss-group-data))
272 (nnmail-expired-article-p
274 (if (listp (setq days (nth 1 e))) days
275 (days-to-time (- days (time-to-days '(0 0)))))
277 (setq nnrss-group-data (delq e nnrss-group-data)
279 (push art not-expirable)))
281 (nnrss-save-group-data group server))
284 (deffoo nnrss-request-delete-group (group &optional force server)
285 (setq group (nnrss-decode-group-name group))
286 (nnrss-possibly-change-group group server)
287 (setq nnrss-server-data
288 (delq (assoc group nnrss-server-data) nnrss-server-data))
289 (nnrss-save-server-data server)
291 (delete-file (nnrss-make-filename group server)))
294 (deffoo nnrss-request-list-newsgroups (&optional server)
295 (nnrss-possibly-change-group nil server)
297 (set-buffer nntp-server-buffer)
299 (dolist (elem nnrss-group-alist)
301 (insert (car elem) "\t" (third elem) "\n"))))
304 (nnoo-define-skeleton nnrss)
306 ;;; Internal functions
307 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
309 (defun nnrss-get-encoding ()
310 "Return an encoding attribute specified in the current xml contents."
311 (goto-char (point-min))
313 (if (re-search-forward
314 "<\\?[^>]*encoding=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
316 (intern-soft (downcase (or (match-string-no-properties 1)
317 (match-string-no-properties 2))))
318 ;; The default encoding for xml.
321 (defun nnrss-fetch (url &optional local)
322 "Fetch URL and put it in a the expected Lisp structure."
323 (mm-with-unibyte-buffer
324 ;;some CVS versions of url.el need this to close the connection quickly
325 (let (cs xmlform htmlform)
326 ;; bit o' work necessary for w3 pre-cvs and post-cvs
328 (let ((coding-system-for-read 'binary))
329 (insert-file-contents url))
330 (let (;; FIXME: shouldn't binding `coding-system-for-read' be
331 ;; moved to `mm-url-insert'?
332 (coding-system-for-read 'binary)
333 ;; mm-url will load mm-util. d-e-m-c should be bound to
334 ;; t then, because of `mm-emacs-mule'.
335 (default-enable-multibyte-characters t))
336 (mm-url-insert url)))
337 (nnheader-remove-cr-followed-by-lf)
338 ;; Decode text according to the encoding attribute.
339 (when (setq cs (nnrss-get-encoding))
340 (mm-decode-coding-region (point-min) (point-max) cs)
341 (mm-enable-multibyte))
342 (goto-char (point-min))
344 ;; Because xml-parse-region can't deal with anything that isn't
345 ;; xml and w3-parse-buffer can't deal with some xml, we have to
346 ;; parse with xml-parse-region first and, if that fails, parse
347 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
348 ;; why w3-parse-buffer fails to parse some well-formed xml and
352 (setq xmlform (xml-parse-region (point-min) (point-max)))
355 (setq htmlform (caddar (w3-parse-buffer
359 nnrss: %s: Not valid XML %s and w3-parse doesn't work %s"
365 (defun nnrss-possibly-change-group (&optional group server)
367 (not (nnrss-server-opened server)))
368 (nnrss-open-server server))
369 (when (and group (not (equal group nnrss-group)))
370 (nnrss-read-group-data group server)
371 (setq nnrss-group group)))
373 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
375 (defun nnrss-generate-active ()
376 (when (y-or-n-p "Fetch extra categories? ")
377 (mapc 'funcall nnrss-extra-categories))
379 (set-buffer nntp-server-buffer)
381 (dolist (elem nnrss-group-alist)
382 (insert (prin1-to-string (car elem)) " 0 1 y\n"))
383 (dolist (elem nnrss-server-data)
384 (unless (assoc (car elem) nnrss-group-alist)
385 (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
389 (defun nnrss-read-server-data (server)
390 (setq nnrss-server-data nil)
391 (let ((file (nnrss-make-filename "nnrss" server)))
392 (when (file-exists-p file)
393 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
394 ;; file names. So, we use `insert-file-contents' instead.
395 (mm-with-multibyte-buffer
396 (let ((coding-system-for-read nnrss-file-coding-system)
397 (file-name-coding-system nnmail-pathname-coding-system))
398 (insert-file-contents file)
399 (eval-region (point-min) (point-max)))))))
401 (defun nnrss-save-server-data (server)
402 (gnus-make-directory nnrss-directory)
403 (let ((coding-system-for-write nnrss-file-coding-system)
404 (file-name-coding-system nnmail-pathname-coding-system))
405 (with-temp-file (nnrss-make-filename "nnrss" server)
406 (insert (format ";; -*- coding: %s; -*-\n"
407 nnrss-file-coding-system))
408 (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
409 (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
411 (defun nnrss-read-group-data (group server)
412 (setq nnrss-group-data nil)
413 (if (hash-table-p nnrss-group-hashtb)
414 (clrhash nnrss-group-hashtb)
415 (setq nnrss-group-hashtb (make-hash-table :test 'equal)))
416 (let ((pair (assoc group nnrss-server-data)))
417 (setq nnrss-group-max (or (cadr pair) 0))
418 (setq nnrss-group-min (+ nnrss-group-max 1)))
419 (let ((file (nnrss-make-filename group server)))
420 (when (file-exists-p file)
421 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
422 ;; file names. So, we use `insert-file-contents' instead.
423 (mm-with-multibyte-buffer
424 (let ((coding-system-for-read nnrss-file-coding-system)
425 (file-name-coding-system nnmail-pathname-coding-system))
426 (insert-file-contents file)
427 (eval-region (point-min) (point-max))))
428 (dolist (e nnrss-group-data)
429 (puthash (or (nth 2 e) (nth 6 e)) t nnrss-group-hashtb)
430 (when (and (car e) (> nnrss-group-min (car e)))
431 (setq nnrss-group-min (car e)))
432 (when (and (car e) (< nnrss-group-max (car e)))
433 (setq nnrss-group-max (car e)))))))
435 (defun nnrss-save-group-data (group server)
436 (gnus-make-directory nnrss-directory)
437 (let ((coding-system-for-write nnrss-file-coding-system)
438 (file-name-coding-system nnmail-pathname-coding-system))
439 (with-temp-file (nnrss-make-filename group server)
440 (insert (format ";; -*- coding: %s; -*-\n"
441 nnrss-file-coding-system))
442 (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
444 (defun nnrss-make-filename (name server)
446 (nnrss-translate-file-chars
449 (not (equal server ""))
455 (gnus-add-shutdown 'nnrss-close 'gnus)
457 (defun nnrss-close ()
458 "Clear internal nnrss variables."
459 (setq nnrss-group-data nil
460 nnrss-server-data nil
461 nnrss-group-hashtb nil
462 nnrss-group-alist nil))
466 (defun nnrss-no-cache (url)
469 (defun nnrss-insert-w3 (url)
470 (mm-with-unibyte-current-buffer
471 (mm-url-insert url)))
473 (defun nnrss-decode-entities-string (string)
475 (mm-with-multibyte-buffer
477 (mm-url-decode-entities-nbsp)
480 (defalias 'nnrss-insert 'nnrss-insert-w3)
482 (defun nnrss-mime-encode-string (string)
483 (mm-with-multibyte-buffer
485 (mm-url-decode-entities-nbsp)
486 (goto-char (point-min))
487 (while (re-search-forward "[\t\n ]+" nil t)
489 (goto-char (point-min))
490 (skip-chars-forward " ")
491 (delete-region (point-min) (point))
492 (goto-char (point-max))
493 (skip-chars-forward " ")
494 (delete-region (point) (point-max))
495 (eword-encode-string (buffer-string) (eval '(- -1 (lsh -1 -1))))))
499 (defun nnrss-check-group (group server)
500 (let (file xml subject url extra changed author
501 date rss-ns rdf-ns content-ns dc-ns)
502 (if (and nnrss-use-local
503 (file-exists-p (setq file (expand-file-name
504 (nnrss-translate-file-chars
505 (concat group ".xml"))
507 (setq xml (nnrss-fetch file t))
508 (setq url (or (nth 2 (assoc group nnrss-server-data))
509 (second (assoc group nnrss-group-alist))))
516 (format "URL to search for %s: " group) "http://")))))
517 (let ((pair (assoc group nnrss-server-data)))
519 (setcdr (cdr pair) (list url))
520 (push (list group nnrss-group-max url) nnrss-server-data)))
522 (setq xml (nnrss-fetch url)))
524 ;; http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
525 ;; for more RSS namespaces.
526 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
527 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
528 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
529 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
530 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
531 (when (and (listp item)
532 (string= (concat rss-ns "item") (car item))
533 (if (setq url (nnrss-decode-entities-string
534 (nnrss-node-text rss-ns 'link (cddr item))))
535 (not (gethash url nnrss-group-hashtb))
536 (setq extra (or (nnrss-node-text content-ns 'encoded item)
537 (nnrss-node-text rss-ns 'description item)))
538 (not (gethash extra nnrss-group-hashtb))))
539 (setq subject (nnrss-node-text rss-ns 'title item))
540 (setq extra (or extra
541 (nnrss-node-text content-ns 'encoded item)
542 (nnrss-node-text rss-ns 'description item)))
543 (setq author (or (nnrss-node-text rss-ns 'author item)
544 (nnrss-node-text dc-ns 'creator item)
545 (nnrss-node-text dc-ns 'contributor item)))
546 (setq date (or (nnrss-node-text dc-ns 'date item)
547 (nnrss-node-text rss-ns 'pubDate item)
548 (message-make-date)))
551 (incf nnrss-group-max)
554 (and subject (nnrss-mime-encode-string subject))
555 (and author (nnrss-mime-encode-string author))
557 (and extra (nnrss-decode-entities-string extra)))
559 (puthash (or url extra) t nnrss-group-hashtb)
563 (nnrss-save-group-data group server)
564 (let ((pair (assoc group nnrss-server-data)))
566 (setcar (cdr pair) nnrss-group-max)
567 (push (list group nnrss-group-max) nnrss-server-data)))
568 (nnrss-save-server-data server))))
570 (defun nnrss-opml-import (opml-file)
571 "OPML subscriptions import.
572 Read the file and attempt to subscribe to each Feed in the file."
573 (interactive "fImport file: ")
575 (lambda (node) (gnus-group-make-rss-group
576 (cdr (assq 'xmlUrl (cadr node)))))
577 (nnrss-find-el 'outline
579 (find-file opml-file)
580 (xml-parse-region (point-min)
583 (defun nnrss-opml-export ()
584 "OPML subscription export.
585 Export subscriptions to a buffer in OPML Format."
587 (with-current-buffer (get-buffer-create "*OPML Export*")
588 (mm-set-buffer-file-coding-system 'utf-8)
590 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
591 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
592 "<opml version=\"1.1\">\n"
594 " <title>mySubscriptions</title>\n"
595 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
597 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
598 " <ownerName>" (user-full-name) "</ownerName>\n"
603 " <outline text=\"" (car sub) "\" xmlUrl=\""
604 (cadr sub) "\"/>\n")))
609 (pop-to-buffer "*OPML Export*")
610 (when (fboundp 'sgml-mode)
613 (defun nnrss-generate-download-script ()
614 "Generate a download script in the current buffer.
615 It is useful when `(setq nnrss-use-local t)'."
617 (insert "#!/bin/sh\n")
618 (insert "WGET=wget\n")
619 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
620 (dolist (elem nnrss-server-data)
621 (let ((url (or (nth 2 elem)
622 (second (assoc (car elem) nnrss-group-alist)))))
623 (insert "$WGET -q -O \"$RSSDIR\"/'"
624 (nnrss-translate-file-chars (concat (car elem) ".xml"))
627 (defun nnrss-translate-file-chars (name)
628 (let ((nnheader-file-name-translation-alist
629 (append nnheader-file-name-translation-alist '((?' . ?_)))))
630 (nnheader-translate-file-chars name)))
632 (defvar nnrss-moreover-url
633 "http://w.moreover.com/categories/category_list_rss.html"
634 "The url of moreover.com categories.")
636 (defun nnrss-snarf-moreover-categories ()
637 "Snarf RSS links from moreover.com."
639 (let (category name url changed)
641 (nnrss-insert nnrss-moreover-url)
642 (goto-char (point-min))
643 (while (re-search-forward
644 "<a name=\"\\([^\"]+\\)\">\\|<a href=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
646 (setq category (match-string 1))
647 (setq url (match-string 2)
648 name (mm-url-decode-entities-string
649 (cadr (mime-decode-parameters
650 (list "c*" (match-string 3))))))
652 (setq name (concat category "." name)))
653 (unless (assoc name nnrss-server-data)
655 (push (list name 0 url) nnrss-server-data)))))
657 (nnrss-save-server-data ""))))
659 (defun nnrss-node-text (namespace local-name element)
660 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
662 (text (if (and node (listp node))
663 (nnrss-node-just-text node)
665 (cleaned-text (if text (gnus-replace-in-string
666 text "^[\000-\037\177]+\\|^ +\\| +$" ""))))
667 (if (string-equal "" cleaned-text)
671 (defun nnrss-node-just-text (node)
672 (if (and node (listp node))
673 (mapconcat 'nnrss-node-just-text (cddr node) " ")
676 (defun nnrss-find-el (tag data &optional found-list)
677 "Find the all matching elements in the data.
678 Careful with this on large documents!"
682 (when (equal tag (car bit))
683 ;; Old xml.el may return a list of string.
684 (when (consp (caddr bit))
685 (setcar (cddr bit) (caaddr bit)))
689 (if (and (listp (car-safe (caddr bit)))
690 (not (stringp (caddr bit))))
702 (defun nnrss-rsslink-p (el)
703 "Test if the element we are handed is an RSS autodiscovery link."
704 (and (eq (car-safe el) 'link)
705 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
706 (or (string-equal (cdr (assoc 'type (cadr el)))
707 "application/rss+xml")
708 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
710 (defun nnrss-get-rsslinks (data)
711 "Extract the <link> elements that are links to RSS from the parsed data."
714 (if (nnrss-rsslink-p el) el))
715 (nnrss-find-el 'link data))))
717 (defun nnrss-extract-hrefs (data)
718 "Recursively extract hrefs from a page's source.
719 DATA should be the output of `xml-parse-region' or
721 (mapcar (lambda (ahref)
722 (cdr (assoc 'href (cadr ahref))))
723 (nnrss-find-el 'a data)))
725 (defmacro nnrss-match-macro (base-uri item
726 onsite-list offsite-list)
727 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
728 (not (string-match "://" ,item)))
729 (setq ,onsite-list (append ,onsite-list (list ,item))))
730 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
732 (defun nnrss-order-hrefs (base-uri hrefs)
733 "Given a list of hrefs, sort them using the following priorities:
734 1. links ending in .rss
735 2. links ending in .rdf
736 3. links ending in .xml
737 4. links containing the above
740 BASE-URI is used to determine the location of the links and
741 whether they are `offsite' or `onsite'."
742 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
743 rss-onsite-in rdf-onsite-in xml-onsite-in
744 rss-offsite-end rdf-offsite-end xml-offsite-end
745 rss-offsite-in rdf-offsite-in xml-offsite-in)
747 (if (not (null href))
748 (cond ((string-match "\\.rss$" href)
750 base-uri href rss-onsite-end rss-offsite-end))
751 ((string-match "\\.rdf$" href)
753 base-uri href rdf-onsite-end rdf-offsite-end))
754 ((string-match "\\.xml$" href)
756 base-uri href xml-onsite-end xml-offsite-end))
757 ((string-match "rss" href)
759 base-uri href rss-onsite-in rss-offsite-in))
760 ((string-match "rdf" href)
762 base-uri href rdf-onsite-in rdf-offsite-in))
763 ((string-match "xml" href)
765 base-uri href xml-onsite-in xml-offsite-in)))))
768 rss-onsite-end rdf-onsite-end xml-onsite-end
769 rss-onsite-in rdf-onsite-in xml-onsite-in
770 rss-offsite-end rdf-offsite-end xml-offsite-end
771 rss-offsite-in rdf-offsite-in xml-offsite-in)))
773 (defun nnrss-discover-feed (url)
774 "Given a page, find an RSS feed using Mark Pilgrim's
775 `ultra-liberal rss locator' (http://diveintomark.org/2002/08/15.html)."
777 (let ((parsed-page (nnrss-fetch url)))
779 ;; 1. if this url is the rss, use it.
780 (if (nnrss-rss-p parsed-page)
781 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
782 (nnrss-rss-title-description rss-ns parsed-page url))
784 ;; 2. look for the <link rel="alternate"
785 ;; type="application/rss+xml" and use that if it is there.
786 (let ((links (nnrss-get-rsslinks parsed-page)))
788 (let* ((xml (nnrss-fetch
789 (cdr (assoc 'href (cadar links)))))
790 (rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")))
791 (nnrss-rss-title-description rss-ns xml (cdr (assoc 'href (cadar links)))))
793 ;; 3. look for links on the site in the following order:
794 ;; - onsite links ending in .rss, .rdf, or .xml
795 ;; - onsite links containing any of the above
796 ;; - offsite links ending in .rss, .rdf, or .xml
797 ;; - offsite links containing any of the above
798 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
799 (match-string 0 url)))
800 (hrefs (nnrss-order-hrefs
801 base-uri (nnrss-extract-hrefs parsed-page)))
803 (while (and (eq rss-link nil) (not (eq hrefs nil)))
804 (let ((href-data (nnrss-fetch (car hrefs))))
805 (if (nnrss-rss-p href-data)
806 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
807 (setq rss-link (nnrss-rss-title-description
808 rss-ns href-data (car hrefs))))
809 (setq hrefs (cdr hrefs)))))
810 (if rss-link rss-link
813 (nnrss-find-rss-via-syndic8 url))))))))
815 (defun nnrss-find-rss-via-syndic8 (url)
816 "Query syndic8 for the rss feeds it has for URL."
817 (if (not (locate-library "xml-rpc"))
819 (message "XML-RPC is not available... not checking Syndic8.")
822 (let ((feedid (xml-rpc-method-call
823 "http://www.syndic8.com/xmlrpc.php"
827 (let* ((feedinfo (xml-rpc-method-call
828 "http://www.syndic8.com/xmlrpc.php"
836 (cdr (assoc "status" listinfo))
839 (cdr (assoc "sitename" listinfo))
843 "sitename" listinfo)))
846 "dataurl" listinfo)))))))
848 (if (not (> (length urllist) 1))
850 (let ((completion-ignore-case t)
852 (mapcar (lambda (listinfo)
853 (cons (cdr (assoc "sitename" listinfo))
855 (cdr (assoc "feedid" listinfo)))))
859 "Multiple feeds found. Select one: "
860 selection nil t) urllist)))))))))
862 (defun nnrss-rss-p (data)
863 "Test if DATA is an RSS feed.
864 Simply ensures that the first element is rss or rdf."
865 (or (eq (caar data) 'rss)
866 (eq (caar data) 'rdf:RDF)))
868 (defun nnrss-rss-title-description (rss-namespace data url)
869 "Return the title of an RSS feed."
870 (if (nnrss-rss-p data)
871 (let ((description (intern (concat rss-namespace "description")))
872 (title (intern (concat rss-namespace "title")))
873 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
876 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
877 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
880 (defun nnrss-get-namespace-prefix (el uri)
881 "Given EL (containing a parsed element) and URI (containing a string
882 that gives the URI for which you want to retrieve the namespace
883 prefix), return the prefix."
884 (let* ((prefix (car (rassoc uri (cadar el))))
886 (split-string (symbol-name prefix) ":")))
887 (ns (cond ((eq (length nslist) 1) ; no prefix given
889 ((eq (length nslist) 2) ; extract prefix
891 (if (and ns (not (string= ns "")))
898 ;;; nnrss.el ends here