Support for multiple picture posts

pull/874/head
Michael 2019-08-06 05:41:49 +00:00
parent 20ed3495b2
commit e214c21025
1 changed files with 32 additions and 26 deletions

View File

@ -621,19 +621,15 @@ function twitter_post_hook(App $a, array &$b)
$msg = Plaintext::shorten($msgarr["title"], $max_char - 50); $msg = Plaintext::shorten($msgarr["title"], $max_char - 50);
} }
$image = ""; if (($msgarr['url'] == $b['plink']) && !empty($msgarr['images']) && (count($msgarr['images']) <= 4)) {
$url_added = false;
if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) { } elseif (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
$msg .= "\n" . $msgarr["url"]; $msg .= "\n" . $msgarr["url"];
$url_added = true; $url_added = true;
} else { } else {
$url_added = false; $url_added = false;
} }
if (isset($msgarr["image"]) && ($msgarr["type"] != "video")) {
$image = $msgarr["image"];
}
if (empty($msg)) { if (empty($msg)) {
return; return;
} }
@ -641,9 +637,16 @@ function twitter_post_hook(App $a, array &$b)
// and now tweet it :-) // and now tweet it :-)
$post = []; $post = [];
if (!empty($image)) { if (!empty($msgarr['images'])) {
try { 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'); $tempfile = tempnam(get_temppath(), 'cache');
file_put_contents($tempfile, $img_str); file_put_contents($tempfile, $img_str);
@ -653,21 +656,24 @@ function twitter_post_hook(App $a, array &$b)
unlink($tempfile); unlink($tempfile);
if (isset($media->media_id_string)) { if (isset($media->media_id_string)) {
$post['media_ids'] = $media->media_id_string; $post['media_ids'] .= $media->media_id_string . ',';
//$details = $cb->account_verifyCredentials();
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 { } 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) { } catch (Exception $e) {
Logger::log('Exception when trying to send to Twitter: ' . $e->getMessage()); 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 = "";
} }
} }