| 1 | package Web::Scraper::Citations; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use version; our $VERSION = qv('0.0.2'); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Mojo::DOM; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Mojo::ByteStream 'b'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use File::Slurp::Tiny qw(read_file); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | # Module implementation here | ||||||||||||||||||||||||||||||||
| 17 | 1 | has 'id' => ( is => 'ro', isa => 'Str' ); | |||||||||||||||||||||||||||||||
| 18 | 1 | has 'citations' => ( is => 'ro', isa => 'Int' ); | |||||||||||||||||||||||||||||||
| 19 | 1 | has 'citations_last5' => ( is => 'ro', isa => 'Int' ); | |||||||||||||||||||||||||||||||
| 20 | 1 | has 'h' => ( is => 'ro', isa => 'Int' ); | |||||||||||||||||||||||||||||||
| 21 | 1 | has 'h_last5' => ( is => 'ro', isa => 'Int' ); | |||||||||||||||||||||||||||||||
| 22 | 1 | has 'i10' => ( is => 'ro', isa => 'Int' ); | |||||||||||||||||||||||||||||||
| 23 | 1 | has 'i10_last5' => ( is => 'ro', isa => 'Int' ); | |||||||||||||||||||||||||||||||
| 24 | 1 | has 'name' => ( is => 'ro', isa => 'Str' ); | |||||||||||||||||||||||||||||||
| 25 | 1 | has 'affiliation'=> ( is => 'ro', isa => 'Str' ); | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use constant STAT_NAMES => qw( citations citations_last5 h h_last5 i10 i10_last5 ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # Mojo functions | ||||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||||||
| 31 | 2 | my $orig = shift; | |||||||||||||||||||||||||||||||
| 32 | 2 | my $class = shift; | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | if ( @_ == 1 && $_[0] !~ /file:/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
@_ == 1 and not $_[0] =~ /file:/
MC/DC50%1/2
@_ == 1 and not $_[0] =~ /file:/
@_ == 1
not $_[0] =~ /file:/
Truth table66%2/3
A and not(B)
A= @_ == 1B= not $_[0] =~ /file:/
| |||||||||||||||||||||||||||||||||
| 35 | 1 | my $id; | |||||||||||||||||||||||||||||||
| 36 | 1 | ▶ | if ( $_[0] =~ /user=(\w+)/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | 0 | $id = $1; | |||||||||||||||||||||||||||||||
| 38 | } else { | ||||||||||||||||||||||||||||||||
| 39 | 1 | $id = $_[0]; | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | 1 | my $url = Mojo::URL->new("http://scholar.google.com/citations?user=$id"); | |||||||||||||||||||||||||||||||
| 42 | 1 | my $object = scrape_URL( $url ); | |||||||||||||||||||||||||||||||
| 43 | 1 | return $class->$orig( %$object ); | |||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | elsif ( $_[0] =~ /file:/ ) { | ||||||||||||||||||||||||||||||||
| 46 | 1 | my $url = Mojo::URL->new($_[0]); | |||||||||||||||||||||||||||||||
| 47 | 1 | my $object = scrape_URL( $url ); | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 1 | return $class->$orig( %$object); | |||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | 1 | }; | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | sub profile_stats { | ||||||||||||||||||||||||||||||
|
Subroutine
profile_stats: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 54 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 55 | 1 | my %stats; | |||||||||||||||||||||||||||||||
| 56 | 1 | map( $stats{$_} = $self->$_, STAT_NAMES ); | |||||||||||||||||||||||||||||||
| 57 | 1 | return \%stats; | |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | sub scrape_URL { | ||||||||||||||||||||||||||||||
|
Subroutine
scrape_URL: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 61 | 2 | my $url = shift; | |||||||||||||||||||||||||||||||
| 62 | 2 | my $object = {}; | |||||||||||||||||||||||||||||||
| 63 | 2 | my $dom; | |||||||||||||||||||||||||||||||
| 64 | my $text; | ||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | if ( $url =~ /http/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 66 | 1 | my $ua = Mojo::UserAgent->new( max_redirects => 5 ); | |||||||||||||||||||||||||||||||
| 67 | 1 | my $response = $ua->get( $url )->res; | |||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | if ( $response->code != 200 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | 0 | croak "Page not found with code ". $response->code. " and message ".$response->message; | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | 1 | $dom = $response->dom; | |||||||||||||||||||||||||||||||
| 72 | 1 | $text = $response->text; | |||||||||||||||||||||||||||||||
| 73 | } elsif ( $url =~ /file/ ) { | ||||||||||||||||||||||||||||||||
| 74 | 1 | my ($fn) = ($url =~ /file:(.+)/); | |||||||||||||||||||||||||||||||
| 75 | 1 | $text = read_file( $fn ); | |||||||||||||||||||||||||||||||
| 76 | 1 | ▶ | croak "Can't open file $fn" unless $text; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | 1 | $dom = Mojo::DOM->new( $text ); | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 2 | ▶ | croak "Error in downloaded text" if !$dom->at("#gsc_prf_in"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | 2 | $object->{'name'} = $dom->at("#gsc_prf_in")->text; | |||||||||||||||||||||||||||||||
| 82 | 2 | $object->{'affiliation'} = $dom->at( ".gsc_prf_il" )->all_text; | |||||||||||||||||||||||||||||||
| 83 | 2 | ($object->{'id'}) = ($text =~ /citations\?user=(\w+)/ ); | |||||||||||||||||||||||||||||||
| 84 | 2 | my @dom_stats = $dom->find(".gsc_rsb_std")->map('text')->each; | |||||||||||||||||||||||||||||||
| 85 | 2 | for my $stat ( STAT_NAMES ) { | |||||||||||||||||||||||||||||||
| 86 | 12 | $object->{$stat} = shift @dom_stats; | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | 2 | return $object; | |||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 1 | ▶ | no Moose; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 92 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 1 | "To an infinite H and beyond"; # Magic true value required at end of module | |||||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||||||