/[XML-Feed]/lib/XML/Feed/RSS.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /lib/XML/Feed/RSS.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (hide annotations)
Sun Mar 16 19:47:49 2008 UTC (16 years, 2 months ago) by dpavlin
File size: 7001 byte(s)
import XML::Feed 0.12 from CPAN

1 dpavlin 1 # $Id: RSS.pm 1934 2006-04-22 05:13:55Z btrott $
2    
3     package XML::Feed::RSS;
4     use strict;
5    
6     use base qw( XML::Feed );
7     use DateTime::Format::Mail;
8     use DateTime::Format::W3CDTF;
9    
10     our $PREFERRED_PARSER = "XML::RSS";
11    
12     sub init_empty {
13     my $feed = shift;
14     eval "use $PREFERRED_PARSER"; die $@ if $@;
15     $feed->{rss} = $PREFERRED_PARSER->new( version => '2.0' );
16     $feed->{rss}->add_module(prefix => "content", uri => 'http://purl.org/rss/1.0/modules/content/');
17     $feed->{rss}->add_module(prefix => "dcterms", uri => 'http://purl.org/rss/1.0/modules/dcterms/');
18     $feed;
19     }
20    
21     sub init_string {
22     my $feed = shift;
23     my($str) = @_;
24     $feed->init_empty;
25     if ($str) {
26     $feed->{rss}->parse($$str);
27     }
28     $feed;
29     }
30    
31     sub format { 'RSS ' . $_[0]->{rss}->{'version'} }
32    
33     ## The following elements are the same in all versions of RSS.
34     sub title { shift->{rss}->channel('title', @_) }
35     sub link { shift->{rss}->channel('link', @_) }
36     sub description { shift->{rss}->channel('description', @_) }
37    
38     ## This is RSS 2.0 only--what's the equivalent in RSS 1.0?
39     sub copyright { shift->{rss}->channel('copyright', @_) }
40    
41     ## The following all work transparently in any RSS version.
42     sub language {
43     my $feed = shift;
44     if (@_) {
45     $feed->{rss}->channel('language', $_[0]);
46     $feed->{rss}->channel->{dc}{language} = $_[0];
47     } else {
48     $feed->{rss}->channel('language') ||
49     $feed->{rss}->channel->{dc}{language};
50     }
51     }
52    
53     sub generator {
54     my $feed = shift;
55     if (@_) {
56     $feed->{rss}->channel('generator', $_[0]);
57     $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent} =
58     $_[0];
59     } else {
60     $feed->{rss}->channel('generator') ||
61     $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent};
62     }
63     }
64    
65     sub author {
66     my $feed = shift;
67     if (@_) {
68     $feed->{rss}->channel('webMaster', $_[0]);
69     $feed->{rss}->channel->{dc}{creator} = $_[0];
70     } else {
71     $feed->{rss}->channel('webMaster') ||
72     $feed->{rss}->channel->{dc}{creator};
73     }
74     }
75    
76     sub modified {
77     my $rss = shift->{rss};
78     if (@_) {
79     $rss->channel('pubDate',
80     DateTime::Format::Mail->format_datetime($_[0]));
81     ## XML::RSS is so weird... if I set this, it will try to use
82     ## the value for the lastBuildDate, which I don't want--because
83     ## this date is formatted for an RSS 1.0 feed. So it's commented out.
84     #$rss->channel->{dc}{date} =
85     # DateTime::Format::W3CDTF->format_datetime($_[0]);
86     } else {
87     my $date;
88     eval {
89     if (my $ts = $rss->channel('pubDate')) {
90     $date = DateTime::Format::Mail->parse_datetime($ts);
91     } elsif ($ts = $rss->channel->{dc}{date}) {
92     $date = DateTime::Format::W3CDTF->parse_datetime($ts);
93     }
94     };
95     return $date;
96     }
97     }
98    
99     sub entries {
100     my $rss = $_[0]->{rss};
101     my @entries;
102     for my $item (@{ $rss->{items} }) {
103     push @entries, XML::Feed::Entry::RSS->wrap($item);
104     }
105     @entries;
106     }
107    
108     sub add_entry {
109     my $feed = shift;
110     my($entry) = @_;
111     $feed->{rss}->add_item(%{ $entry->unwrap });
112     }
113    
114     sub as_xml { $_[0]->{rss}->as_string }
115    
116     package XML::Feed::Entry::RSS;
117     use strict;
118    
119     use XML::Feed::Content;
120    
121     use base qw( XML::Feed::Entry );
122    
123     sub init_empty { $_[0]->{entry} = { } }
124    
125     sub title {
126     my $entry = shift;
127     @_ ? $entry->{entry}{title} = $_[0] : $entry->{entry}{title};
128     }
129    
130     sub link {
131     my $entry = shift;
132     if (@_) {
133     $entry->{entry}{link} = $_[0];
134     ## For RSS 2.0 output from XML::RSS. Sigh.
135     $entry->{entry}{permaLink} = $_[0];
136     } else {
137     $entry->{entry}{link} || $entry->{entry}{guid};
138     }
139     }
140    
141     sub summary {
142     my $item = shift->{entry};
143     if (@_) {
144     $item->{description} = ref($_[0]) eq 'XML::Feed::Content' ?
145     $_[0]->body : $_[0];
146     ## Because of the logic below, we need to add some dummy content,
147     ## so that we'll properly recognize the description we enter as
148     ## the summary.
149     if (!$item->{content}{encoded} &&
150     !$item->{'http://www.w3.org/1999/xhtml'}{body}) {
151     $item->{content}{encoded} = ' ';
152     }
153     } else {
154     ## Some RSS feeds use <description> for a summary, and some use it
155     ## for the full content. Pretty gross. We don't want to return the
156     ## full content if the caller expects a summary, so the heuristic is:
157     ## if the <entry> contains both a <description> and one of the elements
158     ## typically used for the full content, use <description> as summary.
159     my $txt;
160     if ($item->{description} &&
161     ($item->{content}{encoded} ||
162     $item->{'http://www.w3.org/1999/xhtml'}{body})) {
163     $txt = $item->{description};
164     }
165     XML::Feed::Content->wrap({ type => 'text/plain', body => $txt });
166     }
167     }
168    
169     sub content {
170     my $item = shift->{entry};
171     if (@_) {
172     my $c = ref($_[0]) eq 'XML::Feed::Content' ? $_[0]->body : $_[0];
173     $item->{content}{encoded} = $c;
174     } else {
175     my $body =
176     $item->{content}{encoded} ||
177     $item->{'http://www.w3.org/1999/xhtml'}{body} ||
178     $item->{description};
179     XML::Feed::Content->wrap({ type => 'text/html', body => $body });
180     }
181     }
182    
183     sub category {
184     my $item = shift->{entry};
185     if (@_) {
186     $item->{category} = $item->{dc}{subject} = $_[0];
187     } else {
188     $item->{category} || $item->{dc}{subject};
189     }
190     }
191    
192     sub author {
193     my $item = shift->{entry};
194     if (@_) {
195     $item->{author} = $item->{dc}{creator} = $_[0];
196     } else {
197     $item->{author} || $item->{dc}{creator};
198     }
199     }
200    
201     ## XML::RSS doesn't give us access to the rdf:about for the <item>,
202     ## so we have to fall back to the <link> element in RSS 1.0 feeds.
203     sub id {
204     my $item = shift->{entry};
205     if (@_) {
206     $item->{guid} = $_[0];
207     } else {
208     $item->{guid} || $item->{link};
209     }
210     }
211    
212     sub issued {
213     my $item = shift->{entry};
214     if (@_) {
215     $item->{dc}{date} = DateTime::Format::W3CDTF->format_datetime($_[0]);
216     $item->{pubDate} = DateTime::Format::Mail->format_datetime($_[0]);
217     } else {
218     ## Either of these could die if the format is invalid.
219     my $date;
220     eval {
221     if (my $ts = $item->{pubDate}) {
222     my $parser = DateTime::Format::Mail->new;
223     $parser->loose;
224     $date = $parser->parse_datetime($ts);
225     } elsif ($ts = $item->{dc}{date}) {
226     $date = DateTime::Format::W3CDTF->parse_datetime($ts);
227     }
228     };
229     return $date;
230     }
231     }
232    
233     sub modified {
234     my $item = shift->{entry};
235     if (@_) {
236     $item->{dcterms}{modified} =
237     DateTime::Format::W3CDTF->format_datetime($_[0]);
238     } else {
239     if (my $ts = $item->{dcterms}{modified}) {
240     return eval { DateTime::Format::W3CDTF->parse_datetime($ts) };
241     }
242     }
243     }
244    
245     1;

  ViewVC Help
Powered by ViewVC 1.1.26