root/trunk/plagger/lib/Plagger/Plugin/Subscription/OPML.pm

Revision 14 (checked in by miyagawa, 15 years ago)

fix root opml entries bug

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Plugin::Subscription::OPML;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Plagger::UserAgent;
6 use URI;
7 use XML::OPML;
8
9 sub register {
10     my($self, $context) = @_;
11
12     $context->register_hook(
13         $self,
14         'subscription.load' => \&load,
15     );
16 }
17
18 sub load {
19     my($self, $context) = @_;
20     my $uri = URI->new($self->conf->{url})
21         or $context->error("config 'url' is missing");
22
23     my $xml;
24     if ($uri->scheme =~ /^https?$/) {
25         $context->log(debug => "Fetch remote OPML from $uri");
26         my $response = Plagger::UserAgent->new->get($uri);
27         unless ($response->is_success) {
28             $context->error("Fetch $uri failed: ". $response->status_line);
29         }
30         $xml = $response->content;
31     }
32     elsif ($uri->scheme eq 'file') {
33         $context->log(debug => "Open local OPML file " . $uri->path);
34         open my $fh, '<', $uri->path
35             or $context->error( $uri->path . ": $!" );
36         $xml = join '', <$fh>;
37     }
38     else {
39         $context->error("Unsupported URI scheme: " . $uri->scheme);
40     }
41
42     my $opml = XML::OPML->new;
43     $opml->parse($xml);
44     for my $outline (@{ $opml->outline }) {
45         $self->walk_down($outline, $context, 0, []);
46     }
47 }
48
49 sub walk_down {
50     my($self, $outline, $context, $depth, $containers) = @_;
51
52     if (delete $outline->{opmlvalue}) {
53         my $title = delete $outline->{title};
54         push @$containers, $title if $depth > 0;
55         for my $channel (values %$outline) {
56             $self->walk_down($channel, $context, $depth + 1, $containers);
57         }
58         pop @$containers if $depth > 0;
59     } else {
60         my $feed = Plagger::Feed->new;
61         $feed->url($outline->{xmlUrl});
62         $feed->link($outline->{htmlUrl});
63         $feed->title($outline->{title});
64         $feed->tags($containers);
65         $context->subscription->add($feed);
66     }
67 }
68
69 1;
Note: See TracBrowser for help on using the browser.