| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | WebService::Hatena::BookmarkCount -- Interface for Hatena::Bookmark's XML-RPC API | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | use WebService::Hatena::BookmarkCount; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | my @list = ( | ||||||||||||||||||||||||||||
| 10 | 'http://www.hatena.ne.jp/info/webservices', | ||||||||||||||||||||||||||||
| 11 | 'http://www.kawa.net/works/perl/hatena/bookmarkcount.html', | ||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||
| 13 | my $hash = WebService::Hatena::BookmarkCount->getCount( @list ); | ||||||||||||||||||||||||||||
| 14 | foreach my $url ( @list ) { | ||||||||||||||||||||||||||||
| 15 | printf( "%5d %s\n", $hash->{$url}, $url ); | ||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | my $top = 'http://japan.cnet.com/'; | ||||||||||||||||||||||||||||
| 19 | my $total = WebService::Hatena::BookmarkCount->getTotalCount( $top ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | WebService::Hatena::BookmarkCount is a interface for the Hatena::Bookmark | ||||||||||||||||||||||||||||
| 24 | Web Services's XML-RPC API. This provides two methods, I<bookmark.getCount> | ||||||||||||||||||||||||||||
| 25 | and I<bookmark.getTotalCount>, to get numbers of count on bookmarks. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 METHODS | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head3 $hash = WebService::Hatena::BookmarkCount->getCount( @list ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | This method makes a I<bookmark.getCount> XML-RPC call for the Hatena::Bookmark | ||||||||||||||||||||||||||||
| 32 | Web Services. C<@list> is list of URLs to get a number of registrations in | ||||||||||||||||||||||||||||
| 33 | Hatena::Bookmark. This method returns a reference for a hash, which keys are | ||||||||||||||||||||||||||||
| 34 | URLs and which values are counts returned by the Hatena Web Services. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head3 $hash = WebService::Hatena::BookmarkCount->getTotalCount( $url ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | This method makes a I<bookmark.getTotalCount> XML-RPC call for | ||||||||||||||||||||||||||||
| 39 | the Hatena::Bookmark Web Services. C<$url> is the URL to get a number of | ||||||||||||||||||||||||||||
| 40 | registrations in Hatena::Bookmark. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 MODULE DEPENDENCIES | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | L<XML::TreePP> | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | L<LWP::UserAgent> or L<HTTP::Lite> | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Hatena Bookmark | ||||||||||||||||||||||||||||
| 51 | http://b.hatena.ne.jp/ | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | Documents in Japanese | ||||||||||||||||||||||||||||
| 54 | http://www.kawa.net/works/perl/hatena/bookmarkcount.html | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Yusuke Kawasaki http://www.kawa.net/ | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | The following copyright notice applies to all the files provided in | ||||||||||||||||||||||||||||
| 63 | this distribution, including binary files, unless explicitly noted | ||||||||||||||||||||||||||||
| 64 | otherwise. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Copyright 2006-2010 Yusuke Kawasaki | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 71 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | package WebService::Hatena::BookmarkCount; | ||||||||||||||||||||||||||||
| 76 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 2 | ▶ | use XML::TreePP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 2 | $VERSION = "0.07"; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | my $XMLRPC_URL = 'http://b.hatena.ne.jp/xmlrpc'; | |||||||||||||||||||||||||||
| 84 | 2 | my $WAIT_SECS = 1; | |||||||||||||||||||||||||||
| 85 | 2 | my $MAX_REQUEST = 40; | |||||||||||||||||||||||||||
| 86 | 2 | my $TREEPP_OPTIONS = { force_array => [qw( member )] }; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | 1 | my $package = shift; | |||||||||||||||||||||||||||
| 90 | 1 | my $self = {@_}; | |||||||||||||||||||||||||||
| 91 | 1 | bless $self, $package; | |||||||||||||||||||||||||||
| 92 | 1 | $self->{treepp} = XML::TreePP->new(%$TREEPP_OPTIONS); | |||||||||||||||||||||||||||
| 93 | 1 | $self; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | sub getCount { | ||||||||||||||||||||||||||
|
Subroutine
getCount: called CC=11 SCAR=28.7
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 1 | ▶ | my $self = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 1 | ▶ | $self = $self->new() unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 1 | my $links = [@_]; # copy | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | my $outhash = {}; | |||||||||||||||||||||||||||
| 102 | 1 | my $reqxml; | |||||||||||||||||||||||||||
| 103 | my $resxml; | ||||||||||||||||||||||||||||
| 104 | 1 | my $sleep = $WAIT_SECS; | |||||||||||||||||||||||||||
| 105 | 1 | my $tpp = $self->{treepp}; | |||||||||||||||||||||||||||
| 106 | 1 | ▶ | while ( scalar @$links ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 1 | my @splice = splice( @$links, 0, $MAX_REQUEST ); | |||||||||||||||||||||||||||
| 108 | 1 | my $param = [ map { { value => { string => $_ } }; } @splice ]; | |||||||||||||||||||||||||||
| 109 | 1 | my $reqtree = { | |||||||||||||||||||||||||||
| 110 | methodCall => { | ||||||||||||||||||||||||||||
| 111 | methodName => "bookmark.getCount", | ||||||||||||||||||||||||||||
| 112 | params => { param => $param } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||
| 115 | 1 | ▶ | $reqxml = $tpp->write($reqtree) or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 1 | my $tree; | |||||||||||||||||||||||||||
| 117 | 1 | ( $tree, $resxml ) = $tpp->parsehttp( POST => $XMLRPC_URL, $reqxml ); | |||||||||||||||||||||||||||
| 118 | 1 | ▶ | last unless ref $tree; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | &parse_res_struct( $tree, $outhash ); | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | sleep( $sleep++ ) if scalar @$links; # wait | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 1 | ▶ | $outhash = undef unless scalar keys %$outhash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 1 | ▶ | return if ( !$outhash && !wantarray ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$outhash or wantarray
MC/DC0%0/2
$outhash or wantarray
$outhash
wantarray
Truth table33%1/3
A or B
A= $outhashB= wantarray
| |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | wantarray ? ( $outhash, $reqxml, $resxml ) : $outhash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub getTotalCount { | ||||||||||||||||||||||||||
|
Subroutine
getTotalCount: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | my $self = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | $self = $self->new() unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my $url = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | my $reqtree = { | |||||||||||||||||||||||||||
| 133 | methodCall => { | ||||||||||||||||||||||||||||
| 134 | methodName => "bookmark.getTotalCount", | ||||||||||||||||||||||||||||
| 135 | params => { | ||||||||||||||||||||||||||||
| 136 | param => { | ||||||||||||||||||||||||||||
| 137 | value => { | ||||||||||||||||||||||||||||
| 138 | string => $url, | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | }; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | my $tpp = $self->{treepp}; | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | my $reqxml = $tpp->write($reqtree) or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | my( $tree, $resxml ) = $tpp->parsehttp( POST => $XMLRPC_URL, $reqxml ); | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | return unless ref $tree; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | my $count = &parse_res_simple( $tree ); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | wantarray ? ( $count, $reqxml, $resxml ) : $count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | sub parse_res_struct { | ||||||||||||||||||||||||||
|
Subroutine
parse_res_struct: not called CC=15 SCAR=54.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | my $tree = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | my $hash = shift || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | return unless ref $tree; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | return unless ref $tree->{methodResponse}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | return unless ref $tree->{methodResponse}->{params}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | return unless ref $tree->{methodResponse}->{params}->{param}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | my $param = $tree->{methodResponse}->{params}->{param}; | |||||||||||||||||||||||||||
| 162 | 0 | ▶ | return unless ref $param->{value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | return unless ref $param->{value}->{struct}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | my $array = $param->{value}->{struct}->{member}; | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | return unless ref $array; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | return unless scalar @$array; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | foreach my $member (@$array) { | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | next unless defined $member->{name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | next unless ref $member->{value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | my $name = $member->{name}; | |||||||||||||||||||||||||||
| 172 | 0 | my $value = $member->{value}; | |||||||||||||||||||||||||||
| 173 | 0 | ▶ | my $type = ( sort keys %$value )[0] or next; # first value | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | $hash->{$name} = $value->{$type}; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 0 | $hash; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | sub parse_res_simple { | ||||||||||||||||||||||||||
|
Subroutine
parse_res_simple: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my $tree = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | return unless ref $tree; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | return unless ref $tree->{methodResponse}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | return unless ref $tree->{methodResponse}->{params}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | return unless ref $tree->{methodResponse}->{params}->{param}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 0 | my $param = $tree->{methodResponse}->{params}->{param}; | |||||||||||||||||||||||||||
| 186 | 0 | ▶ | return unless ref $param->{value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 0 | my $int = $param->{value}->{int}; | |||||||||||||||||||||||||||
| 188 | 0 | $int; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 2 | 1; | |||||||||||||||||||||||||||