| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of CPAN-WWW-Top100-Retrieve | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2014 by Apocalypse. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use strict; use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | # Declare our package | ||||||||||||||||||||||||||||
| 11 | package CPAN::WWW::Top100::Retrieve; | ||||||||||||||||||||||||||||
| 12 | # git description: f223abe | ||||||||||||||||||||||||||||
| 13 | 1 | $CPAN::WWW::Top100::Retrieve::VERSION = '1.001'; | |||||||||||||||||||||||||||
| 14 | 1 | our $AUTHORITY = 'cpan:APOCAL'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # ABSTRACT: Retrieves the CPAN Top100 data from http://ali.as/top100 | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # import the Moose stuff | ||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use MooseX::StrictConstructor 0.08; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Params::Coerce; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # get some utility stuff | ||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use HTML::TableExtract; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use CPAN::WWW::Top100::Retrieve::Dist; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use CPAN::WWW::Top100::Retrieve::Utils qw( default_top100_uri dbids type2dbid dbid2type ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | has 'debug' => ( | ||||||||||||||||||||||||||||
| 34 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 35 | is => 'rw', | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 37 | 1 | ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | has 'ua' => ( | ||||||||||||||||||||||||||||
| 40 | isa => 'LWP::UserAgent', | ||||||||||||||||||||||||||||
| 41 | is => 'rw', | ||||||||||||||||||||||||||||
| 42 | required => 0, | ||||||||||||||||||||||||||||
| 43 | lazy => 1, | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 45 | 0 | LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||||||||||
| 47 | 1 | ); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | has 'error' => ( | |||||||||||||||||||||||||||
| 50 | isa => 'Str', | ||||||||||||||||||||||||||||
| 51 | is => 'ro', | ||||||||||||||||||||||||||||
| 52 | writer => '_error', | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Taken from Moose::Cookbook::Basics::Recipe5 | ||||||||||||||||||||||||||||
| 56 | 1 | subtype 'My::Types::URI' => as class_type('URI'); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | coerce 'My::Types::URI' | ||||||||||||||||||||||||||||
| 59 | => from 'Object' | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | => via { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $_->isa('URI') ? $_ : Params::Coerce::coerce( 'URI', $_ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | => from 'Str' | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | => via { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 0 | URI->new( $_, 'http' ) | |||||||||||||||||||||||||||
| 66 | 1 | }; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | has 'uri' => ( | ||||||||||||||||||||||||||||
| 69 | isa => 'My::Types::URI', | ||||||||||||||||||||||||||||
| 70 | is => 'rw', | ||||||||||||||||||||||||||||
| 71 | required => 0, | ||||||||||||||||||||||||||||
| 72 | lazy => 1, | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 74 | 0 | default_top100_uri(); | |||||||||||||||||||||||||||
| 75 | }, | ||||||||||||||||||||||||||||
| 76 | 1 | coerce => 1, | |||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | has '_data' => ( | ||||||||||||||||||||||||||||
| 80 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 81 | is => 'ro', | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 83 | 1 | ); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub _retrieve { | ||||||||||||||||||||||||||
|
Subroutine
_retrieve: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 86 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Do we already have data? | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ( keys %{ $self->_data } > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | warn "Using cached data" if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | return 1; | |||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | warn "Starting retrieve run" if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Okay, get the data via LWP | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | warn "LWP->get( " . $self->uri . " )" if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | my $response = $self->ua->get( $self->uri ); | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ( $response->is_error ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | my $errstr = "LWP Error: " . $response->status_line . "\n" . $response->content; | |||||||||||||||||||||||||||
| 101 | 0 | $self->_error( $errstr ); | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | warn $errstr if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | return 0; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Parse it! | ||||||||||||||||||||||||||||
| 107 | 0 | return $self->_parse( $response->content ); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub _parse { | ||||||||||||||||||||||||||
|
Subroutine
_parse: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||
| 111 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | 0 | my $content = shift; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Get the tables! | ||||||||||||||||||||||||||||
| 115 | 0 | foreach my $dbid ( sort { $a <=> $b } @{ dbids() } ) { | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | warn "Parsing dbid $dbid..." if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my $table_error; | |||||||||||||||||||||||||||
| 119 | 0 | my $table = HTML::TableExtract->new( attribs => { id => "ds$dbid" }, error_handle => \$table_error ); | |||||||||||||||||||||||||||
| 120 | 0 | $table->parse( $content ); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ( ! $table->tables ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | my $errstr = "Unable to parse table $dbid"; | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | $errstr .= " $table_error" if length $table_error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | $self->_error( $errstr ); | |||||||||||||||||||||||||||
| 126 | 0 | ▶ | warn $errstr if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | return 0; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | foreach my $ts ( $table->tables ) { | |||||||||||||||||||||||||||
| 131 | # Store it in our data struct! | ||||||||||||||||||||||||||||
| 132 | 0 | my %cols; | |||||||||||||||||||||||||||
| 133 | 0 | foreach my $row ( $ts->rows ) { | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ( ! keys %cols ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | # First row, the headers! | ||||||||||||||||||||||||||||
| 136 | 0 | my $c = 0; | |||||||||||||||||||||||||||
| 137 | 0 | %cols = map { $_ => $c++ } @$row; | |||||||||||||||||||||||||||
| 138 | } else { | ||||||||||||||||||||||||||||
| 139 | # Make the object! | ||||||||||||||||||||||||||||
| 140 | my $obj = CPAN::WWW::Top100::Retrieve::Dist->new( | ||||||||||||||||||||||||||||
| 141 | ## no critic ( ProhibitAccessOfPrivateData ) | ||||||||||||||||||||||||||||
| 142 | 'dbid' => $dbid, | ||||||||||||||||||||||||||||
| 143 | 'type' => dbid2type( $dbid ), | ||||||||||||||||||||||||||||
| 144 | 'rank' => $row->[ $cols{ 'Rank' } ], | ||||||||||||||||||||||||||||
| 145 | 'author' => $row->[ $cols{ 'Author' } ], | ||||||||||||||||||||||||||||
| 146 | 'dist' => $row->[ $cols{ 'Distribution' } ], | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # ugly logic here, but needed to "collate" the different report types | ||||||||||||||||||||||||||||
| 149 | 'score' => ( exists $cols{ 'Dependencies' } ? $row->[ $cols{ 'Dependencies' } ] : | ||||||||||||||||||||||||||||
| 150 | ( exists $cols{ 'Dependents' } ? $row->[ $cols{ 'Dependents' } ] : | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | ( exists $cols{ 'Score' } ? $row->[ $cols{ 'Score' } ] : undef ) ) ), | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 152 | ); | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | push( @{ $self->_data->{ $dbid } }, $obj ); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | return 1; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | sub list { | ||||||||||||||||||||||||||
|
Subroutine
list: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 165 | 0 | my $type = shift; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | return if ! defined $type or ! length $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $type and length $type
MC/DC0%0/2
defined $type and length $type
defined $type
length $type
Truth table0%0/3
A and B
A= defined $typeB= length $type
| |||||||||||||||||||||||||||||
| 168 | 0 | $type = type2dbid( lc( $type ) ); | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | return if ! defined $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # if we haven't retrieved yet, do it! | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | return if ! $self->_retrieve; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # Generate a copy of our data | ||||||||||||||||||||||||||||
| 175 | 0 | my @r = ( @{ $self->_data->{ $type } } ); | |||||||||||||||||||||||||||
| 176 | 0 | return \@r; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # from Moose::Manual::BestPractices | ||||||||||||||||||||||||||||
| 180 | 1 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 181 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 1 | 1; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | __END__ | ||||||||||||||||||||||||||||