truncaterss

dvanhorn <[email protected]> Mon, 19 Jan 2004 14:28:29 -0500
Newsgroups gmane.org.ballistichelmet.devel
Message-ID <[email protected]>
I found it frequently necessary to truncate RSS files.  Here's a script for
doing just that in local/bin.

David

#! /home/bh/local/bin/mzscheme -r

;; truncaterss file.rss 5 > file_5.rss
;; will create file_5.rss with the first five items from file.rss

(require (lib "1.ss" "srfi")
         (lib "xml.ss" "xml")
         (lib "match.ss"))

(define file (vector-ref argv 0))
(define n    (string->number (vector-ref argv 1)))

(define (get-rss)
  (xml->xexpr
   ((eliminate-whitespace '(rss channel item) (lambda (x) x))
    (document-element (with-input-from-file file read-xml)))))

(match (get-rss)
  ((and rss
        ('rss _
         ('channel _
          ('title _ _)
          ('link _ _)
          ('description _ _)
          . (and (('item _ . _) ...)
                 (set! set-items)
                 (get! get-items)))))
   (set-items (take (get-items) n))
   (write-xml/content (xexpr->xml rss))))