Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Last active June 8, 2020 17:38
Show Gist options
  • Save PJZ9n/dd9c2b13071a85ca459d14fd6820dc71 to your computer and use it in GitHub Desktop.
Save PJZ9n/dd9c2b13071a85ca459d14fd6820dc71 to your computer and use it in GitHub Desktop.
当日のツイートをすべて取得
<?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_BOT_TOKEN,
TWITTER_API_BOT_TOKEN_SECRET
);
$allTweets = [];
$before = new DateTime(date("Y/m/d 00:00:00"), new DateTimeZone("Asia/Tokyo"));
for ($i = 0; $i < 5; $i++) {
$tweets = $twitterUser->get("statuses/user_timeline", ["count" => 200]);
foreach ($tweets as $tweet) {
$createdAt = new DateTime($tweet->created_at);
$createdAt->setTimezone(new DateTimeZone("Asia/Tokyo"));
if ($createdAt < $before) {
break 2;
}
$allTweets[] = $tweet;
}
}
$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);
$tweet = 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);
}
?>
<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