Support for multiple picture posts

This commit is contained in:
Michael 2019-08-06 05:41:49 +00:00
parent 20ed3495b2
commit e214c21025

View file

@ -621,19 +621,15 @@ function twitter_post_hook(App $a, array &$b)
$msg = Plaintext::shorten($msgarr["title"], $max_char - 50);
}
$image = "";
if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
if (($msgarr['url'] == $b['plink']) && !empty($msgarr['images']) && (count($msgarr['images']) <= 4)) {
$url_added = false;
} elseif (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
$msg .= "\n" . $msgarr["url"];
$url_added = true;
} else {
$url_added = false;
}
if (isset($msgarr["image"]) && ($msgarr["type"] != "video")) {
$image = $msgarr["image"];
}
if (empty($msg)) {
return;
}
@ -641,9 +637,16 @@ function twitter_post_hook(App $a, array &$b)
// and now tweet it :-)
$post = [];
if (!empty($image)) {
if (!empty($msgarr['images'])) {
try {
$img_str = Network::fetchUrl($image);
$post['media_ids'] = '';
$counter = 0;
foreach ($msgarr['images'] as $image) {
if (++$counter > 4) {
continue;
}
$img_str = Network::fetchUrl($image['url']);
$tempfile = tempnam(get_temppath(), 'cache');
file_put_contents($tempfile, $img_str);
@ -653,21 +656,24 @@ function twitter_post_hook(App $a, array &$b)
unlink($tempfile);
if (isset($media->media_id_string)) {
$post['media_ids'] = $media->media_id_string;
//$details = $cb->account_verifyCredentials();
$post['media_ids'] .= $media->media_id_string . ',';
if (!empty($image['description'])) {
$data = ['media_id' => $media->media_id_string,
'alt_text' => ['text' => substr($image['description'], 0, 420)]];
$ret = $cb->media_metadata_create($data);
Logger::info('Metadata create', ['data' => $data, 'return' => json_encode($ret)]);
}
} else {
throw new Exception('Failed upload of ' . $image);
throw new Exception('Failed upload of ' . $image['url']);
}
}
$post['media_ids'] = rtrim($post['media_ids'], ',');
if (empty($post['media_ids'])) {
unset($post['media_ids']);
}
} catch (Exception $e) {
Logger::log('Exception when trying to send to Twitter: ' . $e->getMessage());
// Workaround: Remove the picture link so that the post can be reposted without it
// When there is another url already added, a second url would be superfluous.
if (!$url_added) {
$msg .= "\n" . $image;
}
$image = "";
}
}