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