| 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
| |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | ua($self, $args{ua}) if $args{ua}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 0 | ▶ | $self->{ua} = LWP::UserAgent->new() unless $self->{ua}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my $url = ($path =~ /^htt/ ? $path | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$self->{'limit'} || 0
$self->{'limit'}
Truth table0%0/2
A
A= $self->{'limit'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $ua and ref $ua eq "LWP::UserAgent"
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"
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $limit and $limit =~ /^\d+$/
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+$/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $linkref = $linkref->[7] if ref($linkref) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | return unless $linkref->{id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $str .= "notes = ".$linkref->{notes}."\n" if $linkref->{notes}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $str .= " pop = ".$linkref->{pop}."\n" if $linkref->{pop}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $str .= " date = ".$linkref->{date}."\n" if $linkref->{date}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | $str .= " tags = ".join(', ', sort keys %{$linkref->{tag}})."\n" | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | if $linkref->{tag}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 84 | 0 | my $rs = $ua->get($url); | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | return unless $rs->is_success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | for my $str (split /<a class="tag" /, $scrap) { | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | next unless my($tag) = $str =~ /^href=.*?>(.*?)<\/a>/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | $linkset{$id}{tag}{$tag}++; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | delete $linkset{$id}{tag} unless keys %{$linkset{$id}{tag}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my ($pop) = $scrap =~ /a class="pop".*?>.*?by (\d+) /si; | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | $linkset{$id}{pop} = $pop if $pop; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | my ($date) = $scrap =~ / class="date" title="(.*?)"/si; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | $linkset{$id}{date} = $date if $date; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | last if ++$num == $limit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | last unless my($page0,$page1) = $html =~ / page (\d+) of (\d+)/si; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | last if $page0 == $page1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | $page = $page ? $page + 1 : 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | return %linkset; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | 1; | |||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||