0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

twitter api v1 でリストを極める

Posted at

twitter api v2 は微妙に機能が足りない。
ということで、twitter api v1 を使う。

今回は laravel にて。
use Abraham\TwitterOAuth\TwitterOAuth;
を利用

指定したリストから200件取得し人気のツイート順に並べる

$twi = new TwitterOAuth(
    config('app.twitter_client_id'),//APIKEY
    config('app.twitter_client_secret'),//APISECRET
    config('app.twitter_account_token'),//ユーザーのトークン
    config('app.twitter_account_token_secret')//ユーザーのシークレットトークン
);


$timeline = $twi->get("lists/statuses", [
    "list_id" => "1612264628268204043",
    "count" => 200
]);



$res = [];
foreach ($timeline as $v) {

    $res[] = [
        'id' => $v->id,
        'text' => $v->text,
        'created_at' => date('Y-m-d H:i:s', strtotime($v->created_at)),
        'retweet_count' => $v->retweet_count,
        'favorite_count' => $v->favorite_count,
        'retweet_favorite_sum_count' => $v->retweet_count+$v->favorite_count,
        'user' => [
            'id' => $v->user->id,
            'name' => $v->user->name,
            'screen_name' => $v->user->screen_name
        ]
    ];

}

$res = collect($res);
$res = $res->sortByDesc('favorite_count')->values()->toArray();

結果

Array
(
    [0] => Array
        (
            [id] => 1612350183727312896
            [text] => RT @kawaiyorikirei: え、待って待って!!今岡田将生と谷口彰悟がフリーってこと⁉︎ 
それって私にも可能性があるってこと⁉︎ https://t.co/EZeleJaNtb
            [created_at] => 2023-01-09 16:26:54
            [retweet_count] => 11
            [favorite_count] => 0
            [retweet_favorite_sum_count] => 11
            [user] => Array
                (
                    [id] => 1411627715996778496
                    [name] => もふ子🌸
                    [screen_name] => mofuko_n_n
                )

        )

    [1] => Array
        (
            [id] => 1612346131857702918
            [text] => 4人中2人は子持ちで、
1人は第二子妊娠中だった🫣🫣🫣
1人はおこちゃま背負いながらお教室やってた🤭

前までの私だったら
あー…いいなぁ幸せそう
って思って多分もやっとしていただろう。
妊娠もその日に知ったし。
今はいつか自分も… https://t.co/iMP3dtGDfz
            [created_at] => 2023-01-09 16:10:48
            [retweet_count] => 0
            [favorite_count] => 2
            [retweet_favorite_sum_count] => 2
            [user] => Array
                (
                    [id] => 1411627715996778496
                    [name] => もふ子🌸
                    [screen_name] => mofuko_n_n
                )

        )

指定したユーザーのリスト一覧を取得

(リストを持つユーザーでログインしていないと、エラーが出る)


$list_ids = $twi->get("lists/list", [
    "screen_name" => "twikon_club"
]);

結果

Array
(
    [0] => stdClass Object
        (
            [id] => 1612264628268204043
            [id_str] => 1612264628268204043
            [name] => 婚活
            [uri] => /twikon_club/lists/1612264628268204043
            [subscriber_count] => 0
            [member_count] => 49
            [mode] => private
            [description] => 婚活の識者様をご登録させて頂いております。

//以下略

指定したリストにユーザーを追加

$member_tmp = $twi->post("lists/members/create", [
    "list_id" => "1612264628268204043",//listのid
    "slug" => " 1612264628268204043",//listのid
    "owner_screen_name" => "twikon_club",//リストの持ち主のscreen_name
    "user_id" => "125023968"//登録したいユーザー
]);

「婚活」クラスタのユーザーを取得

$member_tmp = $twi->get("users/search", [
    "q" => "婚活",
    "page" => 1,
    "count" => 10
]);

以上

・婚活クラスタのユーザーを取得
・婚活リストに上記ユーザーを追加
・婚活リストから人気のツイートを検索すれば、婚活に対する人気のツイートがなにかを取得することが可能。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?