Last active
August 29, 2015 13:56
-
-
Save vibbow/9298381 to your computer and use it in GitHub Desktop.
自动RT指定Twitter用户的所有Tweet。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
set_time_limit(300); | |
mb_internal_encoding("UTF-8"); | |
header('Content-Type: text/html; charset=utf-8'); | |
define('APP_DIR', __DIR__ . DIRECTORY_SEPARATOR); | |
define('TWIP_URL', 'TWIP O 模式地址'); // TWIP O 模式地址,实在懒得去处理OAuth的东西了 | |
define('TWITTER_ID', 'Twitter User ID'); // Twitter 用户的 ID,防止用户改名 | |
define('ADD_STR', '#土豪 的日常 RT @[USERNAME] '); // 要附加在原始推文前的内容 | |
$last_id = file_exists(APP_DIR . 'last.id') ? file_get_contents(APP_DIR . 'last.id') : NULL; | |
// 如果还没有last.id文件,则获取最后一条推文的tweet_id | |
if (empty($last_id)) { | |
echo "Generate last id"; | |
$url = TWIP_URL . 'statuses/user_timeline.json?user_id=' . TWITTER_ID . '&count=1&include_rts=false'; | |
$last_id = json_decode(file_get_contents($url), TRUE)[0]['id_str']; | |
file_put_contents(APP_DIR . 'last.id', $last_id); | |
} | |
else { | |
echo "Get tweets<br />\n"; | |
$url = TWIP_URL . 'statuses/user_timeline.json?user_id=' . TWITTER_ID . '&include_rts=false&since_id=' . $last_id; | |
$tweets = json_decode(file_get_contents($url), TRUE); | |
foreach ($tweets as $tweet) { | |
$content = $tweet['text']; | |
$tweet_id = $tweet['id_str']; | |
$username = $tweet['user']['screen_name']; | |
// 如果当前tweet_id比保存的tweet_id要新,则将当前的tweet_id保存为last_id | |
if (gmp_cmp($tweet_id, $last_id) > 0) { | |
$last_id = $tweet_id; | |
file_put_contents(APP_DIR . 'last.id', $last_id); | |
} | |
// 过滤掉 @用户开头的, 引用转推, RT转推 | |
if ((preg_match('/^@[a-zA-Z0-9_]+ .+/', $content) > 0) || | |
(preg_match('/"@[a-zA-Z0-9_]+: @[a-zA-Z0-9_]+ .+"/', $content) > 0) || | |
(preg_match('/RT @[a-zA-Z0-9_]+ .+/', $content) > 0)) { | |
echo "Skip: {$content}<br />\n"; | |
continue; | |
} | |
else { | |
echo "Rewteet: {$content}<br />\n"; | |
// 组合新的推文 | |
$prefix = str_replace('[USERNAME]', $username, ADD_STR); | |
$new_content = $prefix . $content; | |
// RT回复推文 | |
rt_reply($new_content, $tweet_id); | |
} | |
} | |
} | |
function rt_reply($content, $tweet_id) { | |
if (mb_strlen($content) > 140) { | |
$content = mb_substr($content, 0, 140); | |
} | |
$url = TWIP_URL . 'statuses/update.json'; | |
$post = array( | |
'status' => $content, | |
'in_reply_to_status_id ' => $tweet_id | |
); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_POST, TRUE); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); | |
curl_exec($ch); | |
curl_close($ch); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment