| 1 | #============================================ | ||
| 2 | # PCÌfXNgbvæðLv`µAèúIÉM·évO for Mac | ||
| 3 | # ------------------------------------------- | ||
| 4 | # ANZT | ||
| 5 | # os String gpOS | ||
| 6 | # Mac or Win(not supported yet) | ||
| 7 | # capture_file_path String Lv`æÌpX | ||
| 8 | # K{ | ||
| 9 | # interval_time Integer ob`ÌÀsüú | ||
| 10 | # 0ÌêÍêxÌÝÀs | ||
| 11 | # api_url String Lv`æMæAPIÌURL | ||
| 12 | # ±êªwè³êÄ¢éêÍLv`Éf[^ðM·é | ||
| 13 | # api_attr HashRef æMÉattributeðt¯éê | ||
| 14 | # { id => 1, name => 2 } | ||
| 15 | # debug Integer fobO[h | ||
| 16 | # 0 ... O\¦µÈ¢(ftHg) | ||
| 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 | # æÌLv`ðÀ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 | # èúob`Às | ||
| 85 | # req: | ||
| 86 | # res: | ||
| 87 | # result: ¬÷1A»êÈO0 | ||
| 88 | #========================================== | ||
| 89 | sub execute_by_interval { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | # Lv`æÌpXÌfBNgªAEgÈê | ||
| 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 | # Lv`À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__ | ||