1 ! Copyright (C) 2006 Chris Double, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: xml.utilities kernel assocs xml.generator
5 strings sequences xml.data xml.writer
6 io.streams.string combinators xml xml.entities io.files io
7 http.client namespaces xml.generator hashtables ;
9 : ?children>string ( tag/f -- string/f )
10 [ children>string ] [ f ] if* ;
12 : any-tag-named ( tag names -- tag-inside )
13 f -rot [ tag-named nip dup ] curry* find 2drop ;
15 TUPLE: feed title link entries ;
19 TUPLE: entry title link description pub-date ;
23 : rss1.0-entry ( tag -- entry )
24 [ "title" tag-named children>string ] keep
25 [ "link" tag-named children>string ] keep
26 [ "description" tag-named children>string ] keep
27 f "date" "http://purl.org/dc/elements/1.1/" <name>
28 tag-named ?children>string
31 : rss1.0 ( xml -- feed )
34 [ "title" tag-named children>string ] keep
35 "link" tag-named children>string
37 "item" tags-named [ rss1.0-entry ] map <feed> ;
39 : rss2.0-entry ( tag -- entry )
40 [ "title" tag-named children>string ] keep
41 [ "link" tag-named ] keep
42 [ "guid" tag-named dupd ? children>string ] keep
43 [ "description" tag-named children>string ] keep
44 "pubDate" tag-named children>string <entry> ;
46 : rss2.0 ( xml -- feed )
48 [ "title" tag-named children>string ] keep
49 [ "link" tag-named children>string ] keep
50 "item" tags-named [ rss2.0-entry ] map <feed> ;
52 : atom1.0-entry ( tag -- entry )
53 [ "title" tag-named children>string ] keep
54 [ "link" tag-named "href" swap at ] keep
56 { "content" "summary" } any-tag-named
57 dup tag-children [ string? not ] contains?
58 [ tag-children [ write-chunk ] string-out ]
59 [ children>string ] if
61 { "published" "updated" "issued" "modified" } any-tag-named
62 children>string <entry> ;
64 : atom1.0 ( xml -- feed )
65 [ "title" tag-named children>string ] keep
66 [ "link" tag-named "href" swap at ] keep
67 "entry" tags-named [ atom1.0-entry ] map <feed> ;
69 : xml>feed ( xml -- feed )
73 { "feed" [ atom1.0 ] }
76 : read-feed ( stream -- feed )
77 [ read-xml ] with-html-entities xml>feed ;
79 : download-feed ( url -- feed )
80 #! Retrieve an news syndication file, return as a feed tuple.
81 http-get-stream rot 200 = [
84 2drop "Error retrieving newsfeed file" throw
90 << title >> [ dup entry-title , ]
91 << link [ dup entry-link ] == href // >>
92 << published >> [ dup entry-pub-date , ]
93 << content >> [ entry-description , ]
96 : feed>xml ( feed -- xml )
98 << feed [ "http://www.w3.org/2005/Atom" ] == xmlns >> [
99 << title >> [ dup feed-title , ]
100 << link [ dup feed-link ] == href // >>
101 feed-entries [ entry, ] each
105 : write-feed ( feed -- )