blib/lib/WWW/Scraper/Delicious.pm

filter: Statementstmt 13.9% 11 / 79 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 30.0% 3 / 10 Podpod 100% 6 / 6 totaltotal 12.0% 20 / 166 CC 35 SCAR 46.8
CC
35
Cov
8%
_scrape
240.0
dumplink
90.0
getlinks
30.0
CRAP 107.4
↑ Summary
1 package WWW::Scraper::Delicious;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.10';
9  
10 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
11 0 my ($class, %args) = @_;
12 0 my $self = {};
13 0 ▶ limit($self, $args{limit}) if $args{limit};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"limit"} 0 0 0
14 0 ▶ ua($self, $args{ua}) if $args{ua};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"ua"} 0 0 0
15 0 ▶ $self->{ua} = LWP::UserAgent->new() unless $self->{ua};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ua"} 0 0 0
16 0 bless($self, $class);
17 0 return $self;
18 }
19  
20 0 ▶sub getlinks {
Subroutine getlinks: not called CC=5 SCAR=34.0
Pod documented
21 0 my ($self, $path) = @_;
22 0 ▶ return unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
23 0 ▶ my $url = ($path =~ /^htt/ ? $path
Branch: 2 branches
BranchTrueFalseTotal
$path =~ /^htt/ ? : 0 0 0
$path =~ m[^/] ? : 0 0 0
24 : ( $path =~ /^\// ? "http://del.icio.us$path"
25 : "http://del.icio.us/$path" ) );
26 0 ▶ my $limit = $self->{limit} || 0;
Condition0%0/2
$self->{'limit'} || 0
l!l
00
MC/DC0%0/1
$self->{'limit'} || 0
$self->{'limit'}
Truth table0%0/2
A
A= $self->{'limit'}
Aresult
11
00
27 0 my %linkset = _scrape($self->{ua}, $url, $limit);
28 0 return %linkset;
29 }
30  
31 0 ▶sub getlinksarray {
Subroutine getlinksarray: not called CC=2 SCAR=17.9
Pod documented
32 0 my ($self, $path) = @_;
33 0 my %linkset = getlinks($self, $path);
34 0 ▶ return unless keys %linkset;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %linkset 0 0 0
35 0 my @table = sort { $b->[5] cmp $a->[5] }
36 0 map { [ $linkset{$_}{id},
37 $linkset{$_}{url},
38 $linkset{$_}{desc},
39 $linkset{$_}{notes},
40 $linkset{$_}{pop},
41 $linkset{$_}{date},
42 0 $linkset{$_}{tag}, $linkset{$_} ]
43 } keys %linkset;
44 0 return @table;
45 }
46  
47 0 ▶sub ua {
Subroutine ua: not called CC=3 SCAR=24.8
Pod documented
48 0 my ($self, $ua) = @_;
49 0 ▶ $self->{ua} = $ua if defined $ua
Branch: 1 branch
BranchTrueFalseTotal
if defined $ua and ref $ua eq "LWP::UserAgent" 0 0 0
Condition0%0/3
defined $ua and ref $ua eq "LWP::UserAgent"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ua and ref $ua eq "LWP::UserAgent"
defined $ua ref $ua eq "LWP::UserAgent"
Truth table0%0/3
A and B
A= defined $uaB= ref $ua eq "LWP::UserAgent"
ABresult
0X0
100
111
50 && ref($ua) eq 'LWP::UserAgent';
51 0 return $self->{ua};
52 }
53  
54 0 ▶sub limit { # limit of 0 is default (unlimited)
Subroutine limit: not called CC=3 SCAR=24.8
Pod documented
55 0 my ($self, $limit) = @_;
56 0 ▶ $self->{limit} = $limit if defined $limit && $limit =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit and $limit =~ /^\d+$/ 0 0 0
Condition0%0/3
defined $limit and $limit =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $limit and $limit =~ /^\d+$/
defined $limit $limit =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $limitB= $limit =~ /^\d+$/
ABresult
0X0
100
111
57 0 return $self->{limit};
58 }
59  
60 0 ▶sub dumplink {
Subroutine dumplink: not called CC=9 SCAR=45.0
Pod documented
61 0 my ($self, $linkref) = @_;
62 0 ▶ return unless $linkref;
Branch: 1 branch
BranchTrueFalseTotal
unless $linkref 0 0 0
63 0 ▶ $linkref = $linkref->[7] if ref($linkref) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $linkref eq "ARRAY" 0 0 0
64 0 ▶ return unless $linkref->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $linkref->{"id"} 0 0 0
65 0 my $str = " id = ".$linkref->{id}."\n";
66 0 $str .= " url = ".$linkref->{url}."\n";
67 0 ▶ $str .= " desc = ".$linkref->{desc}."\n" if $linkref->{desc};
Branch: 1 branch
BranchTrueFalseTotal
if $linkref->{"desc"} 0 0 0
68 0 ▶ $str .= "notes = ".$linkref->{notes}."\n" if $linkref->{notes};
Branch: 1 branch
BranchTrueFalseTotal
if $linkref->{"notes"} 0 0 0
69 0 ▶ $str .= " pop = ".$linkref->{pop}."\n" if $linkref->{pop};
Branch: 1 branch
BranchTrueFalseTotal
if $linkref->{"pop"} 0 0 0
70 0 ▶ $str .= " date = ".$linkref->{date}."\n" if $linkref->{date};
Branch: 1 branch
BranchTrueFalseTotal
if $linkref->{"date"} 0 0 0
71 0 $str .= " tags = ".join(', ', sort keys %{$linkref->{tag}})."\n"
72 0 ▶ if $linkref->{tag};
Branch: 1 branch
BranchTrueFalseTotal
if $linkref->{"tag"} 0 0 0
73 0 return $str;
74 }
75  
76 0 ▶sub _scrape {
Subroutine _scrape: not called CC=15 SCAR=54.8
77 0 my ($ua, $url, $limit) = @_;
78 0 my (%linkset, $page);
79 0 my $num = 0;
80  
81 0 while (1) {
82  
83 0 ▶ my $url = "$url?setcount=100" . ($page ? "&page=$page" : '');
Branch: 1 branch
BranchTrueFalseTotal
$page ? : 0 0 0
84 0 my $rs = $ua->get($url);
85 0 ▶ return unless $rs->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $rs->is_success 0 0 0
86 0 my $html = $rs->content;
87  
88 0 my @tmp = split /<li class="post" key="/si, $html;
89 0 for my $scrap (@tmp[1..$#tmp]) {
90 0 $scrap =~ s/\s*<\/li>.*$//si;
91  
92 0 ▶ next unless (my ($id, $url, $desc) = $scrap =~
Branch: 1 branch
BranchTrueFalseTotal
unless (() = $scrap =~ m[^(.*?)".*?a href="(.*?)".*?>(.*?)</a>]is) == 3 0 0 0
93 /^(.*?)".*?a href="(.*?)".*?>(.*?)<\/a>/si) == 3;
94 0 $linkset{$id} = { id => $id, url => $url, desc => $desc };
95  
96 0 my ($notes) = $scrap =~ /class="notes">(.*?)<\/p>/si;
97 0 ▶ $linkset{$id}{notes} = $notes if $notes;
Branch: 1 branch
BranchTrueFalseTotal
if $notes 0 0 0
98  
99 0 for my $str (split /<a class="tag" /, $scrap) {
100 0 ▶ next unless my($tag) = $str =~ /^href=.*?>(.*?)<\/a>/;
Branch: 1 branch
BranchTrueFalseTotal
unless my($tag) = $str =~ m[^href=.*?>(.*?)</a>] 0 0 0
101 0 $linkset{$id}{tag}{$tag}++;
102 }
103 0 ▶ delete $linkset{$id}{tag} unless keys %{$linkset{$id}{tag}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$linkset{$id}{"tag"};} 0 0 0
104  
105 0 my ($pop) = $scrap =~ /a class="pop".*?>.*?by (\d+) /si;
106 0 ▶ $linkset{$id}{pop} = $pop if $pop;
Branch: 1 branch
BranchTrueFalseTotal
if $pop 0 0 0
107
108 0 my ($date) = $scrap =~ / class="date" title="(.*?)"/si;
109 0 ▶ $linkset{$id}{date} = $date if $date;
Branch: 1 branch
BranchTrueFalseTotal
if $date 0 0 0
110  
111 0 ▶ last if ++$num == $limit;
Branch: 1 branch
BranchTrueFalseTotal
if ++$num == $limit 0 0 0
112 }
113
114 0 ▶ last unless my($page0,$page1) = $html =~ / page (\d+) of (\d+)/si;
Branch: 1 branch
BranchTrueFalseTotal
unless () = $html =~ / page (\d+) of (\d+)/is 0 0 0
115 0 ▶ last if $page0 == $page1;
Branch: 1 branch
BranchTrueFalseTotal
if $page0 == $page1 0 0 0
116 0 ▶ $page = $page ? $page + 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$page ? : 0 0 0
117 }
118  
119 0 return %linkset;
120 }
121  
122 1 1;
123 __END__
↑ Summary