Merge pull request #569 from annando/twitter-improve
Twitter: Code for sending media to Twitter has been optimised
This commit is contained in:
commit
5d4c3a1da9
|
@ -528,8 +528,8 @@ function twitter_post_hook(App $a, &$b)
|
||||||
|
|
||||||
$connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
|
$connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
|
||||||
|
|
||||||
// Set the timeout for upload to 15 seconds
|
// Set the timeout for upload to 30 seconds
|
||||||
$connection->setTimeouts(10, 15);
|
$connection->setTimeouts(10, 30);
|
||||||
|
|
||||||
$max_char = 280;
|
$max_char = 280;
|
||||||
$msgarr = BBCode::toPlaintext($b, $max_char, true, 8);
|
$msgarr = BBCode::toPlaintext($b, $max_char, true, 8);
|
||||||
|
@ -543,14 +543,23 @@ function twitter_post_hook(App $a, &$b)
|
||||||
|
|
||||||
if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
|
if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
|
||||||
$msg .= "\n" . $msgarr["url"];
|
$msg .= "\n" . $msgarr["url"];
|
||||||
|
$url_added = true;
|
||||||
|
} else {
|
||||||
|
$url_added = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($msgarr["image"]) && ($msgarr["type"] != "video")) {
|
if (isset($msgarr["image"]) && ($msgarr["type"] != "video")) {
|
||||||
$image = $msgarr["image"];
|
$image = $msgarr["image"];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (empty($msg)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// and now tweet it :-)
|
// and now tweet it :-)
|
||||||
if (strlen($msg) && ($image != "")) {
|
$post = [];
|
||||||
|
|
||||||
|
if (!empty($image)) {
|
||||||
try {
|
try {
|
||||||
$img_str = Network::fetchUrl($image);
|
$img_str = Network::fetchUrl($image);
|
||||||
|
|
||||||
|
@ -561,60 +570,27 @@ function twitter_post_hook(App $a, &$b)
|
||||||
|
|
||||||
unlink($tempfile);
|
unlink($tempfile);
|
||||||
|
|
||||||
$post = ['status' => $msg, 'media_ids' => $media->media_id_string];
|
$post['media_ids'] = $media->media_id_string;
|
||||||
|
|
||||||
if ($iscomment) {
|
|
||||||
$post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = $connection->post('statuses/update', $post);
|
|
||||||
|
|
||||||
logger('twitter_post_with_media send, result: ' . print_r($result, true), LOGGER_DEBUG);
|
|
||||||
|
|
||||||
if ($result->source) {
|
|
||||||
Config::set("twitter", "application_name", strip_tags($result->source));
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($result->errors || $result->error) {
|
|
||||||
logger('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
|
|
||||||
|
|
||||||
// Workaround: Remove the picture link so that the post can be reposted without it
|
|
||||||
$msg .= " " . $image;
|
|
||||||
$image = "";
|
|
||||||
} elseif ($iscomment) {
|
|
||||||
logger('twitter_post: Update extid ' . $result->id_str . " for post id " . $b['id']);
|
|
||||||
Item::update(['extid' => "twitter::" . $result->id_str, 'body' => $result->text], ['id' => $b['id']]);
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
logger('Exception when trying to send to Twitter: ' . $e->getMessage());
|
logger('Exception when trying to send to Twitter: ' . $e->getMessage());
|
||||||
|
|
||||||
// Workaround: Remove the picture link so that the post can be reposted without it
|
// Workaround: Remove the picture link so that the post can be reposted without it
|
||||||
$msg .= " " . $image;
|
// When there is another url already added, a second url would be superfluous.
|
||||||
|
if (!$url_added) {
|
||||||
|
$msg .= "\n" . $image;
|
||||||
|
}
|
||||||
|
|
||||||
$image = "";
|
$image = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strlen($msg) && ($image == "")) {
|
$post['status'] = $msg;
|
||||||
// -----------------
|
|
||||||
$max_char = 280;
|
|
||||||
$msgarr = BBCode::toPlaintext($b, $max_char, true, 8);
|
|
||||||
$msg = $msgarr["text"];
|
|
||||||
|
|
||||||
if (($msg == "") && isset($msgarr["title"])) {
|
|
||||||
$msg = Plaintext::shorten($msgarr["title"], $max_char - 50);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($msgarr["url"])) {
|
|
||||||
$msg .= "\n" . $msgarr["url"];
|
|
||||||
}
|
|
||||||
// -----------------
|
|
||||||
$url = 'statuses/update';
|
|
||||||
$post = ['status' => $msg, 'weighted_character_count' => 'true'];
|
|
||||||
|
|
||||||
if ($iscomment) {
|
if ($iscomment) {
|
||||||
$post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
|
$post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$url = 'statuses/update';
|
||||||
$result = $connection->post($url, $post);
|
$result = $connection->post($url, $post);
|
||||||
logger('twitter_post send, result: ' . print_r($result, true), LOGGER_DEBUG);
|
logger('twitter_post send, result: ' . print_r($result, true), LOGGER_DEBUG);
|
||||||
|
|
||||||
|
@ -640,7 +616,6 @@ function twitter_post_hook(App $a, &$b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function twitter_addon_admin_post(App $a)
|
function twitter_addon_admin_post(App $a)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user