Спойлер (Отобразить)
CODE (htmlphp):
скопировать код в буфер обмена
скопировать код в буфер обмена
- #!/usr/bin/perl -w
- ###
- use WebService::Antigate;
- use strict;
- use conf;
- use LWP::UserAgent;
- use HTTP::Request::Common;
- use URI::Escape;
- use warnings;
- ###
- print "Читаем конфигурацию ...\n";
- my %msg = ();
- #Читаем конфигурацию
- my %cfg = conf::reed('config.cfg');
- my %category = conf::reed('categorys.db');
- my %city = conf::reed('city.db');
- my %operator = conf::reed('operators.db');
- # Создаём хеш настройки
- $msg{category1} = 52;
- $msg{category2} = $category{$cfg{category}} || undef;
- $msg{city} = $city{$cfg{city}} || undef;
- # Первый телефон
- $msg{phone1_country} = $cfg{country_code1} || 1;
- $msg{phone1_operator} = $operator{$cfg{operator1}} || 0;
- $msg{phone1_code} = $cfg{code1} || '';
- $msg{phone1_number} = $cfg{phone1} || '';
- $msg{public_phone1_from} = $cfg{c1} || 9;
- $msg{public_phone1_to} = $cfg{'do1'} || 21;
- # Второй телефон
- $msg{phone2_country} = $cfg{country_code2} || 1;
- $msg{phone2_operator} = $operator{$cfg{operator2}} || 0;
- $msg{phone2_code} = $cfg{code2} || '';
- $msg{phone2_number} = $cfg{phone2} || '';
- $msg{public_phone2_from} = $cfg{c2} || 9;
- $msg{public_phone2_to} = $cfg{'do2'} || 21;
- # Тpеттий телефон
- $msg{phone3_country} = $cfg{country_code3} || 1;
- $msg{phone3_operator} = $operator{$cfg{operator3}} || 0;
- $msg{phone3_code} = $cfg{code3} || '';
- $msg{phone3_number} = $cfg{phone3} || '';
- $msg{public_phone3_from} = $cfg{c3} || 9;
- $msg{public_phone3_to} = $cfg{'do3'} || 21;
- # Зануляем не нужные переменные
- %category=();
- %city=();
- %operator=();
- print "Читаю список постов ...\n";
- # Читаем список постов
- my @posts;
- read_posts(\@posts);
- # Создаём обьект браузера
- my $ua = LWP::UserAgent->new;
- $ua->cookie_jar({});
- $ua->agent('Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.19) Gecko/2010031422 Firefox/3.0.19');
- print "Начинаю постить ...\n";
- # Начинаем постить
- while (@posts) {
- print "Получаю заголовок + сообщение и картинки ...\n";
- # Получаем заголовок+сообщение и картинки
- my $txt = shift @posts or last;
- if (defined $txt) {
- $txt = load($txt);
- } else {
- print "Отмена постинга ...\n";
- next;
- }
- my @images = ($txt) =~ /\[img\](.*?)\[\/img\]/gi;
- $txt =~ s/\[img\](.*?)\[\/img\]//g;
- my ($tem, $message) = split /[-]{3,}msg[-]{3,}/i, $txt;
- print "Вход (".$cfg{login}.";".$cfg{pass}.").\n";
- if (login($ua, $cfg{login}, $cfg{pass})) {
- print "Первая стадия посыла объявления.\n";
- if (etap1($ua)) {
- print "Вторая стадия посыла объявления.\n";
- if (my $id = etap2($ua,$tem,$message)) {
- print "Отправка изображений.\n";
- if (etap3($ua, \@images, $id)) {
- print "Объявление успешно отправлено.\n";
- } else {
- print "При отправки изображения произошла ошибка!\n";
- }
- } else {
- print "Ошибка на второй стадии!\n";
- }
- } else {
- print "Ошибка на первой стадии!\n";
- }
- } else {
- print "Ошибка авторизации!\n";
- }
- print "Пауза ".$cfg{pause}." секунд.\n";
- sleep $cfg{pause};
- }
- sub etap1 {
- my $ua = shift;
- my $answ = $ua->post('http://av.by/advpublic/advpublic.php',
- {
- event => 'Pre_Add',
- category_parent => 52,
- category_id => $msg{category2},
- category_avto => 0,
- country_id => 1,
- country_name => '',
- submit => '%A0%C4%E0%EB%E5%E5%A0',
- })->content;
- if ($answ =~ /upload_image/) {
- return 1;
- } else {
- return undef;
- }
- }
- sub etap2 {
- my $ua = shift;
- my $tema = shift;
- my $message = shift;
- my $file = $ua->get("http://av.by/image.php?event=2")->content();
- open (FILE, ">Captcha.jpeg");
- binmode(FILE);
- print FILE "$file";
- close (FILE);
- my $recognizer = WebService::Antigate->new(key => 'тут ключик', attempts => 25);
- my $id = $recognizer->upload(file => 'Captcha.jpeg');
- unless($id)
- {
- die "Error while uploading captcha: ", $recognizer->errno, " (", $recognizer->errstr, ")";
- }
- my $captcha = $recognizer->recognize($id);
- unless($captcha)
- {
- die "Error while recognizing captcha: ", $recognizer->errno, " (", $recognizer->errstr, ")";
- }
- print "Recognized captcha is: ", $captcha, "\n";
- my $req = POST('http://av.by/advpublic/advpublic.php',
- [
- event => 'Add',
- category_parent => 52,
- category_id => $msg{category2},
- category_avto => 0,
- country_id => 1,
- country_name => '%C1%E5%EB%E0%F0%F3%F1%FC',
- public_topic => $tema,
- public_text => $message,
- public_period_id => 4,
- city_id => $msg{city},
- city_name => '',
- phone1_country_id => $msg{phone1_country},
- phone1_operator_id => $msg{phone1_operator},
- phone1_code => $msg{phone1_code},
- phone1_number => $msg{phone1_number},
- public_phone1_from => $msg{public_phone1_from},
- public_phone1_to => $msg{public_phone1_to},
- phone2_country_id => $msg{phone2_country},
- phone2_operator_id => $msg{phone2_operator},
- phone2_code => $msg{phone2_code},
- phone2_number => $msg{phone2_number},
- public_phone2_from => $msg{public_phone2_from},
- public_phone2_to => $msg{public_phone2_to},
- phone3_country_id => $msg{phone3_country},
- phone3_operator_id => $msg{phone3_operator},
- phone3_code => $msg{phone3_code},
- phone3_number => $msg{phone3_number},
- public_phone3_from => $msg{public_phone3_from},
- public_phone3_to => $msg{public_phone3_to},
- upload_image => 1,
- image_security_code => $captcha,
- 'send' => '%A0%CE%F2%EF%F0%E0%E2%E8%F2%FC%A0',
- ]);
- my $res = $ua->request($req);
- if ($res->code == 302) {
- my ($link) = ($res->as_string) =~ /advpublic\.php\?event=Image_Next\&public_id=(\d+)/;
- $ua->get('http://av.by/advpublic/advpublic.php?event=Image_Next&public_id='.$link)->code;
- return $link;
- } else {
- open (I, '> index.html');
- print I $res->as_string;
- close(I);
- print "Код ответа ".$res->code."\n";
- return undef;
- }
- }
- sub etap3 {
- my ($ua, $a, $id) = @_;
- $_ = $cfg{messages}.$_ for (@$a);
- my $i=0;
- while (@$a) {
- my $img = shift @$a;
- $i++;
- # если файл не найден, выводим соответствующую ошибку и выходим
- unless (defined $img && -e $img && -f $img) {
- print "Error: no file passed or file not exists\nFile:".$img."\n";
- next;
- }
- my $req = POST('http://av.by/advpublic/advpublic.php',
- 'Content_Type' => 'form-data',
- 'Content' => [
- 'event' => 'Image_Add',
- 'public_id' => $id,
- 'image_id' => $i,
- 'public_image' => [$img],
- ]);
- $req = $ua->request($req);
- if ($req->is_success) {
- print "Image uploaded ".$img.".\n";
- }
- }
- $ua->get('http://av.by/advpublic/advpublic.php?event=View&public_id='.$id)->code;
- }
- sub login {
- my ($ua, $login, $pass) = @_;
- return undef unless (defined $ua or defined $login or defined $pass);
- my $answ = $ua->post('http://av.by/public/login.php',
- {
- event => 'Login',
- username => ,
- password => ,
- login => $login,
- pwd => $pass,
- })->as_string;
- if (my ($pid) = ($answ) =~ /Location: \/public\/index\.php\?event=(\d+)/i) {
- print "Авторизация успешна.\n";
- my $x = $ua->get('http://av.by/public/index.php?event='.$pid)->code;
- return $pid;
- } else {
- return undef;
- }
- }
- sub read_posts { # Читаем список постов
- my $a = shift;
- @$a = glob $cfg{messages}."*.txt";
- return scalar @$a;
- }
- sub load
- {
- my $fiel = shift || 1;
- if (-f $fiel){
- open(F, "<", $fiel) || die $fiel.' - '.$!.$/;
- chomp(my @list = <F>);
- close F;
- return join ("\n",@list);
- } else {
- return undef;
- }
- }