1 ! Copyright (C) 2006 Chris Double, Daniel Ehrenberg.
2 ! Portions copyright (C) 2008, 2009 Slava Pestov.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors byte-arrays calendar.format calendar.parser
5 combinators combinators.short-circuit continuations http.client
6 kernel present sequences strings urls xml xml.data
7 xml.entities.html xml.syntax xml.traversal xml.writer ;
10 : any-tag-named ( tag names -- tag-inside )
11 [ f ] 2dip [ tag-named nip dup ] with find 2drop ;
13 TUPLE: feed title url entries ;
15 : <feed> ( -- feed ) feed new ;
17 TUPLE: entry title url description date ;
19 : set-entries ( feed entries -- feed )
21 [ [ derive-url ] change-url ] with map
24 : <entry> ( -- entry ) entry new ;
26 : try-parsing-timestamp ( string -- timestamp )
27 [ rfc822>timestamp ] [ drop rfc3339>timestamp ] recover ;
29 : rss1.0-entry ( tag -- entry )
31 [ "title" tag-named children>string >>title ]
32 [ "link" tag-named children>string >url >>url ]
33 [ "description" tag-named children>string >>description ]
35 f "date" "http://purl.org/dc/elements/1.1/" <name>
36 tag-named dup [ children>string try-parsing-timestamp ] when
41 : rss1.0 ( xml -- feed )
44 [ "title" tag-named children>string >>title ]
45 [ "link" tag-named children>string >url >>url ] bi
46 ] [ "item" tags-named [ rss1.0-entry ] map set-entries ] bi ;
48 : rss2.0-entry ( tag -- entry )
50 [ "title" tag-named children>string >>title ]
51 [ { "link" "guid" } any-tag-named children>string >url >>url ]
52 [ { "description" "encoded" } any-tag-named children>string >>description ]
54 { "date" "pubDate" } any-tag-named
55 children>string try-parsing-timestamp >>date
59 : rss2.0 ( xml -- feed )
62 [ "title" tag-named children>string >>title ]
63 [ "link" tag-named children>string >url >>url ]
64 [ "item" tags-named [ rss2.0-entry ] map set-entries ]
67 ERROR: invalid-atom-link ;
69 : atom-link ( tag -- url/f )
71 [ "link" "alternate" "rel" tag-named-with-attr ]
72 [ "link" "self" "rel" tag-named-with-attr ]
75 [ "href" attr >url ] [ f ] if* ;
77 : atom1.0-entry ( tag -- entry )
79 [ "title" tag-named children>string >>title ]
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 )
96 [ "title" tag-named children>string >>title ]
98 [ "entry" tags-named [ atom1.0-entry ] map set-entries ]
101 : xml>feed ( xml -- feed )
105 { "feed" [ atom1.0 ] }
108 GENERIC: parse-feed ( seq -- feed )
110 M: string parse-feed [ string>xml xml>feed ] with-html-entities ;
112 M: byte-array parse-feed [ bytes>xml xml>feed ] with-html-entities ;
114 : download-feed ( url -- feed )
115 ! Retrieve an news syndication file, return as a feed tuple.
116 http-get nip parse-feed ;
120 : entry>xml ( entry -- xml )
124 [ date>> timestamp>rfc3339 ]
129 <title type="html"><-></title>
130 <link rel="alternate" href=<-> />
131 <published><-></published>
132 <content type="html"><-></content>
136 : feed>xml ( feed -- xml )
139 [ entries>> [ entry>xml ] map ] tri
141 <feed xmlns="http://www.w3.org/2005/Atom">
143 <link rel="alternate" href=<-> />