line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#============================================
|
2
|
|
|
|
|
|
|
# PCのデスクトップ画像をキャプチャし、定期的に送信するプログラム for Mac
|
3
|
|
|
|
|
|
|
# -------------------------------------------
|
4
|
|
|
|
|
|
|
# アクセサ
|
5
|
|
|
|
|
|
|
# os String 使用OS
|
6
|
|
|
|
|
|
|
# Mac or Win(not supported yet)
|
7
|
|
|
|
|
|
|
# capture_file_path String キャプチャ画像のパス
|
8
|
|
|
|
|
|
|
# 必須
|
9
|
|
|
|
|
|
|
# interval_time Integer バッチの実行周期
|
10
|
|
|
|
|
|
|
# 0の場合は一度のみ実行
|
11
|
|
|
|
|
|
|
# api_url String キャプチャ画像送信先APIのURL
|
12
|
|
|
|
|
|
|
# これが指定されている場合はキャプチャ時にデータを送信する
|
13
|
|
|
|
|
|
|
# api_attr HashRef 画像送信時にattributeを付ける場合
|
14
|
|
|
|
|
|
|
# { id => 1, name => 2 }
|
15
|
|
|
|
|
|
|
# debug Integer デバッグモード
|
16
|
|
|
|
|
|
|
# 0 ... ログ表示しない(デフォルト)
|
17
|
|
|
|
|
|
|
# 1 ... ログ表示する
|
18
|
|
|
|
|
|
|
#============================================
|
19
|
|
|
|
|
|
|
package Sorauta::Capture::ScreenShot;
|
20
|
1
|
|
|
1
|
|
24591
|
use base qw/Class::Accessor::Fast/;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
789
|
|
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
|
|
|
|
|
|
|
# 画像のキャプチャを実行
|
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
|
|
|
|
|
|
|
# 定期バッチ実行
|
85
|
|
|
|
|
|
|
# req:
|
86
|
|
|
|
|
|
|
# res:
|
87
|
|
|
|
|
|
|
# result: 成功時1、それ以外0
|
88
|
|
|
|
|
|
|
#==========================================
|
89
|
|
|
|
|
|
|
sub execute_by_interval {
|
90
|
|
|
|
|
|
|
my $self = shift;
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# キャプチャ画像のパスのディレクトリがアウトな場合
|
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
|
|
|
|
|
|
|
# キャプチャ実行
|
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__
|