1 ! Copyright (C) 2006 Chris Double, Daniel Ehrenberg.
2 ! Portions copyright (C) 2008 Slava Pestov.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: xml.traversal kernel assocs math.order
5 strings sequences xml.data xml.writer
6 io.streams.string combinators xml xml.entities.html io.files io
7 http.client namespaces make xml.syntax hashtables
8 calendar.format accessors continuations urls present ;
11 : any-tag-named ( tag names -- tag-inside )
12 [ f ] 2dip [ tag-named nip dup ] with find 2drop ;
14 TUPLE: feed title url entries ;
16 : <feed> ( -- feed ) feed new ;
18 TUPLE: entry title url description date ;
20 : set-entries ( feed entries -- feed )
22 [ [ derive-url ] change-url ] with map
25 : <entry> ( -- entry ) entry new ;
27 : try-parsing-timestamp ( string -- timestamp )
28 [ rfc822>timestamp ] [ drop rfc3339>timestamp ] recover ;
30 : rss1.0-entry ( tag -- entry )
33 [ "title" tag-named children>string >>title ]
34 [ "link" tag-named children>string >url >>url ]
35 [ "description" tag-named children>string >>description ]
37 f "date" "http://purl.org/dc/elements/1.1/" <name>
38 tag-named dup [ children>string try-parsing-timestamp ] when
43 : rss1.0 ( xml -- feed )
47 [ "title" tag-named children>string >>title ]
48 [ "link" tag-named children>string >url >>url ] bi
49 ] [ "item" tags-named [ rss1.0-entry ] map set-entries ] bi ;
51 : rss2.0-entry ( tag -- entry )
54 [ "title" tag-named children>string >>title ]
55 [ { "link" "guid" } any-tag-named children>string >url >>url ]
56 [ { "description" "encoded" } any-tag-named children>string >>description ]
58 { "date" "pubDate" } any-tag-named
59 children>string try-parsing-timestamp >>date
63 : rss2.0 ( xml -- feed )
67 [ "title" tag-named children>string >>title ]
68 [ "link" tag-named children>string >url >>url ]
69 [ "item" tags-named [ rss2.0-entry ] map set-entries ]
72 : atom-entry-link ( tag -- url/f )
73 "link" tags-named [ "rel" attr "alternate" = ] find nip
74 dup [ "href" attr >url ] when ;
76 : atom1.0-entry ( tag -- entry )
79 [ "title" tag-named children>string >>title ]
80 [ atom-entry-link >>url ]
82 { "content" "summary" } any-tag-named
83 dup children>> [ string? not ] any?
84 [ children>> xml>string ]
85 [ children>string ] if >>description
88 { "published" "updated" "issued" "modified" }
89 any-tag-named children>string try-parsing-timestamp
94 : atom1.0 ( xml -- feed )
97 [ "title" tag-named children>string >>title ]
98 [ "link" tag-named "href" attr >url >>url ]
99 [ "entry" tags-named [ atom1.0-entry ] map set-entries ]
102 : xml>feed ( xml -- feed )
106 { "feed" [ atom1.0 ] }
109 : string>feed ( string -- feed )
110 [ string>xml xml>feed ] with-html-entities ;
112 : download-feed ( url -- feed )
113 #! Retrieve an news syndication file, return as a feed tuple.
114 http-get nip string>feed ;
118 : entry>xml ( entry -- xml )
122 [ date>> timestamp>rfc3339 ]
127 <title type="html"><-></title>
129 <published><-></published>
130 <content type="html"><-></content>
134 : feed>xml ( feed -- xml )
137 [ entries>> [ entry>xml ] map ] tri
139 <feed xmlns="http://www.w3.org/2005/Atom">