lib/Sorauta/Capture/ScreenShot.pm

filter: Statementstmt 100% 3 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 4 / 4 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
↑ Summary
1 #============================================
2 # PC‚̃fƒXƒNƒgƒbƒv‰æ‘œ‚ðƒLƒƒƒvƒ`ƒƒ‚µA’èŠú“I‚É‘—M‚·‚éƒvƒƒOƒ‰ƒ€ for Mac
3 # -------------------------------------------
4 # ƒAƒNƒZƒT
5 # os String Žg—pOS
6 # Mac or Win(not supported yet)
7 # capture_file_path String ƒLƒƒƒvƒ`ƒƒ‰æ‘œ‚̃pƒX
8 # •K{
9 # interval_time Integer ƒoƒbƒ`‚ÌŽÀsŽüŠú
10 # 0‚̏ꍇ‚͈ê“x‚̂ݎÀs
11 # api_url String ƒLƒƒƒvƒ`ƒƒ‰æ‘œ‘—MæAPI‚ÌURL
12 # ‚±‚ꂪŽw’肳‚ê‚Ä‚¢‚éê‡‚̓Lƒƒƒvƒ`ƒƒŽž‚Ƀf[ƒ^‚𑗐M‚·‚é
13 # api_attr HashRef ‰æ‘œ‘—MŽž‚Éattribute‚ð•t‚¯‚éê‡
14 # { id => 1, name => 2 }
15 # debug Integer ƒfƒoƒbƒOƒ‚[ƒh
16 # 0 ... ƒƒO•\ަ‚µ‚È‚¢(ƒfƒtƒHƒ‹ƒg)
17 # 1 ... ƒƒO•\ަ‚·‚é
18 #============================================
19 package Sorauta::Capture::ScreenShot;
20 1 ▶use base qw/Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21
22 use 5.012003;
23 use strict;
24 use warnings;
25 use utf8;
26 use CGI::Carp qw/fatalsToBrowser/;
27 use Data::Dumper;
28 use LWP::UserAgent;
29 use HTTP::Request::Common qw/POST/;
30 use File::Basename;
31 use Sorauta::Utility;
32
33 our $VERSION = '0.02';
34
35 __PACKAGE__->mk_accessors(
36 qw/os capture_file_path debug interval_time api_url api_attr/);
37
38 #==========================================
39 # ‰æ‘œ‚̃Lƒƒƒvƒ`ƒƒ‚ðŽÀs
40 # req:
41 # res:
42 #==========================================
43 sub execute {
44 my $self = shift;
45
46 # must be defined accessors
47 if (!$self->os || !$self->capture_file_path || !length($self->interval_time)) {
48 my $message = join $/,
49 '====================',
50 'must be define accessor ',
51 'os ... Mac or Win',
52 'capture_file_path ... /Users/user1/Desktop/capture.jpg',
53 'interval_time ... 10(when you capture only once, please set 0)',
54 '====================';
55 die $message;
56 }
57
58 # execute
59 print '=================================', $/;
60 print ' Batch Start', $/;
61 print '=================================', $/;
62
63 while (1) {
64 my $res = $self->execute_by_interval;
65
66 print "[", get_timestamp(time), "]capture ", $res ? "succeeded" : "failed";
67
68 if ($self->interval_time != 0) {
69 print ", please wait ", $self->interval_time, "sec", $/;
70
71 sleep $self->interval_time;
72 }
73 else {
74 print $/;
75
76 last;
77 }
78 }
79
80 return 1;
81 }
82
83 #==========================================
84 # ’èŠúƒoƒbƒ`ŽÀs
85 # req:
86 # res:
87 # result: ¬Œ÷Žž1A‚»‚êˆÈŠO0
88 #==========================================
89 sub execute_by_interval {
90 my $self = shift;
91
92 # ƒLƒƒƒvƒ`ƒƒ‰æ‘œ‚̃pƒX‚̃fƒBƒŒƒNƒgƒŠ‚ªƒAƒEƒg‚ȏꍇ
93 my($filename, $dir, $ext) = fileparse( $self->capture_file_path);
94 unless (-d $dir) {
95 die "was not exists directory of output capture file path";
96 }
97
98 # ƒLƒƒƒvƒ`ƒƒŽÀs
99 if ($self->os =~ /^Win/i) {
100 # not implement now...
101 }
102 else {
103 my $cmd = 'screencapture -x '.$self->capture_file_path;
104 `$cmd`;
105 }
106
107 # send capture data to server
108 if (length($self->api_url)) {
109 my $ua = new LWP::UserAgent;
110 my $req = POST(
111 $self->api_url,
112 Content_Type => 'form-data',
113 Content => [
114 %{$self->api_attr},
115 ]
116 );
117
118 my $res = $ua->request($req);
119 if ($res->is_success) {
120 print "[Sorauta::Capture::ScreenShot]send capture file ... success.", $/;
121 }
122 else {
123 print "[Sorauta::Capture::ScreenShot]send capture file ... failed: ", $res->status_line, $/;
124
125 return 0;
126 }
127 }
128
129 return 1;
130 }
131
132 1;
133
134 __END__
↑ Summary