| 1 | package XML::RSS::Parser::Lite::Item; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | our $VERSION = '0.12'; | ||
| 6 | |||
| 7 | sub new { | ||
| 8 | my $class = shift; | ||
| 9 | |||
| 10 | my $self = { | ||
| 11 | 'title', '', | ||
| 12 | 'url', '', | ||
| 13 | 'description', '', | ||
| 14 | 'pubDate', '', | ||
| 15 | 'author', '', | ||
| 16 | 'category', '', | ||
| 17 | 'comments', '', | ||
| 18 | 'enclosure', '', | ||
| 19 | 'guid', '', | ||
| 20 | 'source', '', | ||
| 21 | @_, | ||
| 22 | }; | ||
| 23 | |||
| 24 | bless($self, $class); | ||
| 25 | return $self; | ||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | sub set { | ||
| 30 | my $self = shift; | ||
| 31 | |||
| 32 | my %defs = (@_); | ||
| 33 | foreach my $k (keys %defs) { | ||
| 34 | $self->{$k} = $defs{$k}; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | sub get { | ||
| 39 | my $self = shift; | ||
| 40 | |||
| 41 | my $want = shift; | ||
| 42 | return $self->{$want}; | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | package XML::RSS::Parser::Lite; | ||
| 47 | |||
| 48 | use strict; | ||
| 49 | use XML::Parser::Lite; | ||
| 50 | |||
| 51 | our $VERSION = '0.12'; | ||
| 52 | |||
| 53 | sub new { | ||
| 54 | my $class = shift; | ||
| 55 | |||
| 56 | my $parser = new XML::Parser::Lite; | ||
| 57 | my $self = { | ||
| 58 | # mandatory | ||
| 59 | parser => $parser, | ||
| 60 | place => '', | ||
| 61 | title => '', | ||
| 62 | url => '', | ||
| 63 | description => '', | ||
| 64 | items => [], | ||
| 65 | latest => new XML::RSS::Parser::Lite::Item, | ||
| 66 | # optionals | ||
| 67 | pubDate => '', | ||
| 68 | language => '', | ||
| 69 | copyright => '', | ||
| 70 | managingEditor => '', | ||
| 71 | webMaster => '', | ||
| 72 | pubDate => '', | ||
| 73 | lastBuildDate => '', | ||
| 74 | category => '', | ||
| 75 | generator => '', | ||
| 76 | docs => '', | ||
| 77 | cloud => '', | ||
| 78 | ttl => '', | ||
| 79 | image => '', | ||
| 80 | rating => '', | ||
| 81 | textInput => '', | ||
| 82 | skipHours => '', | ||
| 83 | skipDays => '', | ||
| 84 | }; | ||
| 85 | |||
| 86 | $self->{parser}->setHandlers( | ||
| 87 | Final => sub { shift; $self->final(@_) }, | ||
| 88 | Start => sub { shift; $self->start(@_) }, | ||
| 89 | End => sub { shift; $self->end(@_) }, | ||
| 90 | Char => sub { shift; $self->char(@_) }, | ||
| 91 | ); | ||
| 92 | |||
| 93 | bless($self, $class); | ||
| 94 | return $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub parse { | ||
| 98 | my $self = shift; | ||
| 99 | my $xml = shift; | ||
| 100 | |||
| 101 | $self->{parser}->parse($xml); | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | sub final { | ||
| 106 | my $self = shift; | ||
| 107 | |||
| 108 | $self->{parser}->setHandlers(Final => undef, Start => undef, End => undef, Char => undef); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub start { | ||
| 112 | my $self = shift; | ||
| 113 | my $tag = shift; | ||
| 114 | |||
| 115 | $self->{place} .= "/$tag"; | ||
| 116 | $self->{latest} = $self->add if ($self->{place} eq '/rss/channel/item'); | ||
| 117 | $self->{latest} = $self->add if ($self->{place} eq '/rdf:RDF/item'); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub char { | ||
| 121 | my $self = shift; | ||
| 122 | my $text = shift; | ||
| 123 | |||
| 124 | # set item | ||
| 125 | $self->{latest}->set('title', $text) if ($self->{place} eq '/rss/channel/item/title'); | ||
| 126 | $self->{latest}->set('url', $text) if ($self->{place} eq '/rss/channel/item/link'); | ||
| 127 | $self->{latest}->set('description', $text) if ($self->{place} eq '/rss/channel/item/description'); | ||
| 128 | # optional fields | ||
| 129 | $self->{latest}->set('pubDate', $text) if ($self->{place} eq '/rss/channel/item/pubDate'); | ||
| 130 | $self->{latest}->set('author', $text) if ($self->{place} eq '/rss/channel/item/author'); | ||
| 131 | $self->{latest}->set('category', $text) if ($self->{place} eq '/rss/channel/item/category'); | ||
| 132 | $self->{latest}->set('comments', $text) if ($self->{place} eq '/rss/channel/item/comments'); | ||
| 133 | $self->{latest}->set('enclosure', $text) if ($self->{place} eq '/rss/channel/item/enclosure'); | ||
| 134 | $self->{latest}->set('guid', $text) if ($self->{place} eq '/rss/channel/item/guid'); | ||
| 135 | $self->{latest}->set('source', $text) if ($self->{place} eq '/rss/channel/item/source'); | ||
| 136 | |||
| 137 | # compatibility with RSS1.0 | ||
| 138 | $self->{latest}->set('title', $text) if ($self->{place} eq '/rdf:RDF/item/title'); | ||
| 139 | $self->{latest}->set('url', $text) if ($self->{place} eq '/rdf:RDF/item/link'); | ||
| 140 | $self->{latest}->set('description', $text) if ($self->{place} eq '/rdf:RDF/item/description'); | ||
| 141 | |||
| 142 | # set channel | ||
| 143 | $self->{title} = $text if ($self->{place} eq '/rss/channel/title'); | ||
| 144 | $self->{url} = $text if ($self->{place} eq '/rss/channel/link'); | ||
| 145 | $self->{description} = $text if ($self->{place} eq '/rss/channel/description'); | ||
| 146 | $self->{pubDate} = $text if ($self->{place} eq '/rss/channel/pubDate'); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub end { | ||
| 150 | my $self = shift; | ||
| 151 | my $tag = shift; | ||
| 152 | |||
| 153 | my $place = $self->{place}; | ||
| 154 | $place = substr($place, 0, length($place)-length($tag)-1); # regex here causes segmentation fault! | ||
| 155 | |||
| 156 | # compatibility with RSS1.0 | ||
| 157 | $self->{place} = $place; | ||
| 158 | $self->{title} = $tag if ($self->{place} eq '/rdf:RDF/channel/title'); | ||
| 159 | $self->{url} = $tag if ($self->{place} eq '/rdf:RDF/channel/link'); | ||
| 160 | $self->{description} = $tag if ($self->{place} eq '/rdf:RDF/channel/description'); | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | |||
| 165 | sub add { | ||
| 166 | my ($self) = shift; | ||
| 167 | |||
| 168 | my $it = new XML::RSS::Parser::Lite::Item(@_); | ||
| 169 | push(@{$self->{items}}, $it); | ||
| 170 | |||
| 171 | return $it; | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | sub count { | ||
| 176 | my $self = shift; | ||
| 177 | return scalar @{$self->{items}}; | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | sub get { | ||
| 182 | my $self = shift; | ||
| 183 | |||
| 184 | my $what = shift; | ||
| 185 | if ($what =~ /^\d*$/) { | ||
| 186 | return @{$self->{items}}[$what]; | ||
| 187 | } | ||
| 188 | |||
| 189 | return $self->{$what}; | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | 1; | ||
| 194 | |||
| 195 | __END__ |