Weixin/Plugin/Beauty.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 201 CC 28 SCAR 65.7
CC
28
Cov
0%
call
812.0
BEGIN
2.0
BEGIN
2.0
CRAP 710.8
1 package Mojo::Weixin::Plugin::Beauty;
2 our $PRIORITY = 95;
3 use Storable qw(retrieve nstore);
4 use Encode qw();
5 use Mojo::Util qw();
6 use List::Util qw(first);
7 sub call{
8 my $client = shift;
9 my $data = shift;
10 my $file = $data->{file} || './Beauty.dat';
11 my $db = {};$db = retrieve($file) if -e $file;
12 my $boards = ref($data->{board}) eq "ARRAY"? $data->{board}: [{command=>"看妹子",url=>'http://huaban.com/boards/19570858/'}];
13 for(@{ $boards }){
14 my $command = $_->{command} ;
15 my $url = $_->{url};
16 next if not $command;
17 next if not $url;
18 if(not exists $db->{$url}){
19 $db->{$url} = {command=>$command,pins=>[],last_pin_id=>undef,url=>$url};
20 }
21 else{
22 $db->{$url}{command} = $command;
23 $db->{$url}{url} = $url;
24 }
25 }
26 for my $url (keys %$db){
27 if(not first {$url eq $_->{url} } @{ $boards } ){
28 delete $db->{$url};
29 next;
30 }
31 if(@{$db->{$url}{pins}} != 0){
32 $db->{$url}{last_pin_id} = $db->{$url}{pins}->[-1]->{id} if not defined $db->{$url}{last_pin_id};
33 next ;
34 }
35 my $html = $client->http_get($url);
36 if(defined $html){
37 if($html=~m#\Qapp.page["board"]\E\s*=\s*(.*);\s?#){
38 my $data = $1;
39 $data=~s/ / /g;
40 my $unicode_data = Mojo::Util::html_unescape(Encode::decode("utf8",$data));
41 $data = Encode::encode("utf8",$unicode_data);
42 my $json = $client->from_json($data);
43 if(defined $json and defined $json->{pins}){
44 for(@{ $json->{pins} }){
45 push @{$db->{$url}{pins}},{id=>$_->{pin_id}, url=>'http://img.hb.aicdn.com/' . $_->{file}{key}};
46 }
47 }
48 }
49 }
50 if(@{$db->{$url}{pins}} == 0){
51 $client->error("插件[ ".__PACKAGE__ . " ]初始化数据失败: [$db->{$url}{command}]($url)");
52 }
53 else{
54 $db->{$url}{last_pin_id} = $db->{$url}{pins}->[-1]->{id};
55 }
56 }
57  
58 my $callback = sub{
59 my($client,$msg) = @_;
60 my $command = $msg->content;
61 my $board = first { $command eq $_->{command}} values %$db ;
62 return if not defined $board;
63 return if $msg->from eq "bot";
64 $msg->allow_plugin(0);
65 my $pin = shift @{$board->{pins}};
66 if(not defined $pin){
67 $client->http_get(
68 $board->{url} . '?ilf1frwr&max='. $board->{last_pin_id} . '&limit=20&wfl=1',
69 {
70 Accept=>'application/json',
71 Referer=>$board->{url},
72 'X-Request'=>'JSON',
73 'X-Requested-With'=>'XMLHttpRequest',
74 json=>1,
75 },
76 sub{
77 my $json = shift;
78 return if not defined $json;
79 if(defined $json->{board}{pins} ){
80 if(@{$json->{board}{pins}} > 0){
81 push @{$board->{pins}},{id=>$_->{pin_id}, url=>'http://img.hb.aicdn.com/' . $_->{file}{key}} for @{$json->{board}{pins}};
82 }
83 else{
84 my $html = $client->http_get($board->{url});
85 if(defined $html){
86 if($html=~m#\Qapp.page["board"]\E\s*=\s*(.*?);#){
87 my $data = $1;
88 my $json = $client->to_json($data);
89 if(defined $json and defined $json->{pins}){
90 for(@{ $json->{pins} }){
91 push @{$board->{pins}},{id=>$_->{pin_id}, url=>'http://img.hb.aicdn.com/' . $_->{file}{key}};
92 }
93 }
94 }
95 }
96 }
97 }
98 my $pin = shift @{$board->{pins}};
99 if($msg->type eq "group_message"){
100 $client->send_media($msg->group,$pin->{url},sub{$_[1]->from("bot")});
101 }
102 elsif($msg->type eq "friend_message" and $msg->class eq "recv"){
103 $client->send_media($msg->sender,$pin->{url},sub{$_[1]->from("bot")});
104 }
105 elsif($msg->type eq "friend_message" and $msg->class eq "send"){
106 $client->send_media($msg->receiver,$pin->{url},sub{$_[1]->from("bot")});
107 }
108 $board->{last_pin_id} = $pin->{id};
109 nstore($db,$file);
110 }
111 );
112 }
113 else{
114 if($msg->type eq "group_message"){
115 $client->send_media($msg->group,$pin->{url},sub{$_[1]->from("bot")});
116 }
117 elsif($msg->type eq "friend_message" and $msg->class eq "recv"){
118 $client->send_media($msg->sender,$pin->{url},sub{$_[1]->from("bot")});
119 }
120 elsif($msg->type eq "friend_message" and $msg->class eq "send"){
121 $client->send_media($msg->receiver,$pin->{url},sub{$_[1]->from("bot")});
122 }
123 $board->{last_pin_id} = $pin->{id};
124 nstore($db,$file);
125 }
126 };
127 $client->on(receive_message=>$callback,send_message=>$callback);
128 }
129  
130 1;