equinox79
9/7/2012 - 7:56 AM

指定されたディレクトリを監視して画像ファイルがうpされるとpyazoに投稿

指定されたディレクトリを監視して画像ファイルがうpされるとpyazoに投稿

#!/usr/bin/env perl
use strict;
use warnings;

use File::Monitor::Lite;
use HTTP::Request::Common;
use IO::File;
use LWP::UserAgent;

my $upload_path = '/foo/bar';    # ここを直す
my $pyazo_url   = 'http://yairc.cfe.jp:5000/';

my $fh   = IO::File->new();
my $ua   = LWP::UserAgent->new();
my $dmon = File::Monitor::Lite->new(
    in   => $upload_path,
    name => qr/.+\.\U(png|jpg|jpeg)/,
);

$ua->agent('Gyazo/1.0');

while ( $dmon->check() and sleep 1 ) {
    next unless ( $dmon->anychange );
    my @created_files = $dmon->created;
    for my $filename (@created_files) {
        next unless ( $fh->open($filename) );
        my $res = $ua->request(
            POST(
                $pyazo_url,
                Content_Type => 'form-data',
                Content      => { imagedata => [$filename] },
            )
        );
        print sprintf( "%s ... %s\n", $filename, $res->code );
        print $res->decoded_content . "\n" if ( $res->is_success );
    }
}