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-verbose t
70 "Write messages when requesting group.")
72 (defvar nnrss-description-field 'X-Gnus-Description
73 "Field name used for DESCRIPTION.
74 To use the description in headers, put this name into `nnmail-extra-headers'.")
76 (defvar nnrss-url-field 'X-Gnus-Url
77 "Field name used for URL.
78 To use the description in headers, put this name into `nnmail-extra-headers'.")
80 (defvar nnrss-content-function nil
81 "A function which is called in `nnrss-request-article'.
82 The arguments are (ENTRY GROUP ARTICLE).
83 ENTRY is the record of the current headline. GROUP is the group name.
84 ARTICLE is the article number of the current headline.")
86 (defvar nnrss-file-coding-system mm-universal-coding-system
87 "Coding system used when reading and writing files.")
89 (nnoo-define-basics nnrss)
91 ;;; Interface functions
93 (defsubst nnrss-format-string (string)
94 (gnus-replace-in-string string " *\n *" " "))
96 (defun nnrss-decode-group-name (group)
97 (if (and group (mm-coding-system-p 'utf-8))
98 (setq group (mm-decode-coding-string group 'utf-8))
101 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
102 (setq group (nnrss-decode-group-name group))
103 (nnrss-possibly-change-group group server)
106 (set-buffer nntp-server-buffer)
108 (dolist (article articles)
109 (if (setq e (assq article nnrss-group-data))
110 (insert (number-to-string (car e)) "\t" ;; number
115 (or (nth 4 e) "(nobody)")
121 (format "<%d@%s.nnrss>" (car e) group)
132 (memq nnrss-description-field
133 nnmail-extra-headers))
134 (concat (symbol-name nnrss-description-field)
136 (nnrss-format-string (nth 6 e))
140 (memq nnrss-url-field
141 nnmail-extra-headers))
142 (concat (symbol-name nnrss-url-field)
144 (nnrss-format-string (nth 2 e))
150 (deffoo nnrss-request-group (group &optional server dont-check)
152 (message (concat "nnrss requesting " group "...")))
153 (setq group (nnrss-decode-group-name group))
154 (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 (message (concat "nnrss done with " group ".")))))
167 (deffoo nnrss-close-group (group &optional server)
170 (deffoo nnrss-request-article (article &optional group server buffer)
171 (setq group (nnrss-decode-group-name group))
172 (when (stringp article)
173 (setq article (if (string-match "\\`<\\([0-9]+\\)@" article)
174 (string-to-number (match-string 1 article))
176 (nnrss-possibly-change-group group server)
177 (let ((e (assq article nnrss-group-data))
178 (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"))
193 (let ((text (if (nth 6 e)
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)
207 (insert "<#multipart type=alternative>\n"
208 "<#part type=\"text/plain\">\n")
213 (insert "\n" link "\n")))
217 "<#part type=\"text/html\">\n"
218 "<html><head></head><body>\n")
222 (insert "<p><a href=\"" link "\">link</a></p>\n"))
223 (insert "</body></html>\n"
227 (goto-char (point-min))
228 (search-forward "\n\n")
230 (insert (format "Message-ID: <%d@%s.nnrss>\n"
232 (let ((rfc2047-encoding-type 'mime)
233 rfc2047-encode-max-chars)
234 (rfc2047-encode-string
235 (gnus-replace-in-string group "[\t\n ]+" "_")))))
236 (when nnrss-content-function
237 (funcall nnrss-content-function e group article)))))
240 (nnheader-report 'nnrss err))
242 (nnheader-report 'nnrss "no such id: %d" article))
244 (nnheader-report 'nnrss "article %s retrieved" (car e))
245 ;; we return the article number.
246 (cons nnrss-group (car e))))))
248 (deffoo nnrss-request-list (&optional server)
249 (nnrss-possibly-change-group nil server)
250 (nnrss-generate-active)
253 (deffoo nnrss-open-server (server &optional defs connectionless)
254 (nnrss-read-server-data server)
255 (nnoo-change-server 'nnrss server defs)
258 (deffoo nnrss-request-expire-articles
259 (articles group &optional server force)
260 (setq group (nnrss-decode-group-name group))
261 (nnrss-possibly-change-group group server)
262 (let (e days not-expirable changed)
263 (dolist (art articles)
264 (if (and (setq e (assq art nnrss-group-data))
265 (nnmail-expired-article-p
267 (if (listp (setq days (nth 1 e))) days
268 (days-to-time (- days (time-to-days '(0 0)))))
270 (setq nnrss-group-data (delq e nnrss-group-data)
272 (push art not-expirable)))
274 (nnrss-save-group-data group server))
277 (deffoo nnrss-request-delete-group (group &optional force server)
278 (setq group (nnrss-decode-group-name group))
279 (nnrss-possibly-change-group group server)
281 ;; There may be two or more entries in `nnrss-group-alist' since
282 ;; this function didn't delete them formerly.
283 (while (setq elem (assoc group nnrss-group-alist))
284 (setq nnrss-group-alist (delq elem nnrss-group-alist))))
285 (setq nnrss-server-data
286 (delq (assoc group nnrss-server-data) nnrss-server-data))
287 (nnrss-save-server-data server)
289 (delete-file (nnrss-make-filename group server)))
292 (deffoo nnrss-request-list-newsgroups (&optional server)
293 (nnrss-possibly-change-group nil server)
295 (set-buffer nntp-server-buffer)
297 (dolist (elem nnrss-group-alist)
299 (insert (car elem) "\t" (third elem) "\n"))))
302 (nnoo-define-skeleton nnrss)
304 ;;; Internal functions
305 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
307 (defun nnrss-get-encoding ()
308 "Return an encoding attribute specified in the current xml contents."
309 (goto-char (point-min))
311 (if (re-search-forward
312 "<\\?[^>]*encoding=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
314 (intern-soft (downcase (or (match-string-no-properties 1)
315 (match-string-no-properties 2))))
316 ;; The default encoding for xml.
319 (defun nnrss-fetch (url &optional local)
320 "Fetch URL and put it in a the expected Lisp structure."
321 (mm-with-unibyte-buffer
322 ;;some CVS versions of url.el need this to close the connection quickly
323 (let (cs xmlform htmlform)
324 ;; bit o' work necessary for w3 pre-cvs and post-cvs
326 (let ((coding-system-for-read 'binary))
327 (insert-file-contents url))
328 ;; FIXME: shouldn't binding `coding-system-for-read' be moved
329 ;; to `mm-url-insert'?
330 (let ((coding-system-for-read 'binary))
331 (mm-url-insert url)))
332 (nnheader-remove-cr-followed-by-lf)
333 ;; Decode text according to the encoding attribute.
334 (when (setq cs (nnrss-get-encoding))
335 (mm-decode-coding-region (point-min) (point-max) cs)
336 (mm-enable-multibyte))
337 (goto-char (point-min))
339 ;; Because xml-parse-region can't deal with anything that isn't
340 ;; xml and w3-parse-buffer can't deal with some xml, we have to
341 ;; parse with xml-parse-region first and, if that fails, parse
342 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
343 ;; why w3-parse-buffer fails to parse some well-formed xml and
347 (setq xmlform (xml-parse-region (point-min) (point-max)))
350 (setq htmlform (caddar (w3-parse-buffer
354 nnrss: %s: Not valid XML %s and w3-parse doesn't work %s"
360 (defun nnrss-possibly-change-group (&optional group server)
362 (not (nnrss-server-opened server)))
363 (nnrss-open-server server))
364 (when (and group (not (equal group nnrss-group)))
365 (nnrss-read-group-data group server)
366 (setq nnrss-group group)))
368 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
370 (defun nnrss-generate-active ()
371 (when (y-or-n-p "Fetch extra categories? ")
372 (mapc 'funcall nnrss-extra-categories))
374 (set-buffer nntp-server-buffer)
376 (dolist (elem nnrss-group-alist)
377 (insert (prin1-to-string (car elem)) " 0 1 y\n"))
378 (dolist (elem nnrss-server-data)
379 (unless (assoc (car elem) nnrss-group-alist)
380 (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
384 (defun nnrss-read-server-data (server)
385 (setq nnrss-server-data nil)
386 (let ((file (nnrss-make-filename "nnrss" server)))
387 (when (file-exists-p file)
388 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
389 ;; file names. So, we use `insert-file-contents' instead.
390 (mm-with-multibyte-buffer
391 (let ((coding-system-for-read nnrss-file-coding-system)
392 (file-name-coding-system nnmail-pathname-coding-system))
393 (insert-file-contents file)
394 (eval-region (point-min) (point-max)))))))
396 (defun nnrss-save-server-data (server)
397 (gnus-make-directory nnrss-directory)
398 (let ((coding-system-for-write nnrss-file-coding-system)
399 (file-name-coding-system nnmail-pathname-coding-system))
400 (with-temp-file (nnrss-make-filename "nnrss" server)
401 (insert (format ";; -*- coding: %s; -*-\n"
402 nnrss-file-coding-system))
403 (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
405 (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
407 (defun nnrss-read-group-data (group server)
408 (setq nnrss-group-data nil)
409 (if (hash-table-p nnrss-group-hashtb)
410 (clrhash nnrss-group-hashtb)
411 (setq nnrss-group-hashtb (make-hash-table :test 'equal)))
412 (let ((pair (assoc group nnrss-server-data)))
413 (setq nnrss-group-max (or (cadr pair) 0))
414 (setq nnrss-group-min (+ nnrss-group-max 1)))
415 (let ((file (nnrss-make-filename group server)))
416 (when (file-exists-p file)
417 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
418 ;; file names. So, we use `insert-file-contents' instead.
419 (mm-with-multibyte-buffer
420 (let ((coding-system-for-read nnrss-file-coding-system)
421 (file-name-coding-system nnmail-pathname-coding-system))
422 (insert-file-contents file)
423 (eval-region (point-min) (point-max))))
424 (dolist (e nnrss-group-data)
425 (puthash (or (nth 2 e) (nth 6 e)) t nnrss-group-hashtb)
426 (when (and (car e) (> nnrss-group-min (car e)))
427 (setq nnrss-group-min (car e)))
428 (when (and (car e) (< nnrss-group-max (car e)))
429 (setq nnrss-group-max (car e)))))))
431 (defun nnrss-save-group-data (group server)
432 (gnus-make-directory nnrss-directory)
433 (let ((coding-system-for-write nnrss-file-coding-system)
434 (file-name-coding-system nnmail-pathname-coding-system))
435 (with-temp-file (nnrss-make-filename group server)
436 (insert (format ";; -*- coding: %s; -*-\n"
437 nnrss-file-coding-system))
438 (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
440 (defun nnrss-make-filename (name server)
442 (nnrss-translate-file-chars
445 (not (equal server ""))
451 (gnus-add-shutdown 'nnrss-close 'gnus)
453 (defun nnrss-close ()
454 "Clear internal nnrss variables."
455 (setq nnrss-group-data nil
456 nnrss-server-data nil
457 nnrss-group-hashtb nil
458 nnrss-group-alist nil))
462 (defun nnrss-no-cache (url)
465 (defun nnrss-insert-w3 (url)
466 (mm-with-unibyte-current-buffer
467 (mm-url-insert url)))
469 (defun nnrss-decode-entities-string (string)
471 (mm-with-multibyte-buffer
473 (mm-url-decode-entities-nbsp)
476 (defalias 'nnrss-insert 'nnrss-insert-w3)
478 (defun nnrss-mime-encode-string (string)
479 (mm-with-multibyte-buffer
481 (mm-url-decode-entities-nbsp)
482 (goto-char (point-min))
483 (while (re-search-forward "[\t\n ]+" nil t)
485 (goto-char (point-min))
486 (skip-chars-forward " ")
487 (delete-region (point-min) (point))
488 (goto-char (point-max))
489 (skip-chars-forward " ")
490 (delete-region (point) (point-max))
491 (let ((rfc2047-encoding-type 'mime)
492 rfc2047-encode-max-chars)
493 (rfc2047-encode-region (point-min) (point-max)))
494 (goto-char (point-min))
495 (while (search-forward "\n" nil t)
496 (delete-backward-char 1))
501 (defun nnrss-check-group (group server)
502 (let (file xml subject url extra changed author
503 date rss-ns rdf-ns content-ns dc-ns)
504 (if (and nnrss-use-local
505 (file-exists-p (setq file (expand-file-name
506 (nnrss-translate-file-chars
507 (concat group ".xml"))
509 (setq xml (nnrss-fetch file t))
510 (setq url (or (nth 2 (assoc group nnrss-server-data))
511 (second (assoc group nnrss-group-alist))))
518 (format "URL to search for %s: " group) "http://")))))
519 (let ((pair (assoc group nnrss-server-data)))
521 (setcdr (cdr pair) (list url))
522 (push (list group nnrss-group-max url) nnrss-server-data)))
524 (setq xml (nnrss-fetch url)))
526 ;; http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
527 ;; for more RSS namespaces.
528 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
529 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
530 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
531 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
532 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
533 (when (and (listp item)
534 (string= (concat rss-ns "item") (car item))
535 (if (setq url (nnrss-decode-entities-string
536 (nnrss-node-text rss-ns 'link (cddr item))))
537 (not (gethash url nnrss-group-hashtb))
538 (setq extra (or (nnrss-node-text content-ns 'encoded item)
539 (nnrss-node-text rss-ns 'description item)))
540 (not (gethash extra nnrss-group-hashtb))))
541 (setq subject (nnrss-node-text rss-ns 'title item))
542 (setq extra (or extra
543 (nnrss-node-text content-ns 'encoded item)
544 (nnrss-node-text rss-ns 'description item)))
545 (setq author (or (nnrss-node-text rss-ns 'author item)
546 (nnrss-node-text dc-ns 'creator item)
547 (nnrss-node-text dc-ns 'contributor item)))
548 (setq date (or (nnrss-node-text dc-ns 'date item)
549 (nnrss-node-text rss-ns 'pubDate item)
550 (message-make-date)))
553 (incf nnrss-group-max)
556 (and subject (nnrss-mime-encode-string subject))
557 (and author (nnrss-mime-encode-string author))
559 (and extra (nnrss-decode-entities-string extra)))
561 (puthash (or url extra) t nnrss-group-hashtb)
565 (nnrss-save-group-data group server)
566 (let ((pair (assoc group nnrss-server-data)))
568 (setcar (cdr pair) nnrss-group-max)
569 (push (list group nnrss-group-max) nnrss-server-data)))
570 (nnrss-save-server-data server))))
572 (defun nnrss-opml-import (opml-file)
573 "OPML subscriptions import.
574 Read the file and attempt to subscribe to each Feed in the file."
575 (interactive "fImport file: ")
577 (lambda (node) (gnus-group-make-rss-group
578 (cdr (assq 'xmlUrl (cadr node)))))
579 (nnrss-find-el 'outline
581 (find-file opml-file)
582 (xml-parse-region (point-min)
585 (defun nnrss-opml-export ()
586 "OPML subscription export.
587 Export subscriptions to a buffer in OPML Format."
589 (with-current-buffer (get-buffer-create "*OPML Export*")
590 (mm-set-buffer-file-coding-system 'utf-8)
592 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
593 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
594 "<opml version=\"1.1\">\n"
596 " <title>mySubscriptions</title>\n"
597 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
599 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
600 " <ownerName>" (user-full-name) "</ownerName>\n"
605 " <outline text=\"" (car sub) "\" xmlUrl=\""
606 (cadr sub) "\"/>\n")))
611 (pop-to-buffer "*OPML Export*")
612 (when (fboundp 'sgml-mode)
615 (defun nnrss-generate-download-script ()
616 "Generate a download script in the current buffer.
617 It is useful when `(setq nnrss-use-local t)'."
619 (insert "#!/bin/sh\n")
620 (insert "WGET=wget\n")
621 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
622 (dolist (elem nnrss-server-data)
623 (let ((url (or (nth 2 elem)
624 (second (assoc (car elem) nnrss-group-alist)))))
625 (insert "$WGET -q -O \"$RSSDIR\"/'"
626 (nnrss-translate-file-chars (concat (car elem) ".xml"))
629 (defun nnrss-translate-file-chars (name)
630 (let ((nnheader-file-name-translation-alist
631 (append nnheader-file-name-translation-alist '((?' . ?_)))))
632 (nnheader-translate-file-chars name)))
634 (defvar nnrss-moreover-url
635 "http://w.moreover.com/categories/category_list_rss.html"
636 "The url of moreover.com categories.")
638 (defun nnrss-snarf-moreover-categories ()
639 "Snarf RSS links from moreover.com."
641 (let (category name url changed)
643 (nnrss-insert nnrss-moreover-url)
644 (goto-char (point-min))
645 (while (re-search-forward
646 "<a name=\"\\([^\"]+\\)\">\\|<a href=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
648 (setq category (match-string 1))
649 (setq url (match-string 2)
650 name (mm-url-decode-entities-string
651 (rfc2231-decode-encoded-string
654 (setq name (concat category "." name)))
655 (unless (assoc name nnrss-server-data)
657 (push (list name 0 url) nnrss-server-data)))))
659 (nnrss-save-server-data ""))))
661 (defun nnrss-node-text (namespace local-name element)
662 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
664 (text (if (and node (listp node))
665 (nnrss-node-just-text node)
667 (cleaned-text (if text (gnus-replace-in-string
668 text "^[\000-\037\177]+\\|^ +\\| +$" ""))))
669 (if (string-equal "" cleaned-text)
673 (defun nnrss-node-just-text (node)
674 (if (and node (listp node))
675 (mapconcat 'nnrss-node-just-text (cddr node) " ")
678 (defun nnrss-find-el (tag data &optional found-list)
679 "Find the all matching elements in the data.
680 Careful with this on large documents!"
684 (when (equal tag (car bit))
685 ;; Old xml.el may return a list of string.
686 (when (and (consp (caddr bit))
687 (stringp (caaddr bit)))
688 (setcar (cddr bit) (caaddr bit)))
692 (if (and (listp (car-safe (caddr bit)))
693 (not (stringp (caddr bit))))
705 (defun nnrss-rsslink-p (el)
706 "Test if the element we are handed is an RSS autodiscovery link."
707 (and (eq (car-safe el) 'link)
708 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
709 (or (string-equal (cdr (assoc 'type (cadr el)))
710 "application/rss+xml")
711 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
713 (defun nnrss-get-rsslinks (data)
714 "Extract the <link> elements that are links to RSS from the parsed data."
717 (if (nnrss-rsslink-p el) el))
718 (nnrss-find-el 'link data))))
720 (defun nnrss-extract-hrefs (data)
721 "Recursively extract hrefs from a page's source.
722 DATA should be the output of `xml-parse-region' or
724 (mapcar (lambda (ahref)
725 (cdr (assoc 'href (cadr ahref))))
726 (nnrss-find-el 'a data)))
728 (defmacro nnrss-match-macro (base-uri item
729 onsite-list offsite-list)
730 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
731 (not (string-match "://" ,item)))
732 (setq ,onsite-list (append ,onsite-list (list ,item))))
733 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
735 (defun nnrss-order-hrefs (base-uri hrefs)
736 "Given a list of hrefs, sort them using the following priorities:
737 1. links ending in .rss
738 2. links ending in .rdf
739 3. links ending in .xml
740 4. links containing the above
743 BASE-URI is used to determine the location of the links and
744 whether they are `offsite' or `onsite'."
745 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
746 rss-onsite-in rdf-onsite-in xml-onsite-in
747 rss-offsite-end rdf-offsite-end xml-offsite-end
748 rss-offsite-in rdf-offsite-in xml-offsite-in)
750 (if (not (null href))
751 (cond ((string-match "\\.rss$" href)
753 base-uri href rss-onsite-end rss-offsite-end))
754 ((string-match "\\.rdf$" href)
756 base-uri href rdf-onsite-end rdf-offsite-end))
757 ((string-match "\\.xml$" href)
759 base-uri href xml-onsite-end xml-offsite-end))
760 ((string-match "rss" href)
762 base-uri href rss-onsite-in rss-offsite-in))
763 ((string-match "rdf" href)
765 base-uri href rdf-onsite-in rdf-offsite-in))
766 ((string-match "xml" href)
768 base-uri href xml-onsite-in xml-offsite-in)))))
771 rss-onsite-end rdf-onsite-end xml-onsite-end
772 rss-onsite-in rdf-onsite-in xml-onsite-in
773 rss-offsite-end rdf-offsite-end xml-offsite-end
774 rss-offsite-in rdf-offsite-in xml-offsite-in)))
776 (defun nnrss-discover-feed (url)
777 "Given a page, find an RSS feed using Mark Pilgrim's
778 `ultra-liberal rss locator' (http://diveintomark.org/2002/08/15.html)."
780 (let ((parsed-page (nnrss-fetch url)))
782 ;; 1. if this url is the rss, use it.
783 (if (nnrss-rss-p parsed-page)
784 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
785 (nnrss-rss-title-description rss-ns parsed-page url))
787 ;; 2. look for the <link rel="alternate"
788 ;; type="application/rss+xml" and use that if it is there.
789 (let ((links (nnrss-get-rsslinks parsed-page)))
791 (let* ((xml (nnrss-fetch
792 (cdr (assoc 'href (cadar links)))))
793 (rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")))
794 (nnrss-rss-title-description rss-ns xml (cdr (assoc 'href (cadar links)))))
796 ;; 3. look for links on the site in the following order:
797 ;; - onsite links ending in .rss, .rdf, or .xml
798 ;; - onsite links containing any of the above
799 ;; - offsite links ending in .rss, .rdf, or .xml
800 ;; - offsite links containing any of the above
801 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
802 (match-string 0 url)))
803 (hrefs (nnrss-order-hrefs
804 base-uri (nnrss-extract-hrefs parsed-page)))
806 (while (and (eq rss-link nil) (not (eq hrefs nil)))
807 (let ((href-data (nnrss-fetch (car hrefs))))
808 (if (nnrss-rss-p href-data)
809 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
810 (setq rss-link (nnrss-rss-title-description
811 rss-ns href-data (car hrefs))))
812 (setq hrefs (cdr hrefs)))))
813 (if rss-link rss-link
816 (nnrss-find-rss-via-syndic8 url))))))))
818 (defun nnrss-find-rss-via-syndic8 (url)
819 "Query syndic8 for the rss feeds it has for URL."
820 (if (not (locate-library "xml-rpc"))
822 (message "XML-RPC is not available... not checking Syndic8.")
825 (let ((feedid (xml-rpc-method-call
826 "http://www.syndic8.com/xmlrpc.php"
830 (let* ((feedinfo (xml-rpc-method-call
831 "http://www.syndic8.com/xmlrpc.php"
839 (cdr (assoc "status" listinfo))
842 (cdr (assoc "sitename" listinfo))
846 "sitename" listinfo)))
849 "dataurl" listinfo)))))))
851 (if (not (> (length urllist) 1))
853 (let ((completion-ignore-case t)
855 (mapcar (lambda (listinfo)
856 (cons (cdr (assoc "sitename" listinfo))
858 (cdr (assoc "feedid" listinfo)))))
862 "Multiple feeds found. Select one: "
863 selection nil t) urllist)))))))))
865 (defun nnrss-rss-p (data)
866 "Test if DATA is an RSS feed.
867 Simply ensures that the first element is rss or rdf."
868 (or (eq (caar data) 'rss)
869 (eq (caar data) 'rdf:RDF)))
871 (defun nnrss-rss-title-description (rss-namespace data url)
872 "Return the title of an RSS feed."
873 (if (nnrss-rss-p data)
874 (let ((description (intern (concat rss-namespace "description")))
875 (title (intern (concat rss-namespace "title")))
876 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
879 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
880 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
883 (defun nnrss-get-namespace-prefix (el uri)
884 "Given EL (containing a parsed element) and URI (containing a string
885 that gives the URI for which you want to retrieve the namespace
886 prefix), return the prefix."
887 (let* ((prefix (car (rassoc uri (cadar el))))
889 (split-string (symbol-name prefix) ":")))
890 (ns (cond ((eq (length nslist) 1) ; no prefix given
892 ((eq (length nslist) 2) ; extract prefix
894 (if (and ns (not (string= ns "")))
901 ;;; nnrss.el ends here
903 ;;; arch-tag: 12910c07-0cdf-44fb-8d2c-416ded64c267