blib/lib/App/Horris/CLI/Command/feed.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 77 CC 4 SCAR 20.8
CC
4
Cov
0%
execute
20.0
BEGIN
2.0
BEGIN
2.0
CRAP 8.0
1 package App::Horris::CLI::Command::feed;
2 # ABSTRACT: rss feed update and notify
3  
4  
5 use Moose;
6 use Const::Fast;
7 use AnyEvent;
8 use AnyEvent::Feed;
9 use Encode;
10 use HTML::Strip;
11 use DBI;
12 use namespace::autoclean;
13 extends 'MooseX::App::Cmd::Command';
14  
15 const my @FEED_URLS => (
16 "http://www.blogger.com/feeds/5137684887780288527/posts/default",
17 "http://www.perl.com/pub/atom.xml",
18 "http://feeds.feedburner.com/YetAnotherCpanRecentChanges",
19 "http://jeen.tistory.com/rss",
20 "http://blogs.perl.org/atom.xml",
21 "http://planet.perl.org/rss20.xml",
22 "http://perlsphere.net/atom.xml",
23 "http://use.perl.org/index.rss",
24 "http://blogsearch.google.co.kr/blogsearch_feeds?q=perl+dancer+OR+catalyst+OR+anyevent+OR+moose&hl=ko&lr=&newwindow=1&prmdo=1&prmd=ivns&output=atom",
25 );
26  
27 has database => (
28 is => 'ro',
29 isa => 'Str',
30 traits => ['Getopt'],
31 required => 1,
32 documentation => "sqlite3 database file",
33 );
34  
35 has interval => (
36 is => 'rw',
37 isa => 'Int',
38 traits => ['Getopt'],
39 default => 300,
40 cmd_aliases => 'i',
41 documentation => "polling interval time as seconds. default is 300 seconds",
42 );
43  
44 sub execute {
45 my ( $self, $opt, $args ) = @_;
46  
47 my $dbh = DBI->connect( "dbi:SQLite:dbname=" . $self->database, "", "" );
48 my $sth_insert = $dbh->prepare("insert into messages values (?, ?, 0, ?)");
49 my $hs = HTML::Strip->new();
50 my $w = AnyEvent->condvar;
51 my @feeders;
52 my %firstfeed;
53 foreach my $url (@FEED_URLS) {
54 my $feed_reader;
55 $feed_reader = AnyEvent::Feed->new(
56 url => $url,
57 interval => $self->interval,
58  
59 on_fetch => sub {
60 my ( $feed_reader, $new_entries, $feed, $error ) = @_;
61 if ( defined $error ) {
62 warn "ERROR: $error\n";
63 return;
64 }
65  
66 #warn "$url\n";
67 unless ( $firstfeed{ $feed->link } ) {
68 warn "Skip the first feeding. :: $url\n";
69 $firstfeed{ $feed->link }++;
70 return;
71 }
72 warn "\n";
73  
74 printf "Added %d entries..\n", scalar @$new_entries;
75 for (@$new_entries) {
76  
77 my ( $hash, $entry ) = @$_;
78  
79 my $body =
80 $hs->parse( encode( 'utf8', $entry->content->body ) );
81 $body =~ s/[\r\n]//g;
82 $body =~ s/\s+/ /g;
83  
84 my $message = sprintf "%s :: %s :: %s\n\n",
85 encode( 'utf8', $entry->title ),
86 substr( $body, 0, 100 ),
87 $entry->link;
88  
89 my $issue_time =
90 $entry->issued ? $entry->issued->epoch : time;
91 $sth_insert->execute( 'rss_atom', $issue_time, $message );
92 }
93 }
94 );
95 push @feeders, $feed_reader;
96 }
97  
98 $w->recv;
99 }
100  
101 1;
102  
103 __END__