Webqq/Plugin/StockInfo.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 84 CC 7 SCAR 26.4
CC
7
Cov
0%
call
20.0
stockid_convert
20.0
stockinfo_convert
2.0
CRAP 14.0
1 package Mojo::Webqq::Plugin::StockInfo;
2 $Mojo::Webqq::Plugin::StockInfo::PRIORITY = 95;
3 use strict;
4 use 5.010;
5 use Encode;
6  
7 sub call{
8 my $client = shift ;
9 my $data = shift;
10 $client->on(receive_message => sub {
11 my($client,$msg)=@_;
12 return unless $msg->allow_plugin;
13 return unless $msg->content =~ /^gp\s+(.*)$/ or $msg->content =~ /^股票\s+(.*)$/;
14 $msg->allow_plugin(0);
15  
16 my $stockid = stockid_convert($1);
17 return unless defined $stockid;
18
19 my $url="http://qt.gtimg.cn/q=$stockid";
20 $client->http_get($url,sub{
21 my $res = shift;
22 $res = encode("utf8",decode("gbk",$res));
23 my $reply_msg = stockinfo_convert($res);
24 $client->reply_message($msg,$reply_msg);
25 })
26
27 });
28 }
29  
30 sub stockid_convert{
31 my $info = shift;
32 ### $info
33 my $stockid;
34 if( $info =~ /^(\d\d\d)(\d\d\d)/ ){
35 my $stockid_head_num = "$1";
36 my $stockid_tail_num = "$2";
37 my @sz=qw/000 001 002 031 038 131 150 159 160 161 162 163 164 165 166 167 169 184 200 300/;
38 my @sh=qw/201 202 203 204 500 502 505 510 511 512 513 518 580 600 601 603 900/;
39 my $stockid_site;
40 if( grep {$_ =~ /^$stockid_head_num$/ }@sz)
41 {
42 $stockid_site="sz";
43 }elsif ( grep {$_ =~ /^$stockid_head_num$/ }@sh ) {
44 $stockid_site="sh";
45 }else{
46 print "不属于股票id(${stockid_head_num}${stockid_tail_num})\n";
47 return undef;
48 }
49 $stockid = "$stockid_site"."$stockid_head_num"."$stockid_tail_num";
50 }else{
51 return undef;
52 }
53 return $stockid;
54 }
55 sub stockinfo_convert{
56  
57 my $res = shift;
58 ### $res
59 (
60 undef,
61 my $name, #名称
62 my $code, #代码
63 #3-5
64 my $current_price, #当前价格
65 my $yesterday_price, #昨日收盘
66 my $today_open_price, #今日开盘
67 #6-8
68 my $totalNumber, #成交量
69 my $outNumber, #外盘
70 my $innerNumber, #内盘
71
72 #9~18
73 my $buy1,
74 my $buyPrice1,
75 my $buy2,
76 my $buyPrice2,
77 my $buy3,
78 my $buyPrice3,
79 my $buy4,
80 my $buyPrice4,
81 my $buy5,
82 my $buyPrice5,
83
84 #19~28
85 my $sell1,
86 my $sellPrice1,
87 my $sell2,
88 my $sellPrice2,
89 my $sell3,
90 my $sellPrice3,
91 my $sell4,
92 my $sellPrice4,
93 my $sell5,
94 my $sellPrice5,
95 undef,
96 my $CurrentTime,
97 my $UpdownPrice,
98 my $UpdownPercent,
99 my $HighPrice,
100 my $LowPrice,
101 undef,
102 undef,
103 my $totalMony,
104 my $changePercent,
105 my $shiYing,
106 undef,
107 undef,
108 undef,
109 my $zhenFu,
110 ) = split /~/,$res;
111 my $reply;
112 $reply .= "股票名称: $name($code)\n";
113 $reply .= "------------------------\n";
114 $reply .= "当前价格:$current_price\t开盘价格:$today_open_price\t昨日收盘:$yesterday_price\n";
115 $reply .= sprintf "幅度 :%-15s换手率 :%-17s\n",$UpdownPercent."%",$changePercent."%";
116 $reply .= "成交量 :$totalNumber\n";
117 $reply .= "------------------------\n";
118 $reply .= sprintf "买一 :%-8s %-5s卖一 :%-8s %-5s\n",$buy1,$buyPrice1,$sell1,$sellPrice1;
119 $reply .= sprintf "买二 :%-8s %-5s卖二 :%-8s %-5s\n",$buy2,$buyPrice2,$sell2,$sellPrice2;
120 $reply .= sprintf "买三 :%-8s %-5s卖三 :%-8s %-5s\n",$buy3,$buyPrice3,$sell3,$sellPrice3;
121 $reply .= sprintf "买四 :%-8s %-5s卖四 :%-8s %-5s\n",$buy4,$buyPrice4,$sell4,$sellPrice4;
122 $reply .= sprintf "买五 :%-8s %-5s卖五 :%-8s %-5s\n",$buy5,$buyPrice5,$sell5,$sellPrice5;
123 return $reply;
124 }
125 1;