自身がフォローしているTwitterアカウントでリスト自動仕分けする方法!?

20220124

Logging

昨日は雨がシトシトと降っていた高知県ですが、あまり寒さを感じなくなってきていますね😌。早く春になれば良いのになって思っております。

ソースコードを読んでいただければ大体分かるかとも思いますが、そんなに難しいコードではありません。タイトル通りの処理をしています。コマンドからファイルを叩くと処理が実行されてそれぞれのリストに仕分けされます、ここでポイントなのはlist_idはどうやって導けばよいのという疑問とTwitterOAuthって何という疑問ぐらいかと思います。

list_idは事前に空のリストを生成すると自動的に割り振られるご自身のリストURLの数値部分になります。次にTwitterOAuthというのは何かというと、これはTwitterAPIを簡単に叩けるライブラリになります。これを事前にインストールすることにより簡単に処理ができます。

尚、ソースコードはTwitterAPI2.0バージョンではありません。そのうち廃止される方で書いています。

<?php
require_once("../vendor/autoload.php");

use Abraham\TwitterOAuth\TwitterOAuth;

if ($argv[0]) {
    require_once "./tw-config.php";

    date_default_timezone_set('Asia/Tokyo');
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
    $response_followers = $connection->get("friends/ids", array(
        'screen_name' => 'zip358com',
        'count' => 1000
    ));
    if ($response_followers->ids) {
        foreach ($response_followers->ids as $key => $val) {
            $response_users = $connection->get("users/show", array(
                'user_id' => $val
            ));
            if(preg_match("/(機械学習|人工知能|AI|Learning)/",$response_users->description)){
                print "[機械学習|人工知能|AI|Learning]". $response_users->id . PHP_EOL . $response_users->description . "," . PHP_EOL;
                $connection->post("lists/members/create", array(
                    'list_id'=>1485120628206497798,
                    'user_id'=>$response_users->id
                ));
            }
            if(preg_match("/(web|WEB|Web|プログラマー|エンジニア|プログラム|プログラミング|API)/",$response_users->description)){
                print "(web|WEB|Web|プログラマー|エンジニア|プログラム|プログラミング|API)". $response_users->id . PHP_EOL . $response_users->description . "," . PHP_EOL;
                $connection->post("lists/members/create", array(
                    'list_id'=>1485121383101526018,
                    'user_id'=>$response_users->id
                ));
            }
            if(preg_match("/(イラスト|写真|デザイン|art|Art|絵|漫画)/",$response_users->description)){
                print "(イラスト|写真|デザイン|art|Art|絵|漫画)". $response_users->id . PHP_EOL . $response_users->description . "," . PHP_EOL;
                $connection->post("lists/members/create", array(
                    'list_id'=>1485121210816294912,
                    'user_id'=>$response_users->id
                ));
            }
            if(preg_match("/(電車|メトロ|運行情報)/",$response_users->description)){
                print "(電車|メトロ)". $response_users->id . PHP_EOL . $response_users->description . "," . PHP_EOL;
                $connection->post("lists/members/create", array(
                    'list_id'=>1485121509320687619,
                    'user_id'=>$response_users->id
                ));
            }            
            if(preg_match("/(高知県|高知市)/",$response_users->description)||preg_match("/(高知県|高知市|kochi)/",$response_users->location)){
                print "(高知県|高知市)". $response_users->id . PHP_EOL . $response_users->description . "," . PHP_EOL;
                $connection->post("lists/members/create", array(
                    'list_id'=>1485121289165893632,
                    'user_id'=>$response_users->id
                ));
            }                        
        }
    }
}

タグ

ID, LIST, Twitte, Twitter, TwitterAPI, TwitterOAuth, url, アカウント, インストール, コード, ここ, こと, コマンド, これ, ご自身, ソース, それぞれ, タイトル, ファイル, フォロー, ポイント, ライブラリ, リスト, 事前, , 処理, 実行, 数値, 方法, , 昨日, 生成, 疑問, , 簡単, 自動, 自身, 部分, , 高知県,