LoginSignup
2
2

More than 5 years have passed since last update.

Twitterのスレッドを1テキストにまとめる

Last updated at Posted at 2017-12-29

任意のツイートから遡ってツイートのテキスト部分だけをまとめるツールです。
色々と適当です。

参考:Twitter APIでつぶやきを取得する
https://qiita.com/yokoh9/items/760e432ebd39040d5a0f

Twitter Apps
https://apps.twitter.com/

twitteroauth
https://github.com/abraham/twitteroauth/releases

使い方:一番上のフォームにスレッドの一番最後のツイートのURLを貼る。ボタンを押す。
100以上ツイートがある場合、一番下のフォームに次のツイートのIDが表示されます。
それをコピーして一番上のフォームにペーストしてボタンを押せば、前のツイートを取得できます。

index.php
<?php
include('header.php');
use Abraham\TwitterOAuth\TwitterOAuth;


if (!empty($_REQUEST['twid'])) {
    $twid = substr($_REQUEST['twid'], strrpos($_REQUEST['twid'], '/')+1);
    $storage = "'/$twid'";
} else {
    $twid = '';
    $storage = "window.localStorage.getItem('twId')";
}

?>

<script>
$(document).ready(function(){
    function changeText () {
        var tcount = $("#twtext").val().length;
        $("#tcount").text(tcount);
        window.localStorage.setItem('twId', $("#twid").val());
    }

    $("#twid").val(<?php echo $storage?>);
    changeText;

    $("#twid").bind("change keyup click", (changeText));
    $("#twtext").bind("change keyup click", (changeText));
    $("#twReset").click(function(){
        $("#twid").val('');
        $("#twid").focus();
    });
});
</script>
<form action="index.php" method="get">
<div class="form-group row align-items-center">
<div class="input-group">
<input type="button" id="twReset" value="×" class="input-group-addon form-control form-control-sm" style="flex:1;display:block;width:30px;">
<input type="text" id="twid" name="twid" class="input-group form-control form-control-sm" placeholder="最後のツイート" autocomplete="off" style="flex:8;" value="/<?php echo $twid?>">
<input type="submit" id="" value="スレッド読込" class="form-control form-control-sm btn-sm btn-primary" style="flex:3;display:block">
</div>
</div>
</form>

<form action="split.php" method="post">
<div class="form-group row align-items-center">
</div>


<?php
    // スレッドを取得
    $connection = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
    $thread = '';
    for ($i = 0; $i < 100; $i++) {
        if (!empty($twid)) {
            $statuses = $connection->get("statuses/show", array("id" => $twid, "tweet_mode"=>"extended"));
            $thread = $statuses->full_text."\n".$thread;
            $twid = $statuses->in_reply_to_status_id;
        } else {
            break;
        }
    }
?>

スレッド内容(最大100ツイート)<br>
<div class="form-group row">
<span style="position:absolute;top:2;left:0;font-size:6px;background-color:#fff;opacity:0.6;color:#0099ff;" id="tcount"></span>
<textarea id="twtext" name="text" class="form-control form-control-sm" rows=15><?php echo $thread?></textarea>
最初のツイート<input type="text" class="form-control form-control-sm" value="/<?php echo $twid?>">
</div>
</form>

<?php

include('footer.php');

?>
header.php
<?php
ini_set( 'display_errors', 1 );

// タイムゾーン
date_default_timezone_set('Asia/Tokyo');

require "twitteroauth/autoload.php";

// APIキー
require "./key.php";
$consumerKey = YOUR_CONSUMER_KEY;
$consumerSecret = YOUR_CONSUMER_SECRET;
$accessToken = YOUR_ACCESSTOKEN;
$accessTokenSecret = YOUR_ACCESSTOKEN_SECRET;

mb_internal_encoding("UTF-8");

?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="robots" content="noindex,nofollow">
    <meta id="_robots" content="noindex,nofollow">
    <title>ツイートまとめ</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="  crossorigin="anonymous"></script>

<style type="text/css">
#pageTop {

  position: fixed;
  bottom: 20px;
  right: 20px;
}

#pageTop a {
  display: block;
  z-index: 999;
  padding: 4px 8px 8px 8px;
  border-radius: 30px;
  width: 35px;
  height: 35px;
  background-color: #9FD6D2;
  color: #fff;
  font-weight: bold;
  text-decoration: none;
  text-align: center;
  opacity: 0.8;
}

#pageTop a:hover {
  text-decoration: none;
  opacity: 0.6;
}
</style>
</head>
<body>
<div class="container" style="margin-top:20px;">
<div class="row">
<div class="col-11 col-md-6">

footer.php
</div>

</div>
</div><!-- /.container -->
<hr style="margin-bottom:400px;">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
<script>
$(function () {
    var topBtn = $('#pageTop');
    topBtn.hide();
    //スクロールが30に達したらボタン表示
    $(window).scroll(function () {
        if ($(this).scrollTop() > 30) {
            topBtn.fadeIn();
        } else {
            topBtn.fadeOut();
        }
    });
    //スクロールでトップへもどる
    topBtn.click(function () {
        $('body,html').animate({
            scrollTop: 0
        }, 500);
        return false;
    });
});
</script>
<p id="pageTop"><a href="#"></a></p>
</body>
</html>

key.phpに自分のアカウントで取得したKEYをセットしておけば、鍵アカウントでも取得できます。

key.php
<?php

define("YOUR_CONSUMER_KEY", "");
define("YOUR_CONSUMER_SECRET", "");
define("YOUR_ACCESSTOKEN", "");
define("YOUR_ACCESSTOKEN_SECRET", "");

?>
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2