streams/Code/Daemon/Content_importer.php

66 lines
1.9 KiB
PHP
Raw Normal View History

<?php
2022-02-16 04:08:28 +00:00
namespace Code\Daemon;
2022-02-16 04:08:28 +00:00
use Code\Web\HTTPSig;
use Code\Lib\Channel;
use Code\Lib\Url;
require_once('include/cli_startup.php');
require_once('include/attach.php');
require_once('include/import.php');
2022-11-27 09:15:28 +00:00
class Content_importer implements DaemonInterface
2021-12-03 03:01:39 +00:00
{
2022-10-20 20:00:55 +00:00
/** @noinspection PhpUnusedParameterInspection */
2022-11-27 09:15:28 +00:00
public function run(int $argc, array $argv): void
2021-12-03 03:01:39 +00:00
{
cli_startup();
2021-12-03 03:01:39 +00:00
$page = $argv[1];
$since = $argv[2];
$until = $argv[3];
$channel_address = $argv[4];
$hz_server = urldecode($argv[5]);
2021-12-03 03:01:39 +00:00
$m = parse_url($hz_server);
2022-01-25 01:26:12 +00:00
$channel = Channel::from_username($channel_address);
2021-12-03 03:01:39 +00:00
if (! $channel) {
logger('itemhelper: channel not found');
killme();
}
2021-12-03 03:01:39 +00:00
$headers = [
'X-API-Token' => random_string(),
'X-API-Request' => $hz_server . '/api/z/1.0/item/export_page?f=&zap_compat=1&since=' . urlencode($since) . '&until=' . urlencode($until) . '&page=' . $page ,
'Host' => $m['host'],
'(request-target)' => 'get /api/z/1.0/item/export_page?f=&zap_compat=1&since=' . urlencode($since) . '&until=' . urlencode($until) . '&page=' . $page ,
];
2022-01-25 01:26:12 +00:00
$headers = HTTPSig::create_sig($headers, $channel['channel_prvkey'], Channel::url($channel), true, 'sha512');
$x = Url::get($hz_server . '/api/z/1.0/item/export_page?f=&zap_compat=1&since=' . urlencode($since) . '&until=' . urlencode($until) . '&page=' . $page, [ 'headers' => $headers ]);
2021-12-03 03:01:39 +00:00
if (! $x['success']) {
logger('no API response', LOGGER_DEBUG);
killme();
}
2021-12-03 03:01:39 +00:00
$j = json_decode($x['body'], true);
2021-12-03 03:01:39 +00:00
if (! $j) {
killme();
}
2021-12-03 03:01:39 +00:00
if (! ($j['item'] || count($j['item']))) {
killme();
}
2021-12-03 03:01:39 +00:00
import_items($channel, $j['item'], false, ((array_key_exists('relocate', $j)) ? $j['relocate'] : null));
2021-12-03 03:01:39 +00:00
killme();
}
}