You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.6 KiB
130 lines
3.6 KiB
7 years ago
|
#!/usr/local/bin/perl
|
||
|
|
||
|
use strict;
|
||
|
use warnings;
|
||
|
|
||
|
use LWP::UserAgent;
|
||
|
use HTTP::Request;
|
||
|
use YAML::XS qw/LoadFile/;
|
||
|
|
||
|
our $api_url = "https://pddimp.yandex.ru/api2/admin";
|
||
|
our $query_options = LoadFile("./yamd_query_options.yml")
|
||
|
or die "Query parameters file not found\n";
|
||
|
our $task_file = shift or die "Usage: yamd.pl <file.yml>\n";
|
||
7 years ago
|
our $task = %{LoadFile($task_file)}{yamd}
|
||
7 years ago
|
or die "Unable to open file '$task_file'\n";
|
||
|
|
||
|
foreach my $check ("domain", "token", "service", "query") {
|
||
7 years ago
|
if (!defined $task->{$check}) {
|
||
7 years ago
|
die "'$check' is undefined in '$task_file'\n";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
my $request_url = "";
|
||
|
my $request_type = "";
|
||
|
my $request_content_type = "";
|
||
|
|
||
|
{
|
||
7 years ago
|
my $service = $task->{service};
|
||
|
my $sub_service = $task->{sub_service};
|
||
|
my $query = $task->{query};
|
||
7 years ago
|
|
||
7 years ago
|
$request_url = $api_url . "/" . $service;
|
||
7 years ago
|
$request_url .= (defined $sub_service) ? "/" . $sub_service : "";
|
||
|
$request_url .= "/" . $query;
|
||
|
|
||
7 years ago
|
if ( $service eq "import" && $query eq "start_import_file" ) {
|
||
7 years ago
|
$request_url .= "?domain=" . $task->{domain};
|
||
|
foreach my $k (keys %{$task->{options}}) {
|
||
|
$request_url .= "&" . $k . "=" . $task->{options}->{$k};
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
if (defined $sub_service) {
|
||
|
$request_type = $query_options->{$service}->{$sub_service}->{$query}->{query_type};
|
||
|
$request_content_type = $query_options->{$service}->{$sub_service}->{$query}->{content_type};
|
||
|
} else {
|
||
|
$request_type = $query_options->{$service}->{$query}->{query_type};
|
||
|
$request_content_type = $query_options->{$service}->{$query}->{content_type};
|
||
|
}
|
||
|
|
||
|
if (!defined $request_content_type) {
|
||
|
$request_content_type = $query_options->{default_content_type};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
my $ua = new LWP::UserAgent;
|
||
|
my $request = new HTTP::Request($request_type => $request_url);
|
||
|
|
||
7 years ago
|
if ( $task->{service} eq "domain" &&
|
||
|
$task->{sub_service} eq "logo" &&
|
||
|
$task->{query} eq "set" ) {
|
||
7 years ago
|
my $boundary = 'X';
|
||
|
my @rand = ('a'..'z', 'A'..'Z');
|
||
|
for (0..14) { $boundary .= $rand[rand(@rand)]; }
|
||
|
|
||
|
$request->header(
|
||
7 years ago
|
'PddToken' => $task->{token},
|
||
7 years ago
|
'Content-type' => $request_content_type . "; boundary=" . $boundary
|
||
|
);
|
||
|
|
||
|
my $field = new HTTP::Message([
|
||
|
'Content-Disposition' => 'form-data; name="domain"',
|
||
|
'Content-Type' => 'text/plain; charset=utf-8'
|
||
|
]);
|
||
7 years ago
|
$field->add_content_utf8($task->{domain});
|
||
7 years ago
|
$request->add_part($field);
|
||
|
|
||
7 years ago
|
open(my $fh, '<', $task->{options}->{file});
|
||
7 years ago
|
my $file_content = new HTTP::Message([
|
||
|
'Content-Disposition' => 'form-data; name="file"; filename="logo"',
|
||
|
'Content-Type' => 'application/octet-stream'
|
||
|
]);
|
||
|
$file_content->add_content($_) while <$fh>;
|
||
|
$request->add_part($file_content);
|
||
|
|
||
|
close $fh;
|
||
7 years ago
|
} elsif ( $task->{service} eq "import" &&
|
||
|
$task->{query} eq "start_import_file" ) {
|
||
7 years ago
|
my $boundary = 'X';
|
||
|
my @rand = ('a'..'z', 'A'..'Z');
|
||
|
for (0..14) { $boundary .= $rand[rand(@rand)]; }
|
||
|
|
||
|
$request->header(
|
||
7 years ago
|
'PddToken' => $task->{token},
|
||
7 years ago
|
'Content-type' => $request_content_type . "; boundary=" . $boundary
|
||
|
);
|
||
|
|
||
7 years ago
|
open(my $fh, '<', $task->{options}->{file});
|
||
7 years ago
|
my $file_content = new HTTP::Message([
|
||
|
'Content-Disposition' => 'form-data; name="import_list_file"; filename="import_list_file"',
|
||
|
'Content-Type' => 'text/plain',
|
||
|
]);
|
||
|
$file_content->add_content($_) while <$fh>;
|
||
|
|
||
|
$request->add_part($file_content);
|
||
|
|
||
|
close $fh;
|
||
7 years ago
|
} else {
|
||
|
$request->header(
|
||
7 years ago
|
'PddToken' => $task->{token},
|
||
7 years ago
|
'Content-type' => $request_content_type
|
||
|
);
|
||
|
|
||
7 years ago
|
my $request_content = "domain=" . $task->{domain};
|
||
|
foreach my $k (keys %{$task->{options}}) {
|
||
|
$request_content .= "&" . $k . "=" . $task->{options}->{$k};
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
$request->content($request_content);
|
||
|
}
|
||
7 years ago
|
|
||
|
my $response = $ua->request($request);
|
||
|
|
||
|
if ($response->is_success) {
|
||
|
print $response->content . "\n\n";
|
||
|
} else {
|
||
|
print $response->status_line . "\n\n";
|
||
|
}
|
||
|
|