| 1 | package WWW::GoKGS; | ||||||||||||||||||||
| 2 | 15 | ▶ | use 5.008_009; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 15 | ▶ | use Carp qw/croak/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 15 | ▶ | use HTML::TreeBuilder::XPath; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 15 | ▶ | use LWP::RobotUA; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 15 | ▶ | use URI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 15 | ▶ | use WWW::GoKGS::Scraper::GameArchives; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 15 | ▶ | use WWW::GoKGS::Scraper::Top100; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 15 | ▶ | use WWW::GoKGS::Scraper::TournList; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 15 | ▶ | use WWW::GoKGS::Scraper::TournInfo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 15 | ▶ | use WWW::GoKGS::Scraper::TournEntrants; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 15 | ▶ | use WWW::GoKGS::Scraper::TournGames; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 15 | ▶ | use WWW::GoKGS::Scraper::TzList; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 15 | our $VERSION = '0.21'; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 7 | ▶ | sub _tree_builder_class { 'HTML::TreeBuilder::XPath' } | ||||||||||||||||||
|
Subroutine
_tree_builder_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||
| 22 | 1 | my $class = shift; | |||||||||||||||||||
| 23 | 0 | ▶ | my %args = @_ == 1 ? %{$_[0]} : @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | 1 | my $self = bless {}, $class; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 1 | for my $key (qw/user_agent/) { | |||||||||||||||||||
| 27 | 1 | ▶ | $self->{$key} = $args{$key} if exists $args{$key}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 1 | $self->init( \%args ); | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 1 | $self; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 1 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||
| 36 | 1 | my ( $self, $args ) = @_; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 1 | ▶ | unless ( exists $self->{user_agent} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 1 | my $class = ref $self; | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | $self->user_agent( | ||||||||||||||||||||
| 42 | LWP::RobotUA->new( | ||||||||||||||||||||
| 43 | agent => $args->{agent} || "$class/" . $class->VERSION, | ||||||||||||||||||||
| 44 | from => $args->{from}, | ||||||||||||||||||||
| 45 | cookie_jar => $args->{cookie_jar}, | ||||||||||||||||||||
| 46 | ) | ||||||||||||||||||||
| 47 | 1 | ▶ | ); | ||||||||||||||||||
|
Condition33%1/3
$args->{'agent'} || "$class/" . $class->VERSION
MC/DC0%0/2
$args->{'agent'} || "$class/" . $class->VERSION
$args->{'agent'}
"$class/" . $class->VERSION
Truth table33%1/3
A || B
A= $args->{'agent'}B= "$class/" . $class->VERSION
| |||||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 1 | return; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 12 | ▶ | sub user_agent { | ||||||||||||||||||
|
Subroutine
user_agent: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 54 | 12 | my $self = shift; | |||||||||||||||||||
| 55 | 12 | ▶ | $self->{user_agent} = shift if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 12 | $self->{user_agent}; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 1 | ▶ | sub agent { | ||||||||||||||||||
|
Subroutine
agent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 60 | 1 | my ( $self, @args ) = @_; | |||||||||||||||||||
| 61 | 1 | $self->user_agent->agent( @args ); | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 1 | ▶ | sub from { | ||||||||||||||||||
|
Subroutine
from: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 65 | 1 | my ( $self, @args ) = @_; | |||||||||||||||||||
| 66 | 1 | $self->user_agent->default_header( 'From', @args ); | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 1 | ▶ | sub cookie_jar { | ||||||||||||||||||
|
Subroutine
cookie_jar: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 70 | 1 | my ( $self, @args ) = @_; | |||||||||||||||||||
| 71 | 1 | $self->user_agent->cookie_jar( @args ); | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | ▶ | sub get { | ||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 75 | 0 | my ( $self, @args ) = @_; | |||||||||||||||||||
| 76 | 0 | $self->user_agent->get( @args ); | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 20 | ▶ | sub _scrapers { | ||||||||||||||||||
|
Subroutine
_scrapers: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 80 | 20 | my $self = shift; | |||||||||||||||||||
| 81 | 20 | ▶ | $self->{_scrapers} ||= $self->__build_scrapers; | ||||||||||||||||||
|
Condition66%2/3
$self->{"_scrapers"} ||= $self->__build_scrapers
MC/DC0%0/2
$self->{"_scrapers"} ||= $self->__build_scrapers
$self->{"_scrapers"}
$self->__build_scrapers
Truth table66%2/3
A ||= B
A= $self->{"_scrapers"}B= $self->__build_scrapers
| |||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 15 | ▶ | BEGIN { # install scrapers | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 85 | 15 | my %scrapers = ( | |||||||||||||||||||
| 86 | game_archives => 'WWW::GoKGS::Scraper::GameArchives', | ||||||||||||||||||||
| 87 | top_100 => 'WWW::GoKGS::Scraper::Top100', | ||||||||||||||||||||
| 88 | tourn_list => 'WWW::GoKGS::Scraper::TournList', | ||||||||||||||||||||
| 89 | tourn_info => 'WWW::GoKGS::Scraper::TournInfo', | ||||||||||||||||||||
| 90 | tourn_entrants => 'WWW::GoKGS::Scraper::TournEntrants', | ||||||||||||||||||||
| 91 | tourn_games => 'WWW::GoKGS::Scraper::TournGames', | ||||||||||||||||||||
| 92 | tz_list => 'WWW::GoKGS::Scraper::TzList', | ||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 15 | my %paths; | |||||||||||||||||||
| 96 | 15 | ▶ | while ( my ($method, $class) = each %scrapers ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 105 | my $path = $paths{$class} = $class->build_uri->path; | |||||||||||||||||||
| 98 | 105 | ▶ | my $body = sub { $_[0]->get_scraper($path) }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 99 | 15 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 100 | 105 | *$method = $body; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 1 | ▶ | sub __build_scrapers { | ||||||||||||||||||
|
Subroutine
__build_scrapers: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 104 | 1 | my $self = shift; | |||||||||||||||||||
| 105 | 1 | my $class = ref $self; | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 1 | my %_scrapers; | |||||||||||||||||||
| 108 | 1 | ▶ | while ( my ($scraper, $path) = each %paths ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | 7 | $_scrapers{$path} = $scraper->new( | |||||||||||||||||||
| 110 | _tree_builder_class => $class->_tree_builder_class, | ||||||||||||||||||||
| 111 | user_agent => $self->user_agent, | ||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 1 | \%_scrapers; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 18 | ▶ | sub get_scraper { | ||||||||||||||||||
|
Subroutine
get_scraper: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 120 | 18 | my ( $self, $path ) = @_; | |||||||||||||||||||
| 121 | 18 | $self->_scrapers->{$path}; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 2 | ▶ | sub each_scraper { | ||||||||||||||||||
|
Subroutine
each_scraper: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 125 | 2 | my ( $self, $code ) = @_; | |||||||||||||||||||
| 126 | 2 | my %scrapers = %{ $self->_scrapers }; | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 2 | ▶ | croak 'Not a CODE reference' unless ref $code eq 'CODE'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 1 | ▶ | while ( my ($path, $scraper) = each %scrapers ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | 7 | $code->( $path => $scraper ); | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 1 | return; | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 6 | ▶ | sub can_scrape { | ||||||||||||||||||
|
Subroutine
can_scrape: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 138 | 6 | my $self = shift; | |||||||||||||||||||
| 139 | 6 | my $uri = $self->_build_uri( shift ); | |||||||||||||||||||
| 140 | 6 | ▶ | my $path = $uri =~ m{^http://www\.gokgs\.com(?::80)?/} && $uri->path; | ||||||||||||||||||
|
Condition66%2/3
$uri =~ m[^http://www\.gokgs\.com(?::80)?/] && $uri->path
MC/DC50%1/2
$uri =~ m[^http://www\.gokgs\.com(?::80)?/] && $uri->path
$uri =~ m[^http://www\.gokgs\.com(?::80)?/]
$uri->path
Truth table66%2/3
A && B
A= $uri =~ m[^http://www\.gokgs\.com(?::80)?/]B= $uri->path
| |||||||||||||||||||||
| 141 | 6 | ▶ | $path ? $self->get_scraper( $path ) : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 1 | ▶ | sub scrape { | ||||||||||||||||||
|
Subroutine
scrape: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||
| 145 | 1 | my ( $self, $arg ) = @_; | |||||||||||||||||||
| 146 | 1 | my $uri = $self->_build_uri( $arg ); | |||||||||||||||||||
| 147 | 1 | my $scraper = $self->can_scrape( $uri ); | |||||||||||||||||||
| 148 | 1 | ▶ | croak "Don't know how to scrape '$arg'" unless $scraper; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 149 | 0 | $scraper->scrape( $self->get($uri), $uri ); | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 7 | ▶ | sub _build_uri { | ||||||||||||||||||
|
Subroutine
_build_uri: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 153 | 7 | my $self = shift; | |||||||||||||||||||
| 154 | 7 | my $uri = URI->new( shift ); | |||||||||||||||||||
| 155 | 7 | ▶ | $uri->scheme( 'http' ) unless $uri->scheme; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | 7 | ▶ | $uri->authority( 'www.gokgs.com' ) unless $uri->authority; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 157 | 7 | $uri; | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 15 | 1; | |||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||