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))
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)
288 ;; There may be two or more entries in `nnrss-group-alist' since
289 ;; this function didn't delete them formerly.
290 (while (setq elem (assoc group nnrss-group-alist))
291 (setq nnrss-group-alist (delq elem nnrss-group-alist))))
292 (setq nnrss-server-data
293 (delq (assoc group nnrss-server-data) nnrss-server-data))
294 (nnrss-save-server-data server)
296 (delete-file (nnrss-make-filename group server)))
299 (deffoo nnrss-request-list-newsgroups (&optional server)
300 (nnrss-possibly-change-group nil server)
302 (set-buffer nntp-server-buffer)
304 (dolist (elem nnrss-group-alist)
306 (insert (car elem) "\t" (third elem) "\n"))))
309 (nnoo-define-skeleton nnrss)
311 ;;; Internal functions
312 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
314 (defun nnrss-get-encoding ()
315 "Return an encoding attribute specified in the current xml contents."
316 (goto-char (point-min))
318 (if (re-search-forward
319 "<\\?[^>]*encoding=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
321 (intern-soft (downcase (or (match-string-no-properties 1)
322 (match-string-no-properties 2))))
323 ;; The default encoding for xml.
326 (defun nnrss-fetch (url &optional local)
327 "Fetch URL and put it in a the expected Lisp structure."
328 (mm-with-unibyte-buffer
329 ;;some CVS versions of url.el need this to close the connection quickly
330 (let (cs xmlform htmlform)
331 ;; bit o' work necessary for w3 pre-cvs and post-cvs
333 (let ((coding-system-for-read 'binary))
334 (insert-file-contents url))
335 (let (;; FIXME: shouldn't binding `coding-system-for-read' be
336 ;; moved to `mm-url-insert'?
337 (coding-system-for-read 'binary)
338 ;; mm-url will load mm-util. d-e-m-c should be bound to
339 ;; t then, because of `mm-emacs-mule'.
340 (default-enable-multibyte-characters t))
341 (mm-url-insert url)))
342 (nnheader-remove-cr-followed-by-lf)
343 ;; Decode text according to the encoding attribute.
344 (when (setq cs (nnrss-get-encoding))
345 (mm-decode-coding-region (point-min) (point-max) cs)
346 (mm-enable-multibyte))
347 (goto-char (point-min))
349 ;; Because xml-parse-region can't deal with anything that isn't
350 ;; xml and w3-parse-buffer can't deal with some xml, we have to
351 ;; parse with xml-parse-region first and, if that fails, parse
352 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
353 ;; why w3-parse-buffer fails to parse some well-formed xml and
357 (setq xmlform (xml-parse-region (point-min) (point-max)))
360 (setq htmlform (caddar (w3-parse-buffer
364 nnrss: %s: Not valid XML %s and w3-parse doesn't work %s"
370 (defun nnrss-possibly-change-group (&optional group server)
372 (not (nnrss-server-opened server)))
373 (nnrss-open-server server))
374 (when (and group (not (equal group nnrss-group)))
375 (nnrss-read-group-data group server)
376 (setq nnrss-group group)))
378 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
380 (defun nnrss-generate-active ()
381 (when (y-or-n-p "Fetch extra categories? ")
382 (mapc 'funcall nnrss-extra-categories))
384 (set-buffer nntp-server-buffer)
386 (dolist (elem nnrss-group-alist)
387 (insert (prin1-to-string (car elem)) " 0 1 y\n"))
388 (dolist (elem nnrss-server-data)
389 (unless (assoc (car elem) nnrss-group-alist)
390 (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
394 (defun nnrss-read-server-data (server)
395 (setq nnrss-server-data nil)
396 (let ((file (nnrss-make-filename "nnrss" server)))
397 (when (file-exists-p file)
398 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
399 ;; file names. So, we use `insert-file-contents' instead.
400 (mm-with-multibyte-buffer
401 (let ((coding-system-for-read nnrss-file-coding-system)
402 (file-name-coding-system nnmail-pathname-coding-system))
403 (insert-file-contents file)
404 (eval-region (point-min) (point-max)))))))
406 (defun nnrss-save-server-data (server)
407 (gnus-make-directory nnrss-directory)
408 (let ((coding-system-for-write nnrss-file-coding-system)
409 (file-name-coding-system nnmail-pathname-coding-system))
410 (with-temp-file (nnrss-make-filename "nnrss" server)
411 (insert (format ";; -*- coding: %s; -*-\n"
412 nnrss-file-coding-system))
413 (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
415 (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
417 (defun nnrss-read-group-data (group server)
418 (setq nnrss-group-data nil)
419 (if (hash-table-p nnrss-group-hashtb)
420 (clrhash nnrss-group-hashtb)
421 (setq nnrss-group-hashtb (make-hash-table :test 'equal)))
422 (let ((pair (assoc group nnrss-server-data)))
423 (setq nnrss-group-max (or (cadr pair) 0))
424 (setq nnrss-group-min (+ nnrss-group-max 1)))
425 (let ((file (nnrss-make-filename group server)))
426 (when (file-exists-p file)
427 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
428 ;; file names. So, we use `insert-file-contents' instead.
429 (mm-with-multibyte-buffer
430 (let ((coding-system-for-read nnrss-file-coding-system)
431 (file-name-coding-system nnmail-pathname-coding-system))
432 (insert-file-contents file)
433 (eval-region (point-min) (point-max))))
434 (dolist (e nnrss-group-data)
435 (puthash (or (nth 2 e) (nth 6 e)) t nnrss-group-hashtb)
436 (when (and (car e) (> nnrss-group-min (car e)))
437 (setq nnrss-group-min (car e)))
438 (when (and (car e) (< nnrss-group-max (car e)))
439 (setq nnrss-group-max (car e)))))))
441 (defun nnrss-save-group-data (group server)
442 (gnus-make-directory nnrss-directory)
443 (let ((coding-system-for-write nnrss-file-coding-system)
444 (file-name-coding-system nnmail-pathname-coding-system))
445 (with-temp-file (nnrss-make-filename group server)
446 (insert (format ";; -*- coding: %s; -*-\n"
447 nnrss-file-coding-system))
448 (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
450 (defun nnrss-make-filename (name server)
452 (nnrss-translate-file-chars
455 (not (equal server ""))
461 (gnus-add-shutdown 'nnrss-close 'gnus)
463 (defun nnrss-close ()
464 "Clear internal nnrss variables."
465 (setq nnrss-group-data nil
466 nnrss-server-data nil
467 nnrss-group-hashtb nil
468 nnrss-group-alist nil))
472 (defun nnrss-no-cache (url)
475 (defun nnrss-insert-w3 (url)
476 (mm-with-unibyte-current-buffer
477 (mm-url-insert url)))
479 (defun nnrss-decode-entities-string (string)
481 (mm-with-multibyte-buffer
483 (mm-url-decode-entities-nbsp)
486 (defalias 'nnrss-insert 'nnrss-insert-w3)
488 (defun nnrss-mime-encode-string (string)
489 (mm-with-multibyte-buffer
491 (mm-url-decode-entities-nbsp)
492 (goto-char (point-min))
493 (while (re-search-forward "[\t\n ]+" nil t)
495 (goto-char (point-min))
496 (skip-chars-forward " ")
497 (delete-region (point-min) (point))
498 (goto-char (point-max))
499 (skip-chars-forward " ")
500 (delete-region (point) (point-max))
501 (eword-encode-string (buffer-string) (eval '(- -1 (lsh -1 -1))))))
505 (defun nnrss-check-group (group server)
506 (let (file xml subject url extra changed author
507 date rss-ns rdf-ns content-ns dc-ns)
508 (if (and nnrss-use-local
509 (file-exists-p (setq file (expand-file-name
510 (nnrss-translate-file-chars
511 (concat group ".xml"))
513 (setq xml (nnrss-fetch file t))
514 (setq url (or (nth 2 (assoc group nnrss-server-data))
515 (second (assoc group nnrss-group-alist))))
522 (format "URL to search for %s: " group) "http://")))))
523 (let ((pair (assoc group nnrss-server-data)))
525 (setcdr (cdr pair) (list url))
526 (push (list group nnrss-group-max url) nnrss-server-data)))
528 (setq xml (nnrss-fetch url)))
530 ;; http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
531 ;; for more RSS namespaces.
532 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
533 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
534 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
535 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
536 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
537 (when (and (listp item)
538 (string= (concat rss-ns "item") (car item))
539 (if (setq url (nnrss-decode-entities-string
540 (nnrss-node-text rss-ns 'link (cddr item))))
541 (not (gethash url nnrss-group-hashtb))
542 (setq extra (or (nnrss-node-text content-ns 'encoded item)
543 (nnrss-node-text rss-ns 'description item)))
544 (not (gethash extra nnrss-group-hashtb))))
545 (setq subject (nnrss-node-text rss-ns 'title item))
546 (setq extra (or extra
547 (nnrss-node-text content-ns 'encoded item)
548 (nnrss-node-text rss-ns 'description item)))
549 (setq author (or (nnrss-node-text rss-ns 'author item)
550 (nnrss-node-text dc-ns 'creator item)
551 (nnrss-node-text dc-ns 'contributor item)))
552 (setq date (or (nnrss-node-text dc-ns 'date item)
553 (nnrss-node-text rss-ns 'pubDate item)
554 (message-make-date)))
557 (incf nnrss-group-max)
560 (and subject (nnrss-mime-encode-string subject))
561 (and author (nnrss-mime-encode-string author))
563 (and extra (nnrss-decode-entities-string extra)))
565 (puthash (or url extra) t nnrss-group-hashtb)
569 (nnrss-save-group-data group server)
570 (let ((pair (assoc group nnrss-server-data)))
572 (setcar (cdr pair) nnrss-group-max)
573 (push (list group nnrss-group-max) nnrss-server-data)))
574 (nnrss-save-server-data server))))
576 (defun nnrss-opml-import (opml-file)
577 "OPML subscriptions import.
578 Read the file and attempt to subscribe to each Feed in the file."
579 (interactive "fImport file: ")
581 (lambda (node) (gnus-group-make-rss-group
582 (cdr (assq 'xmlUrl (cadr node)))))
583 (nnrss-find-el 'outline
585 (find-file opml-file)
586 (xml-parse-region (point-min)
589 (defun nnrss-opml-export ()
590 "OPML subscription export.
591 Export subscriptions to a buffer in OPML Format."
593 (with-current-buffer (get-buffer-create "*OPML Export*")
594 (mm-set-buffer-file-coding-system 'utf-8)
596 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
597 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
598 "<opml version=\"1.1\">\n"
600 " <title>mySubscriptions</title>\n"
601 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
603 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
604 " <ownerName>" (user-full-name) "</ownerName>\n"
609 " <outline text=\"" (car sub) "\" xmlUrl=\""
610 (cadr sub) "\"/>\n")))
615 (pop-to-buffer "*OPML Export*")
616 (when (fboundp 'sgml-mode)
619 (defun nnrss-generate-download-script ()
620 "Generate a download script in the current buffer.
621 It is useful when `(setq nnrss-use-local t)'."
623 (insert "#!/bin/sh\n")
624 (insert "WGET=wget\n")
625 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
626 (dolist (elem nnrss-server-data)
627 (let ((url (or (nth 2 elem)
628 (second (assoc (car elem) nnrss-group-alist)))))
629 (insert "$WGET -q -O \"$RSSDIR\"/'"
630 (nnrss-translate-file-chars (concat (car elem) ".xml"))
633 (defun nnrss-translate-file-chars (name)
634 (let ((nnheader-file-name-translation-alist
635 (append nnheader-file-name-translation-alist '((?' . ?_)))))
636 (nnheader-translate-file-chars name)))
638 (defvar nnrss-moreover-url
639 "http://w.moreover.com/categories/category_list_rss.html"
640 "The url of moreover.com categories.")
642 (defun nnrss-snarf-moreover-categories ()
643 "Snarf RSS links from moreover.com."
645 (let (category name url changed)
647 (nnrss-insert nnrss-moreover-url)
648 (goto-char (point-min))
649 (while (re-search-forward
650 "<a name=\"\\([^\"]+\\)\">\\|<a href=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
652 (setq category (match-string 1))
653 (setq url (match-string 2)
654 name (mm-url-decode-entities-string
655 (cadr (mime-decode-parameters
656 (list "c*" (match-string 3))))))
658 (setq name (concat category "." name)))
659 (unless (assoc name nnrss-server-data)
661 (push (list name 0 url) nnrss-server-data)))))
663 (nnrss-save-server-data ""))))
665 (defun nnrss-node-text (namespace local-name element)
666 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
668 (text (if (and node (listp node))
669 (nnrss-node-just-text node)
671 (cleaned-text (if text (gnus-replace-in-string
672 text "^[\000-\037\177]+\\|^ +\\| +$" ""))))
673 (if (string-equal "" cleaned-text)
677 (defun nnrss-node-just-text (node)
678 (if (and node (listp node))
679 (mapconcat 'nnrss-node-just-text (cddr node) " ")
682 (defun nnrss-find-el (tag data &optional found-list)
683 "Find the all matching elements in the data.
684 Careful with this on large documents!"
688 (when (equal tag (car bit))
689 ;; Old xml.el may return a list of string.
690 (when (and (consp (caddr bit))
691 (stringp (caaddr bit)))
692 (setcar (cddr bit) (caaddr bit)))
696 (if (and (listp (car-safe (caddr bit)))
697 (not (stringp (caddr bit))))
709 (defun nnrss-rsslink-p (el)
710 "Test if the element we are handed is an RSS autodiscovery link."
711 (and (eq (car-safe el) 'link)
712 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
713 (or (string-equal (cdr (assoc 'type (cadr el)))
714 "application/rss+xml")
715 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
717 (defun nnrss-get-rsslinks (data)
718 "Extract the <link> elements that are links to RSS from the parsed data."
721 (if (nnrss-rsslink-p el) el))
722 (nnrss-find-el 'link data))))
724 (defun nnrss-extract-hrefs (data)
725 "Recursively extract hrefs from a page's source.
726 DATA should be the output of `xml-parse-region' or
728 (mapcar (lambda (ahref)
729 (cdr (assoc 'href (cadr ahref))))
730 (nnrss-find-el 'a data)))
732 (defmacro nnrss-match-macro (base-uri item
733 onsite-list offsite-list)
734 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
735 (not (string-match "://" ,item)))
736 (setq ,onsite-list (append ,onsite-list (list ,item))))
737 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
739 (defun nnrss-order-hrefs (base-uri hrefs)
740 "Given a list of hrefs, sort them using the following priorities:
741 1. links ending in .rss
742 2. links ending in .rdf
743 3. links ending in .xml
744 4. links containing the above
747 BASE-URI is used to determine the location of the links and
748 whether they are `offsite' or `onsite'."
749 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
750 rss-onsite-in rdf-onsite-in xml-onsite-in
751 rss-offsite-end rdf-offsite-end xml-offsite-end
752 rss-offsite-in rdf-offsite-in xml-offsite-in)
754 (if (not (null href))
755 (cond ((string-match "\\.rss$" href)
757 base-uri href rss-onsite-end rss-offsite-end))
758 ((string-match "\\.rdf$" href)
760 base-uri href rdf-onsite-end rdf-offsite-end))
761 ((string-match "\\.xml$" href)
763 base-uri href xml-onsite-end xml-offsite-end))
764 ((string-match "rss" href)
766 base-uri href rss-onsite-in rss-offsite-in))
767 ((string-match "rdf" href)
769 base-uri href rdf-onsite-in rdf-offsite-in))
770 ((string-match "xml" href)
772 base-uri href xml-onsite-in xml-offsite-in)))))
775 rss-onsite-end rdf-onsite-end xml-onsite-end
776 rss-onsite-in rdf-onsite-in xml-onsite-in
777 rss-offsite-end rdf-offsite-end xml-offsite-end
778 rss-offsite-in rdf-offsite-in xml-offsite-in)))
780 (defun nnrss-discover-feed (url)
781 "Given a page, find an RSS feed using Mark Pilgrim's
782 `ultra-liberal rss locator' (http://diveintomark.org/2002/08/15.html)."
784 (let ((parsed-page (nnrss-fetch url)))
786 ;; 1. if this url is the rss, use it.
787 (if (nnrss-rss-p parsed-page)
788 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
789 (nnrss-rss-title-description rss-ns parsed-page url))
791 ;; 2. look for the <link rel="alternate"
792 ;; type="application/rss+xml" and use that if it is there.
793 (let ((links (nnrss-get-rsslinks parsed-page)))
795 (let* ((xml (nnrss-fetch
796 (cdr (assoc 'href (cadar links)))))
797 (rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")))
798 (nnrss-rss-title-description rss-ns xml (cdr (assoc 'href (cadar links)))))
800 ;; 3. look for links on the site in the following order:
801 ;; - onsite links ending in .rss, .rdf, or .xml
802 ;; - onsite links containing any of the above
803 ;; - offsite links ending in .rss, .rdf, or .xml
804 ;; - offsite links containing any of the above
805 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
806 (match-string 0 url)))
807 (hrefs (nnrss-order-hrefs
808 base-uri (nnrss-extract-hrefs parsed-page)))
810 (while (and (eq rss-link nil) (not (eq hrefs nil)))
811 (let ((href-data (nnrss-fetch (car hrefs))))
812 (if (nnrss-rss-p href-data)
813 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
814 (setq rss-link (nnrss-rss-title-description
815 rss-ns href-data (car hrefs))))
816 (setq hrefs (cdr hrefs)))))
817 (if rss-link rss-link
820 (nnrss-find-rss-via-syndic8 url))))))))
822 (defun nnrss-find-rss-via-syndic8 (url)
823 "Query syndic8 for the rss feeds it has for URL."
824 (if (not (locate-library "xml-rpc"))
826 (message "XML-RPC is not available... not checking Syndic8.")
829 (let ((feedid (xml-rpc-method-call
830 "http://www.syndic8.com/xmlrpc.php"
834 (let* ((feedinfo (xml-rpc-method-call
835 "http://www.syndic8.com/xmlrpc.php"
843 (cdr (assoc "status" listinfo))
846 (cdr (assoc "sitename" listinfo))
850 "sitename" listinfo)))
853 "dataurl" listinfo)))))))
855 (if (not (> (length urllist) 1))
857 (let ((completion-ignore-case t)
859 (mapcar (lambda (listinfo)
860 (cons (cdr (assoc "sitename" listinfo))
862 (cdr (assoc "feedid" listinfo)))))
866 "Multiple feeds found. Select one: "
867 selection nil t) urllist)))))))))
869 (defun nnrss-rss-p (data)
870 "Test if DATA is an RSS feed.
871 Simply ensures that the first element is rss or rdf."
872 (or (eq (caar data) 'rss)
873 (eq (caar data) 'rdf:RDF)))
875 (defun nnrss-rss-title-description (rss-namespace data url)
876 "Return the title of an RSS feed."
877 (if (nnrss-rss-p data)
878 (let ((description (intern (concat rss-namespace "description")))
879 (title (intern (concat rss-namespace "title")))
880 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
883 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
884 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
887 (defun nnrss-get-namespace-prefix (el uri)
888 "Given EL (containing a parsed element) and URI (containing a string
889 that gives the URI for which you want to retrieve the namespace
890 prefix), return the prefix."
891 (let* ((prefix (car (rassoc uri (cadar el))))
893 (split-string (symbol-name prefix) ":")))
894 (ns (cond ((eq (length nslist) 1) ; no prefix given
896 ((eq (length nslist) 2) ; extract prefix
898 (if (and ns (not (string= ns "")))
905 ;;; nnrss.el ends here