| 1 | package Net::MyPeople::Bot; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use LWP::Protocol::https; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Data::Printer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use File::Util qw(SL); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Encode qw(is_utf8 _utf8_off); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | Log::Log4perl->easy_init($ERROR); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # ABSTRACT: Implements MyPeople-Bot. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | our $VERSION = '0.320'; # VERSION | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | has apikey=>( | |||||||||||||||||||||||||||
| 25 | is=>'rw', | ||||||||||||||||||||||||||||
| 26 | required=>1 | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | has web_proxy_base=>( | |||||||||||||||||||||||||||
| 31 | is=>'rw', | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | has ua=>( | ||||||||||||||||||||||||||||
| 35 | is=>'ro', | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | default=>sub{return LWP::UserAgent->new;}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 37 | 1 | ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | our $API_BASE = 'https://apis.daum.net/mypeople'; | |||||||||||||||||||||||||||
| 42 | 1 | our $API_SEND = $API_BASE . '/buddy/send.json'; | |||||||||||||||||||||||||||
| 43 | 1 | our $API_BUDDY = $API_BASE . '/profile/buddy.json'; | |||||||||||||||||||||||||||
| 44 | 1 | our $API_GROUP_MEMBERS = $API_BASE . '/group/members.json'; | |||||||||||||||||||||||||||
| 45 | 1 | our $API_GROUP_SEND = $API_BASE . '/group/send.json'; | |||||||||||||||||||||||||||
| 46 | 1 | our $API_GROUP_EXIT = $API_BASE . '/group/exit.json'; | |||||||||||||||||||||||||||
| 47 | 1 | our $API_FILE_DOWNLOAD = $API_BASE . '/file/download.json'; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | our $API_SEND_LENGTH = 1000; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub _call_file { | ||||||||||||||||||||||||||
|
Subroutine
_call_file: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 56 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 57 | 0 | my ($apiurl, $param, $path) = @_; | |||||||||||||||||||||||||||
| 58 | 0 | $apiurl .= '?apikey='.uri_escape($self->apikey); | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | $apiurl = $self->web_proxy_base.$apiurl if $self->web_proxy_base; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | my $req = POST( $apiurl, Content=>$param ); | |||||||||||||||||||||||||||
| 62 | 0 | DEBUG $req->as_string; | |||||||||||||||||||||||||||
| 63 | 0 | my $res = $self->ua->request( $req ); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if( $res->is_success ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | my $sl = SL; | |||||||||||||||||||||||||||
| 67 | 0 | $path =~ s@$sl$@@; | |||||||||||||||||||||||||||
| 68 | 0 | my $filepath; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | if( -d $path ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | $filepath = $path.SL.$res->filename; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | else{ | ||||||||||||||||||||||||||||
| 73 | 0 | $filepath = $path; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | DEBUG $filepath; | |||||||||||||||||||||||||||
| 76 | 0 | open my $fh, '>', $filepath; | |||||||||||||||||||||||||||
| 77 | 0 | binmode($fh); | |||||||||||||||||||||||||||
| 78 | 0 | print $fh $res->content; | |||||||||||||||||||||||||||
| 79 | 0 | close $fh; | |||||||||||||||||||||||||||
| 80 | 0 | return $filepath; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | else{ | ||||||||||||||||||||||||||||
| 83 | 0 | ERROR p $res; | |||||||||||||||||||||||||||
| 84 | 0 | return undef; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub _call_multipart { | ||||||||||||||||||||||||||
|
Subroutine
_call_multipart: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 88 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 89 | 0 | my ($apiurl, $param) = @_; | |||||||||||||||||||||||||||
| 90 | 0 | $apiurl .= '?apikey='.$self->apikey; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | $apiurl = $self->web_proxy_base.$apiurl if $self->web_proxy_base; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | #foreach my $k (keys %{$param}){ | ||||||||||||||||||||||||||||
| 94 | # $param->{$k} = uri_escape($param->{$k}); | ||||||||||||||||||||||||||||
| 95 | #} | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | my $req = POST( $apiurl, | |||||||||||||||||||||||||||
| 98 | Content_Type => 'form-data', | ||||||||||||||||||||||||||||
| 99 | Content => $param | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | 0 | DEBUG $req->as_string; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | my $res = $self->ua->request($req); | |||||||||||||||||||||||||||
| 104 | 0 | DEBUG p $res; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if( $res->is_success ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | return from_json( $res->content , {utf8 => 1} ); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | else{ | ||||||||||||||||||||||||||||
| 110 | 0 | ERROR p $res; | |||||||||||||||||||||||||||
| 111 | 0 | return undef; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub _call { | ||||||||||||||||||||||||||
|
Subroutine
_call: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 115 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 116 | 0 | my ($apiurl, $param) = @_; | |||||||||||||||||||||||||||
| 117 | 0 | $apiurl .= '?apikey='.uri_escape($self->apikey); | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | $apiurl = $self->web_proxy_base.$apiurl if $self->web_proxy_base; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | my $req = POST( $apiurl, | |||||||||||||||||||||||||||
| 121 | #Content_Type => 'form-data', | ||||||||||||||||||||||||||||
| 122 | Content=>$param | ||||||||||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||||||||||
| 124 | 0 | DEBUG $req->as_string; | |||||||||||||||||||||||||||
| 125 | 0 | my $res = $self->ua->request( $req ); | |||||||||||||||||||||||||||
| 126 | 0 | DEBUG p $res; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if( $res->is_success ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | return from_json( $res->content , {utf8 => 1} ); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | else{ | ||||||||||||||||||||||||||||
| 132 | 0 | ERROR p $res; | |||||||||||||||||||||||||||
| 133 | 0 | return undef; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub buddy{ | ||||||||||||||||||||||||||
|
Subroutine
buddy: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 140 | 0 | my ($buddyId) = @_; | |||||||||||||||||||||||||||
| 141 | 0 | return $self->_call($API_BUDDY, {buddyId=>$buddyId} ); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub groupMembers{ | ||||||||||||||||||||||||||
|
Subroutine
groupMembers: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 147 | 0 | my ($groupId) = @_; | |||||||||||||||||||||||||||
| 148 | 0 | return $self->_call($API_GROUP_MEMBERS, {groupId=>$groupId} ); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | sub send{ | ||||||||||||||||||||||||||
|
Subroutine
send: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 154 | 0 | my ($buddyId, $content, $attach_path, $do_not_split) = @_; | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | if( $attach_path && -f $attach_path ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$attach_path and -f $attach_path
MC/DC0%0/2
$attach_path and -f $attach_path
$attach_path
-f $attach_path
Truth table0%0/3
A and B
A= $attach_pathB= -f $attach_path
| |||||||||||||||||||||||||||||
| 156 | 0 | return $self->_call_multipart($API_SEND, [buddyId=>$buddyId, attach=>[$attach_path]] ); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | else{ | ||||||||||||||||||||||||||||
| 159 | 0 | my @chunks; | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | if( !$do_not_split && length $content > $API_SEND_LENGTH ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $do_not_split and length $content > $Net::MyPeople::Bot::API_SEND_LENGTH
MC/DC0%0/2
not $do_not_split and length $content > $Net::MyPeople::Bot::API_SEND_LENGTH
not $do_not_split
length $content > $Net::MyPeople::Bot::API_SEND_LENGTH
Truth table0%0/3
not(A) and B
A= not $do_not_splitB= length $content > $Net::MyPeople::Bot::API_SEND_LENGTH
| |||||||||||||||||||||||||||||
| 161 | 0 | @chunks = split(/(.{$API_SEND_LENGTH})/, $content); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | else{ | ||||||||||||||||||||||||||||
| 164 | 0 | push(@chunks,$content); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | my $res; | |||||||||||||||||||||||||||
| 168 | 0 | foreach my $chunk (@chunks){ | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | _utf8_off($chunk) if is_utf8 $chunk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | $res = $self->_call($API_SEND, {buddyId=>$buddyId, content=>$chunk} ); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 0 | return $res; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub groupSend{ | ||||||||||||||||||||||||||
|
Subroutine
groupSend: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 179 | 0 | my ($groupId, $content, $attach_path, $do_not_split) = @_; | |||||||||||||||||||||||||||
| 180 | 0 | ▶ | if( $attach_path && -f $attach_path ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$attach_path and -f $attach_path
MC/DC0%0/2
$attach_path and -f $attach_path
$attach_path
-f $attach_path
Truth table0%0/3
A and B
A= $attach_pathB= -f $attach_path
| |||||||||||||||||||||||||||||
| 181 | 0 | return $self->_call_multipart($API_GROUP_SEND, [groupId=>$groupId, attach=>[$attach_path]] ); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | else{ | ||||||||||||||||||||||||||||
| 184 | 0 | my @chunks; | |||||||||||||||||||||||||||
| 185 | 0 | ▶ | if( !$do_not_split && length $content > $API_SEND_LENGTH ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $do_not_split and length $content > $Net::MyPeople::Bot::API_SEND_LENGTH
MC/DC0%0/2
not $do_not_split and length $content > $Net::MyPeople::Bot::API_SEND_LENGTH
not $do_not_split
length $content > $Net::MyPeople::Bot::API_SEND_LENGTH
Truth table0%0/3
not(A) and B
A= not $do_not_splitB= length $content > $Net::MyPeople::Bot::API_SEND_LENGTH
| |||||||||||||||||||||||||||||
| 186 | 0 | @chunks = split(/(.{$API_SEND_LENGTH})/, $content); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | else{ | ||||||||||||||||||||||||||||
| 189 | 0 | push(@chunks,$content); | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | my $res; | |||||||||||||||||||||||||||
| 193 | 0 | foreach my $chunk (@chunks){ | |||||||||||||||||||||||||||
| 194 | 0 | ▶ | _utf8_off($chunk) if is_utf8 $chunk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | $res = $self->_call($API_GROUP_SEND, {groupId=>$groupId, content=>$chunk} ); | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | 0 | return $res; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | sub groupExit{ | ||||||||||||||||||||||||||
|
Subroutine
groupExit: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 203 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 204 | 0 | my ($groupId) = @_; | |||||||||||||||||||||||||||
| 205 | 0 | return $self->_call($API_GROUP_EXIT, {groupId=>$groupId} ); | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | sub fileDownload{ | ||||||||||||||||||||||||||
|
Subroutine
fileDownload: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 210 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 211 | 0 | my ($fileId, $path) = @_; | |||||||||||||||||||||||||||
| 212 | 0 | return $self->_call_file($API_FILE_DOWNLOAD, {fileId=>$fileId} , $path); | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 216 | 1 | 1; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | __END__ | ||||||||||||||||||||||||||||