| 1 | package POE::Component::WWW::Google::PageRank; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | our $VERSION = '1.001004'; # VERSION | |||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use POE (qw( Wheel::Run Filter::Reference Filter::Line )); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use WWW::Google::PageRank; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | sub spawn { | ||||||||||
| 13 | 1 | ▶ | my $package = shift; | ||||||||
|
Subroutine
spawn: called CC=5 SCAR=16.6
Pod
undocumented
| |||||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | croak "$package requires an even number of arguments" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | if @_ & 1; | ||||||||||
| 17 | |||||||||||
| 18 | 1 | my %params = @_; | |||||||||
| 19 | |||||||||||
| 20 | 1 | $params{ lc $_ } = delete $params{ $_ } for keys %params; | |||||||||
| 21 | |||||||||||
| 22 | delete $params{options} | ||||||||||
| 23 | 1 | ▶ | unless ref $params{options} eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | |||||||||||
| 25 | 1 | my $self = bless \%params, $package; | |||||||||
| 26 | |||||||||||
| 27 | $self->{session_id} = POE::Session->create( | ||||||||||
| 28 | object_states => [ | ||||||||||
| 29 | $self => { | ||||||||||
| 30 | rank => '_rank', | ||||||||||
| 31 | shutdown => '_shutdown', | ||||||||||
| 32 | }, | ||||||||||
| 33 | $self => [ | ||||||||||
| 34 | qw( | ||||||||||
| 35 | _start | ||||||||||
| 36 | _child_error | ||||||||||
| 37 | _child_closed | ||||||||||
| 38 | _child_stderr | ||||||||||
| 39 | _child_stdout | ||||||||||
| 40 | _sig_chld | ||||||||||
| 41 | ) | ||||||||||
| 42 | ], | ||||||||||
| 43 | ], | ||||||||||
| 44 | 1 | ▶ | ( defined $params{options} ? ( options => $params{options} ) : () ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | )->ID; | ||||||||||
| 46 | |||||||||||
| 47 | 1 | return $self; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | sub _start { | ||||||||||
| 51 | 1 | ▶ | my ( $kernel, $self ) = @_[ KERNEL, OBJECT ]; | ||||||||
|
Subroutine
_start: called CC=4 SCAR=14.6
| |||||||||||
| 52 | 1 | $self->{session_id} = $_[SESSION]->ID(); | |||||||||
| 53 | |||||||||||
| 54 | 1 | ▶ | if ( $self->{alias} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 1 | $kernel->alias_set( $self->{alias} ); | |||||||||
| 56 | } | ||||||||||
| 57 | else { | ||||||||||
| 58 | 0 | $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ ); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 1 | ▶ | $self->{wheel} = POE::Wheel::Run->new( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | Program => \&_rank_wheel, | ||||||||||
| 63 | ErrorEvent => '_child_error', | ||||||||||
| 64 | CloseEvent => '_child_closed', | ||||||||||
| 65 | StderrEvent => '_child_stderr', | ||||||||||
| 66 | StdoutEvent => '_child_stdout', | ||||||||||
| 67 | StdioFilter => POE::Filter::Reference->new, | ||||||||||
| 68 | StderrFilter => POE::Filter::Line->new, | ||||||||||
| 69 | ( $^O eq 'MSWin32' ? ( CloseOnCall => 0 ) : ( CloseOnCall => 1 ) ), | ||||||||||
| 70 | ); | ||||||||||
| 71 | |||||||||||
| 72 | $kernel->yield('shutdown') | ||||||||||
| 73 | 1 | ▶ | unless $self->{wheel}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | |||||||||||
| 75 | 1 | $kernel->sig_child( $self->{wheel}->PID, '_sig_chld' ); | |||||||||
| 76 | |||||||||||
| 77 | 1 | undef; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | sub _sig_chld { | ||||||||||
| 81 | 1 | ▶ | $poe_kernel->sig_handled; | ||||||||
|
Subroutine
_sig_chld: called CC=1 SCAR=0.0
| |||||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | sub session_id { | ||||||||||
| 85 | 0 | ▶ | return $_[0]->{session_id}; | ||||||||
|
Subroutine
session_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | sub rank { | ||||||||||
| 89 | 2 | ▶ | my $self = shift; | ||||||||
|
Subroutine
rank: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 90 | 2 | $poe_kernel->post( $self->{session_id} => 'rank' => @_ ); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | sub _rank { | ||||||||||
| 94 | 3 | ▶ | my ( $kernel, $self ) = @_[ KERNEL, OBJECT ]; | ||||||||
|
Subroutine
_rank: called CC=10 SCAR=27.7
| |||||||||||
| 95 | 3 | my $sender = $_[SENDER]->ID; | |||||||||
| 96 | |||||||||||
| 97 | return | ||||||||||
| 98 | 3 | ▶ | if $self->{shutdown}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | |||||||||||
| 100 | 3 | my $args; | |||||||||
| 101 | |||||||||||
| 102 | 3 | ▶ | if ( ref $_[ARG0] eq 'HASH' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 3 | $args = { %{ $_[ARG0] } }; | |||||||||
| 104 | } | ||||||||||
| 105 | else { | ||||||||||
| 106 | 0 | warn "First parameter must be a hashref... trying to adjust"; | |||||||||
| 107 | 0 | $args = { @_[ARG0 .. $#_] }; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | $args->{ lc $_ } = delete $args->{ $_ } | ||||||||||
| 111 | 3 | for grep { !/^_/ } keys %{ $args }; | |||||||||
| 112 | |||||||||||
| 113 | 3 | ▶ | unless ( $args->{event} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 0 | warn "No `event` parameter was specified"; | |||||||||
| 115 | 0 | return; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 3 | ▶ | unless ( $args->{page} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 0 | warn "No `page` parameter was specified"; | |||||||||
| 120 | 0 | return; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | # not sure if this is mandatory but WWW::Google:PageRank rejects URIs | ||||||||||
| 124 | # not matching this regex, so we'll fix it up and hope it won't | ||||||||||
| 125 | # eat us | ||||||||||
| 126 | 3 | ▶ | unless ( $args->{page} =~ m#^https?://#i ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | warn "Parameter `page` does not match m#^https?://#i" | ||||||||||
| 128 | 0 | ▶ | if $self->{debug}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | |||||||||||
| 130 | 0 | $args->{page} = "http://$args->{page}"; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 3 | ▶ | if ( $args->{session} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 1 | ▶ | if ( my $ref = $kernel->alias_resolve( $args->{session} ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 1 | $args->{sender} = $ref->ID; | |||||||||
| 136 | } | ||||||||||
| 137 | else { | ||||||||||
| 138 | 0 | warn "Parameter `session` did not resolve to an active POE" | |||||||||
| 139 | . " session, aborting"; | ||||||||||
| 140 | 0 | return; | |||||||||
| 141 | } | ||||||||||
| 142 | } | ||||||||||
| 143 | else { | ||||||||||
| 144 | 2 | $args->{sender} = $sender; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 3 | $kernel->refcount_increment( $args->{sender} => __PACKAGE__ ); | |||||||||
| 148 | 3 | $self->{wheel}->put( $args ); | |||||||||
| 149 | |||||||||||
| 150 | 3 | undef; | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | sub shutdown { | ||||||||||
| 154 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
shutdown: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 155 | 1 | $poe_kernel->post( $self->{session_id} => 'shutdown' => @_ ); | |||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | sub _shutdown { | ||||||||||
| 159 | 1 | ▶ | my ( $kernel, $self ) = @_[ KERNEL, OBJECT ]; | ||||||||
|
Subroutine
_shutdown: called CC=4 SCAR=14.1
| |||||||||||
| 160 | 1 | $kernel->alarm_remove_all; | |||||||||
| 161 | 1 | $kernel->alias_remove( $_ ) for $kernel->alias_list; | |||||||||
| 162 | $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) | ||||||||||
| 163 | 1 | ▶ | unless $self->{alias}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 164 | |||||||||||
| 165 | 1 | $self->{shutdown} = 1; | |||||||||
| 166 | $self->{wheel}->shutdown_stdin | ||||||||||
| 167 | 1 | ▶ | if $self->{wheel}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | |||||||||||
| 169 | 1 | undef; | |||||||||
| 170 | } | ||||||||||
| 171 | sub _child_closed { | ||||||||||
| 172 | 0 | ▶ | my ( $kernel, $self ) = @_[ KERNEL, OBJECT ]; | ||||||||
|
Subroutine
_child_closed: not called CC=3 SCAR=24.8
| |||||||||||
| 173 | |||||||||||
| 174 | warn "Got _child_closed() (@_[ARG0..$#_])" | ||||||||||
| 175 | 0 | ▶ | if $self->{debug}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | |||||||||||
| 177 | 0 | delete $self->{wheel}; | |||||||||
| 178 | $kernel->yield('shutdown') | ||||||||||
| 179 | 0 | ▶ | unless $self->{shutdown}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | |||||||||||
| 181 | 0 | undef; | |||||||||
| 182 | } | ||||||||||
| 183 | |||||||||||
| 184 | sub _child_error { | ||||||||||
| 185 | 1 | ▶ | my ( $kernel, $self ) = @_[ KERNEL, OBJECT ]; | ||||||||
|
Subroutine
_child_error: called CC=3 SCAR=11.3
| |||||||||||
| 186 | |||||||||||
| 187 | warn "Got _child_error() (@_[ARG0..$#_])" | ||||||||||
| 188 | 1 | ▶ | if $self->{debug}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | |||||||||||
| 190 | 1 | delete $self->{wheel}; | |||||||||
| 191 | $kernel->yield('shutdown') | ||||||||||
| 192 | 1 | ▶ | unless $self->{shutdown}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 193 | |||||||||||
| 194 | 1 | undef; | |||||||||
| 195 | } | ||||||||||
| 196 | |||||||||||
| 197 | sub _child_stderr { | ||||||||||
| 198 | 0 | ▶ | my ( $kernel, $self, $input ) = @_[ KERNEL, OBJECT, ARG0 ]; | ||||||||
|
Subroutine
_child_stderr: not called CC=2 SCAR=17.9
| |||||||||||
| 199 | warn "Got _child_stderr: $input\n" | ||||||||||
| 200 | 0 | ▶ | if $self->{debug}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 201 | |||||||||||
| 202 | 0 | undef; | |||||||||
| 203 | } | ||||||||||
| 204 | |||||||||||
| 205 | sub _child_stdout { | ||||||||||
| 206 | 3 | ▶ | my ( $kernel, $self, $input ) = @_[ KERNEL, OBJECT, ARG0 ]; | ||||||||
|
Subroutine
_child_stdout: called CC=1 SCAR=0.0
| |||||||||||
| 207 | |||||||||||
| 208 | 3 | my $session = delete $input->{sender}; | |||||||||
| 209 | 3 | my $event = delete $input->{event}; | |||||||||
| 210 | |||||||||||
| 211 | 3 | $kernel->post( $session => $event => $input ); | |||||||||
| 212 | 3 | $kernel->refcount_decrement( $session => __PACKAGE__ ); | |||||||||
| 213 | |||||||||||
| 214 | 3 | undef; | |||||||||
| 215 | } | ||||||||||
| 216 | |||||||||||
| 217 | sub _rank_wheel { | ||||||||||
| 218 | 0 | ▶ | if ( $^O eq 'MSWin32' ) { | ||||||||
|
Subroutine
_rank_wheel: not called CC=6 SCAR=37.4
Branch: 1 branch
| |||||||||||
| 219 | 0 | binmode STDIN; | |||||||||
| 220 | 0 | binmode STDOUT; | |||||||||
| 221 | } | ||||||||||
| 222 | |||||||||||
| 223 | 0 | my $raw; | |||||||||
| 224 | 0 | my $size = 4096; | |||||||||
| 225 | 0 | my $filter = POE::Filter::Reference->new; | |||||||||
| 226 | |||||||||||
| 227 | 0 | ▶ | while ( sysread STDIN, $raw, $size ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 228 | 0 | my $requests = $filter->get( [ $raw ] ); | |||||||||
| 229 | 0 | foreach my $req ( @$requests ) { | |||||||||
| 230 | my $page_rank | ||||||||||
| 231 | 0 | ▶ | = WWW::Google::PageRank->new( %{ $req->{options} || {} } ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 232 | |||||||||||
| 233 | 0 | @{ $req }{ qw(rank response) } | |||||||||
| 234 | 0 | = $page_rank->get( $req->{page} ); | |||||||||
| 235 | |||||||||||
| 236 | 0 | ▶ | unless ( $req->{response}->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 237 | 0 | @{ $req }{ qw(rank error) } | |||||||||
| 238 | 0 | = ( undef, $req->{response}->status_line ); | |||||||||
| 239 | } | ||||||||||
| 240 | |||||||||||
| 241 | 0 | my $response = $filter->put( [ $req ] ); | |||||||||
| 242 | 0 | print STDOUT @$response; | |||||||||
| 243 | } | ||||||||||
| 244 | } | ||||||||||
| 245 | } | ||||||||||
| 246 | |||||||||||
| 247 | 2 | 1; | |||||||||
| 248 | __END__ | ||||||||||