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: xml.traversal kernel assocs math.order strings sequences
5 xml.data xml.writer io.streams.string combinators xml
6 xml.entities.html io.files io http.client namespaces make
7 xml.syntax hashtables calendar.format accessors continuations
8 urls present byte-arrays ;
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 )
32 [ "title" tag-named children>string >>title ]
33 [ "link" tag-named children>string >url >>url ]
34 [ "description" tag-named children>string >>description ]
36 f "date" "http://purl.org/dc/elements/1.1/" <name>
37 tag-named dup [ children>string try-parsing-timestamp ] when
42 : rss1.0 ( xml -- feed )
45 [ "title" tag-named children>string >>title ]
46 [ "link" tag-named children>string >url >>url ] bi
47 ] [ "item" tags-named [ rss1.0-entry ] map set-entries ] bi ;
49 : rss2.0-entry ( tag -- entry )
51 [ "title" tag-named children>string >>title ]
52 [ { "link" "guid" } any-tag-named children>string >url >>url ]
53 [ { "description" "encoded" } any-tag-named children>string >>description ]
55 { "date" "pubDate" } any-tag-named
56 children>string try-parsing-timestamp >>date
60 : rss2.0 ( xml -- feed )
63 [ "title" tag-named children>string >>title ]
64 [ "link" tag-named children>string >url >>url ]
65 [ "item" tags-named [ rss2.0-entry ] map set-entries ]
68 : atom-entry-link ( tag -- url/f )
70 [ "rel" attr { f "alternate" } member? ] find nip
71 dup [ "href" attr >url ] when ;
73 : atom1.0-entry ( tag -- entry )
75 [ "title" tag-named children>string >>title ]
76 [ atom-entry-link >>url ]
78 { "content" "summary" } any-tag-named
79 dup children>> [ string? not ] any?
80 [ children>> xml>string ]
81 [ children>string ] if >>description
84 { "published" "updated" "issued" "modified" }
85 any-tag-named children>string try-parsing-timestamp
90 : atom1.0 ( xml -- feed )
93 [ "title" tag-named children>string >>title ]
94 [ "link" tag-named "href" attr >url >>url ]
95 [ "entry" tags-named [ atom1.0-entry ] map set-entries ]
98 : xml>feed ( xml -- feed )
102 { "feed" [ atom1.0 ] }
105 GENERIC: parse-feed ( seq -- feed )
107 M: string parse-feed [ string>xml xml>feed ] with-html-entities ;
109 M: byte-array parse-feed [ bytes>xml xml>feed ] with-html-entities ;
111 : download-feed ( url -- feed )
112 #! Retrieve an news syndication file, return as a feed tuple.
113 http-get nip parse-feed ;
117 : entry>xml ( entry -- xml )
121 [ date>> timestamp>rfc3339 ]
126 <title type="html"><-></title>
128 <published><-></published>
129 <content type="html"><-></content>
133 : feed>xml ( feed -- xml )
136 [ entries>> [ entry>xml ] map ] tri
138 <feed xmlns="http://www.w3.org/2005/Atom">