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.
105 lines
3.0 KiB
105 lines
3.0 KiB
#!/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";
|
|
our $task = LoadFile($task_file)
|
|
or die "Unable to open file '$task_file'\n";
|
|
|
|
foreach my $check ("domain", "token", "service", "query") {
|
|
if (!defined $task->{yamd}->{$check}) {
|
|
die "'$check' is undefined in '$task_file'\n";
|
|
}
|
|
}
|
|
|
|
my $request_url = "";
|
|
my $request_type = "";
|
|
my $request_content_type = "";
|
|
|
|
{
|
|
my $service = $task->{yamd}->{service};
|
|
my $sub_service = $task->{yamd}->{sub_service};
|
|
my $query = $task->{yamd}->{query};
|
|
|
|
$request_url = $api_url . "/" . $service;
|
|
$request_url .= (defined $sub_service) ? "/" . $sub_service : "";
|
|
$request_url .= "/" . $query;
|
|
|
|
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);
|
|
|
|
if ( $task->{yamd}->{service} eq "domain" &&
|
|
$task->{yamd}->{sub_service} eq "logo" &&
|
|
$task->{yamd}->{query} eq "set" ) {
|
|
my $boundary = 'X';
|
|
my @rand = ('a'..'z', 'A'..'Z');
|
|
for (0..14) { $boundary .= $rand[rand(@rand)]; }
|
|
|
|
$request->header(
|
|
'PddToken' => $task->{yamd}->{token},
|
|
'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'
|
|
]);
|
|
$field->add_content_utf8($task->{yamd}->{domain});
|
|
$request->add_part($field);
|
|
|
|
open(my $fh, '<', $task->{yamd}->{options}->{file});
|
|
my $size = (stat $task->{yamd}->{options}->{file})[7];
|
|
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;
|
|
} elsif ( $task->{yamd}->{service} eq "import" &&
|
|
$task->{yamd}->{query} eq "start_import_file" ) {
|
|
die "Not implemented!\n\n";
|
|
} else {
|
|
$request->header(
|
|
'PddToken' => $task->{yamd}->{token},
|
|
'Content-type' => $request_content_type
|
|
);
|
|
|
|
my $request_content = "domain=" . $task->{yamd}->{domain};
|
|
foreach my $k (keys %{$task->{yamd}->{options}}) {
|
|
$request_content .= "&" . $k . "=" . $task->{yamd}->{options}->{$k};
|
|
}
|
|
|
|
$request->content($request_content);
|
|
}
|
|
|
|
my $response = $ua->request($request);
|
|
|
|
if ($response->is_success) {
|
|
print $response->content . "\n\n";
|
|
} else {
|
|
print $response->status_line . "\n\n";
|
|
}
|
|
|
|
|