Advertisement
theanonym

voice.pl

Jan 22nd, 2012
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.99 KB | None | 0 0
  1. use v5.10;
  2. use strict;
  3. use autodie;
  4. use File::Path qw/mkpath/;
  5. use File::Spec::Functions qw/catfile/;
  6. use Getopt::Long;
  7. use Encode;
  8. use LWP;
  9. use Image::Magick;
  10.  
  11. my $opt = {
  12.    image => undef,
  13.    voice => "voice.mp3",
  14.  
  15.    size_x => 3,
  16.    size_y => 15,
  17.  
  18.    tmpdir => catfile(".", "_voice"),
  19. };
  20.  
  21. my $lwp = new LWP::UserAgent;
  22. $lwp->default_header("referer" => "http://www.0chan.ru/");
  23.  
  24. GetOptions($opt,
  25.    "image=s", "voice=s", "size_x=s",
  26.    "size_y=s", "tmpdir=s", "upload",
  27.    "multi", "crop",
  28.    "clear" => sub { clear(); exit },
  29.    "help"  => sub { print_help(); exit },
  30. );
  31.  
  32. die "Can't find image file.\n" unless -f $opt->{image};
  33. die "Can't find mp3 file.\n"   unless -f $opt->{voice};
  34.  
  35. mkpath $opt->{tmpdir};
  36.  
  37. if($opt->{crop}) {
  38.    say "Crop image '$$opt{image}':";
  39.    #----------------------------------------
  40.    my $image = new Image::Magick;
  41.    $image->Read($opt->{image});
  42.    $image->Resize(
  43.       width  => $opt->{size_x} * 300 + ($opt->{size_x} - 1) * 70,
  44.       height => $opt->{size_y} * 50  + ($opt->{size_y} - 1) * 20,
  45.    );
  46.    $image->Write(catfile($opt->{tmpdir}, "big.png"));
  47.    #----------------------------------------
  48.    my $count = $opt->{size_x} * $opt->{size_y};
  49.    for(my($i, $x, $y) = (1, 0, 0); $i <= $count; $y++) {
  50.       for($x = 0; $x < $opt->{size_x}; $x++, $i++) {
  51.          say "$i/$count";
  52.          #----------------------------------------
  53.          my $image = new Image::Magick;
  54.          $image->Read(catfile($opt->{tmpdir}, "big.png"));
  55.          $image->Crop(
  56.             x => $x * 300 + $x * 70,
  57.             y => $y * 50  + $y * 20,
  58.             width  => 300,
  59.             height => 50,
  60.          );
  61.          $image->Write(catfile($opt->{tmpdir}, "$i.png"));
  62.          #----------------------------------------
  63.       }
  64.    }
  65. }
  66.  
  67. if($opt->{upload} || $opt->{multi}) {
  68.    if($opt->{multi} || $opt->{crop}) {
  69.       my $count = $opt->{size_x} * $opt->{size_y};
  70.       say "Upload $count parts of '$$opt{image}':";
  71.       #----------------------------------------
  72.       for my $i (1 .. $count) {
  73.          my $fname = catfile($opt->{tmpdir}, "$i.png");
  74.          die "Can't find image '$fname'\n" unless -f $fname;
  75.          print upload($fname);
  76.          say unless $i % $opt->{size_x};
  77.       }
  78.       #----------------------------------------
  79.    } else {
  80.       say "Upload '$$opt{image}':";
  81.       say upload($opt->{image});
  82.    }
  83. }
  84.  
  85. sub upload {
  86.    my($fname) = @_;
  87.    #----------------------------------------
  88.    my $res = $lwp->post("http://www.0chan.ru/voice.php",
  89.       Content_Type => "form-data",
  90.       Content => [
  91.          voice  => [$opt->{voice}],
  92.          visual => [$fname],
  93.       ],
  94.    );
  95.    #----------------------------------------
  96.    if($res->content =~ /done:(\w+)/) {
  97.       return "[voice=$1]";
  98.    } else {
  99.       die $res->content;
  100.    }
  101. }
  102.  
  103. sub clear {
  104.    no autodie;
  105.    unlink glob catfile($opt->{tmpdir}, "*");
  106. }
  107.  
  108. sub print_help {
  109.    my $help = <<HLP;
  110. Yoba flash uploader
  111.  
  112. Использование:
  113.    perl $0 [аргументы]
  114.  
  115. Справка:
  116.    --image=%  | Путь к файлу картинки
  117.    --voice=%  | Путь к mp3-файлу ("./voice.mp3" по умолчанию)
  118.  
  119.    --size_x=% | Количество блоков по горизонтали (3 по умолчанию)
  120.    --size_y=% | Количество блоков по вертикали (5 по умолчанию)
  121.  
  122.    --crop     | Порезать изображение на части
  123.    --upload   | Загрузить изображение (все части в сочетании с --crop)
  124.    --multi    | Загрузить части порезанной ранее картинки
  125.  
  126.    --clear    | Удалить временные файлы
  127.  
  128. Примеры:
  129.    --image="yoba.png" --upload | Загрузить одну картинку
  130.    --image="bolshoy_yoba.png" --crop --upload | Разрезать и загрузить по частям
  131. HLP
  132.    $help = encode("cp866", $help) if $^O =~ /win/;
  133.    say $help;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement