]> gitweb.factorcode.org Git - factor.git/blob - basis/syndication/syndication.factor
2fa8abcd59e1599410ff15ff8ed151de6374f34f
[factor.git] / basis / syndication / syndication.factor
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.utilities kernel assocs xml.generator math.order
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
8     calendar.format accessors continuations urls present ;
9 IN: syndication
10
11 : any-tag-named ( tag names -- tag-inside )
12     f -rot [ 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 : atom1.0-entry ( tag -- entry )
73     entry new
74     swap {
75         [ "title" tag-named children>string >>title ]
76         [ "link" tag-named "href" swap at >url >>url ]
77         [
78             { "content" "summary" } any-tag-named
79             dup children>> [ string? not ] contains?
80             [ children>> [ write-chunk ] with-string-writer ]
81             [ children>string ] if >>description
82         ]
83         [
84             { "published" "updated" "issued" "modified" } 
85             any-tag-named children>string try-parsing-timestamp
86             >>date
87         ]
88     } cleave ;
89
90 : atom1.0 ( xml -- feed )
91     feed new
92     swap
93     [ "title" tag-named children>string >>title ]
94     [ "link" tag-named "href" swap at >url >>url ]
95     [ "entry" tags-named [ atom1.0-entry ] map set-entries ]
96     tri ;
97
98 : xml>feed ( xml -- feed )
99     dup main>> {
100         { "RDF" [ rss1.0 ] }
101         { "rss" [ rss2.0 ] }
102         { "feed" [ atom1.0 ] }
103     } case ;
104
105 : read-feed ( string -- feed )
106     [ string>xml xml>feed ] with-html-entities ;
107
108 : download-feed ( url -- feed )
109     #! Retrieve an news syndication file, return as a feed tuple.
110     http-get nip read-feed ;
111
112 ! Atom generation
113 : simple-tag, ( content name -- )
114     [ , ] tag, ;
115
116 : simple-tag*, ( content name attrs -- )
117     [ , ] tag*, ;
118
119 : entry, ( entry -- )
120     "entry" [
121         {
122             [ title>> "title" { { "type" "html" } } simple-tag*, ]
123             [ url>> present "href" associate "link" swap contained*, ]
124             [ date>> timestamp>rfc3339 "published" simple-tag, ]
125             [ description>> [ "content" { { "type" "html" } } simple-tag*, ] when* ]
126         } cleave
127     ] tag, ;
128
129 : feed>xml ( feed -- xml )
130     "feed" { { "xmlns" "http://www.w3.org/2005/Atom" } } [
131         [ title>> "title" simple-tag, ]
132         [ url>> present "href" associate "link" swap contained*, ]
133         [ entries>> [ entry, ] each ]
134         tri
135     ] make-xml* ;