]> gitweb.factorcode.org Git - factor.git/blob - basis/syndication/syndication.factor
Merge branch 'lexer-parsing-word-errors' of git://factorcode.org/git/factor into...
[factor.git] / basis / syndication / syndication.factor
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 ;
9 IN: syndication
10
11 : any-tag-named ( tag names -- tag-inside )
12     [ f ] 2dip [ tag-named nip dup ] with find 2drop ;
13
14 TUPLE: feed title url entries ;
15
16 : <feed> ( -- feed ) feed new ;
17
18 TUPLE: entry title url description date ;
19
20 : set-entries ( feed entries -- feed )
21     [ dup url>> ] dip
22     [ [ derive-url ] change-url ] with map
23     >>entries ;
24
25 : <entry> ( -- entry ) entry new ;
26
27 : try-parsing-timestamp ( string -- timestamp )
28     [ rfc822>timestamp ] [ drop rfc3339>timestamp ] recover ;
29
30 : rss1.0-entry ( tag -- entry )
31     entry new
32     swap {
33         [ "title" tag-named children>string >>title ]
34         [ "link" tag-named children>string >url >>url ]
35         [ "description" tag-named children>string >>description ]
36         [
37             f "date" "http://purl.org/dc/elements/1.1/" <name>
38             tag-named dup [ children>string try-parsing-timestamp ] when
39             >>date
40         ]
41     } cleave ;
42
43 : rss1.0 ( xml -- feed )
44     feed new
45     swap [
46         "channel" tag-named
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 ;
50
51 : rss2.0-entry ( tag -- entry )
52     entry new
53     swap {
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 ]
57         [
58             { "date" "pubDate" } any-tag-named
59             children>string try-parsing-timestamp >>date
60         ]
61     } cleave ;
62
63 : rss2.0 ( xml -- feed )
64     feed new
65     swap
66     "channel" tag-named 
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 ]
70     tri ;
71
72 : atom-entry-link ( tag -- url/f )
73     "link" tags-named
74     [ "rel" attr { f "alternate" } member? ] find nip
75     dup [ "href" attr >url ] when ;
76
77 : atom1.0-entry ( tag -- entry )
78     entry new
79     swap {
80         [ "title" tag-named children>string >>title ]
81         [ atom-entry-link >>url ]
82         [
83             { "content" "summary" } any-tag-named
84             dup children>> [ string? not ] any?
85             [ children>> xml>string ]
86             [ children>string ] if >>description
87         ]
88         [
89             { "published" "updated" "issued" "modified" } 
90             any-tag-named children>string try-parsing-timestamp
91             >>date
92         ]
93     } cleave ;
94
95 : atom1.0 ( xml -- feed )
96     feed new
97     swap
98     [ "title" tag-named children>string >>title ]
99     [ "link" tag-named "href" attr >url >>url ]
100     [ "entry" tags-named [ atom1.0-entry ] map set-entries ]
101     tri ;
102
103 : xml>feed ( xml -- feed )
104     dup main>> {
105         { "RDF" [ rss1.0 ] }
106         { "rss" [ rss2.0 ] }
107         { "feed" [ atom1.0 ] }
108     } case ;
109
110 GENERIC: parse-feed ( seq -- feed )
111
112 M: string parse-feed [ string>xml xml>feed ] with-html-entities ;
113
114 M: byte-array parse-feed [ bytes>xml xml>feed ] with-html-entities ;
115
116 : download-feed ( url -- feed )
117     #! Retrieve an news syndication file, return as a feed tuple.
118     http-get nip parse-feed ;
119
120 ! Atom generation
121
122 : entry>xml ( entry -- xml )
123     {
124         [ title>> ]
125         [ url>> present ]
126         [ date>> timestamp>rfc3339 ]
127         [ description>> ]
128     } cleave
129     [XML
130         <entry>
131             <title type="html"><-></title>
132             <link href=<-> />
133             <published><-></published>
134             <content type="html"><-></content>
135         </entry>
136     XML] ;
137
138 : feed>xml ( feed -- xml )
139     [ title>> ]
140     [ url>> present ]
141     [ entries>> [ entry>xml ] map ] tri
142     <XML
143         <feed xmlns="http://www.w3.org/2005/Atom">
144             <title><-></title>
145             <link href=<-> />
146             <->
147         </feed>
148     XML> ;