Created
June 8, 2020 19:37
-
-
Save PJZ9n/75ca19cffe316d5094a86ff56f2a1d83 to your computer and use it in GitHub Desktop.
個人用、本番環境用ではない、テスト用。
This file contains hidden or 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 | |
/** | |
* Copyright (c) 2020 PJZ9n. | |
* | |
* This file is part of Hello-TwitterAPI. | |
* | |
* Hello-TwitterAPI is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* Hello-TwitterAPI is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with Hello-TwitterAPI. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
declare(strict_types=1); | |
require_once "env.php"; | |
require_once "vendor/autoload.php"; | |
use Abraham\TwitterOAuth\TwitterOAuth; | |
session_start(); | |
$twitterUser = new TwitterOAuth( | |
TWITTER_API_CONSUMER_KEY, | |
TWITTER_API_SECRET_KEY, | |
TWITTER_API_USER_TOKEN, | |
TWITTER_API_USER_TOKEN_SECRET | |
); | |
$allTweets = []; | |
$before = new DateTime(date("Y/m/d 00:00:00"), new DateTimeZone("Asia/Tokyo")); | |
for ($i = 0; $i < 5; $i++) { | |
if (isset($max)) { | |
$tweets = $twitterUser->get("statuses/user_timeline", ["count" => 200, "max_id" => $max]); | |
} else { | |
$tweets = $twitterUser->get("statuses/user_timeline", ["count" => 200]); | |
} | |
foreach ($tweets as $tweet) { | |
if (isset($max) && $tweet->id === $max) { | |
continue; | |
} | |
$createdAt = new DateTime($tweet->created_at); | |
$createdAt->setTimezone(new DateTimeZone("Asia/Tokyo")); | |
if ($createdAt < $before) { | |
break 2; | |
} | |
$allTweets[] = $tweet; | |
} | |
$last = end($tweets); | |
$max = $last->id; | |
} | |
$sort = []; | |
foreach ($allTweets as $tweet) { | |
$createdAt = new DateTime($tweet->created_at); | |
$createdAt = $createdAt->setTimezone(new DateTimeZone("Asia/Tokyo")); | |
$sort[] = $createdAt; | |
} | |
array_multisort($sort, SORT_DESC, $allTweets); | |
//echo json_encode($allTweets, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); | |
$tweetc = count($allTweets); | |
$rt = 0; | |
$mention = 0; | |
$mention2 = 0; | |
foreach ($allTweets as $tweet) { | |
if (isset($tweet->retweeted_status)) { | |
$rt++; | |
} | |
if (count($tweet->entities->user_mentions) >= 1) { | |
$mention++; | |
} | |
$mention2 += count($tweet->entities->user_mentions); | |
} | |
$twitterUser = new TwitterOAuth( | |
TWITTER_API_CONSUMER_KEY, | |
TWITTER_API_SECRET_KEY, | |
TWITTER_API_BOT_TOKEN, | |
TWITTER_API_BOT_TOKEN_SECRET | |
); | |
$mentions = $twitterUser->get("statuses/mentions_timeline", ["count" => 200]); | |
foreach ($mentions as $mentiona) { | |
$fromUser = $mentiona->user->screen_name; | |
if ($fromUser !== "pjz9n") { | |
continue; | |
} | |
$text = $mentiona->text; | |
if (strpos($text, "!test") === false) { | |
continue; | |
} | |
echo "fav" . PHP_EOL; | |
$rr = $twitterUser->post("favorites/create", [ | |
"id" => $mentiona->id, | |
]); | |
echo "<pre>"; | |
var_dump($rr); | |
echo "</pre>"; | |
echo "いいね: {$twitterUser->getLastHttpCode()}" . PHP_EOL; | |
echo "reply" . PHP_EOL; | |
//var_dump($tweet, $rt, $mention, $mention2); | |
$r = $twitterUser->post("statuses/update", [ | |
"status" => "@{$fromUser} {$before->format("Y/m/d")}のデータ\nツイート数: {$tweetc}\n内RT数: {$rt}\nメンション数: {$mention}\nメンション人数: {$mention2}", | |
"in_reply_to_status_id" => $mentiona->id, | |
]); | |
echo "<pre>"; | |
var_dump($r); | |
echo "</pre>"; | |
echo "リプライ: {$twitterUser->getLastHttpCode()}" . PHP_EOL; | |
break; | |
} | |
?> | |
<html lang="ja"> | |
<head> | |
<title>Twitter</title> | |
</head> | |
<body> | |
<h1>Twitterツイート取得</h1> | |
<hr> | |
<p><?php echo count($allTweets) ?>個のツイートを取得しました。内RT<?php echo $rt ?>, メンション<?php echo $mention ?> | |
(<?php echo $mention2 ?>人)</p> | |
<ul> | |
<?php foreach ($allTweets as $tweet) : ?> | |
<li> | |
<ul> | |
<li> | |
<?php echo (new DateTime($tweet->created_at))->setTimezone(new DateTimeZone("Asia/Tokyo"))->format("Y/m/d H:i:s") ?> | |
</li> | |
<li> | |
<?php echo htmlspecialchars($tweet->text) ?> | |
</li> | |
</ul> | |
</li> | |
<?php endforeach ?> | |
</ul> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment