| 1 | package Plagger::Plugin::Subscription::Odeo; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin::Subscription::OPML ); | ||
| 4 | |||
| 5 | use Plagger::UserAgent; | ||
| 6 | use URI::Escape; | ||
| 7 | use HTML::Entities; | ||
| 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 | |||
| 21 | my $account = $self->conf->{account} | ||
| 22 | or $context->error("config 'account' is missing"); | ||
| 23 | |||
| 24 | my $uri = URI->new("http://www.odeo.com/profile/$account/opml.xml"); | ||
| 25 | $context->log(debug => "Fetch remote OPML from $uri"); | ||
| 26 | |||
| 27 | my $response = Plagger::UserAgent->new->get($uri); | ||
| 28 | unless ($response->is_success) { | ||
| 29 | $context->error("Fetch $uri failed: ". $response->status_line); | ||
| 30 | } | ||
| 31 | |||
| 32 | my $xml = $response->content; | ||
| 33 | |||
| 34 | # fix Odeo's bad OPML | ||
| 35 | $xml =~ s{<outline text="(.*?)" type="link" url="(http://.*?)" count="(\d+)"/>}{ | ||
| 36 | my($title, $url, $count) = ($1, $2, $3); | ||
| 37 | |||
| 38 | $title = uri_unescape($title); | ||
| 39 | $title =~ s/\r\n//g; | ||
| 40 | $title =~ tr/\+/ /; | ||
| 41 | $title = encode_html($title); | ||
| 42 | $url = encode_html($url); | ||
| 43 | |||
| 44 | qq(<outline text="$title" type="rss" xmlUrl="$url" count="$count"/>) | ||
| 45 | }eg; | ||
| 46 | |||
| 47 | $self->load_opml($context, \$xml); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub encode_html { | ||
| 51 | HTML::Entities::encode($_[0], q("<>&)); | ||
| 52 | } | ||
| 53 | |||
| 54 | 1; | ||
| 55 | |||
| 56 | __END__ |