root/trunk/plagger/lib/Plagger/Plugin/CustomFeed/PerlMonks.pm

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

use XML::LibXML in CustomFeed?::PerlMonks?. Still needs to do parse_head in Aggregator::Simple

Line 
1 package Plagger::Plugin::CustomFeed::PerlMonks;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Plagger::UserAgent;
6 use Plagger::Util;
7 use URI;
8 use URI::QueryParam;
9 use XML::LibXML;
10
11 sub register {
12     my($self, $context) = @_;
13     $context->register_hook(
14         $self,
15         'customfeed.handle' => \&handle,
16     );
17 }
18
19 sub handle {
20     my ( $self, $context, $args ) = @_;
21
22     if ( $args->{feed}->url =~ /perlmonks.(?:com|org)\/\?node_id=30175$/ ) {
23         $self->aggregate( $context, $args );
24         return 1;
25     }
26
27     return;
28 }
29
30 sub aggregate {
31     my ( $self, $context, $args ) = @_;
32
33     my $url = URI->new( $args->{feed}->url );
34     $context->log(info => "GET $url");
35
36     my $agent = Plagger::UserAgent->new;
37     my $res   = $agent->fetch( $url, $self );
38
39     if ( $res->is_error ) {
40         $context->log( error => "GET $url failed: " . $res->status );
41         return;
42     }
43
44     my $content = Plagger::Util::decode_content($res);
45     my $title   = "Perl Monks Newest Nodes";
46
47     my $feed = Plagger::Feed->new;
48     $feed->title($title);
49     $feed->meta( $args->{feed}->meta );
50     $feed->link( $args->{feed}->url );
51
52     my $parser = XML::LibXML->new;
53     my $pm_doc = $parser->parse_string( $content );
54
55     my ($node) = $pm_doc->findnodes("/NEWESTNODES/INFO");
56
57     my @nodes = ();
58     for my $node ( $pm_doc->findnodes("/NEWESTNODES/NODE") ) {
59         my $type = $node->getAttribute( 'nodetype' );
60         next if $type eq "note" || $type eq "user";
61
62         my $new_node = {
63             author     => $node->getAttribute('authortitle'),
64             createtime => $node->getAttribute('createtime'),
65             title      => $node->textContent,
66         };
67
68         $new_node->{title} =~ s/\n//g;
69         $new_node->{link} = "http://perlmonks.org/?node_id="
70             . $node->getAttribute('node_id');
71
72         push @nodes, $new_node;
73     }
74
75     for my $node ( sort { $a->{createtime} <=> $b->{createtime} } @nodes ) {
76         my $entry = Plagger::Entry->new;
77         $entry->title( $node->{title} );
78         $entry->link( $node->{link} );
79         $entry->author( $node->{author} );
80
81         my $dt = Plagger::Date->strptime( "%Y%m%d%H%M%S", $node->{createtime} );
82         $dt->set_time_zone('America/New_York');
83         $entry->date( $dt );
84
85         $feed->add_entry($entry);
86     }
87
88     $context->update->add($feed);
89 }
90
91 1;
92
93 __END__
94
95 =head1 NAME
96
97 Plagger::Plugin::CustomFeed::PerlMonks - Perl Monks Newest Nodes Custom Feed
98
99 =head1 SYNOPSIS
100
101   - module: Subscription::Config
102     config:
103       feed:
104         - http://perlmonks.org/?node_id=30175
105
106   - module: CustomFeed::PerlMonks
107
108 =head1 DESCRIPTION
109
110 This plugin creates a custom feed off of the Perl Monks Newest Nodes
111 XML Feed.
112
113 =head1 AUTHOR
114
115 Jeff Bisbee
116
117 =head1 SEE ALSO
118
119 L<Plagger>
120
121 =cut
Note: See TracBrowser for help on using the browser.