Skip to content

Instantly share code, notes, and snippets.

@kenichimiki
Created August 9, 2019 03:15
Show Gist options
  • Save kenichimiki/88a7c6f149d160b2f96d184922877d4a to your computer and use it in GitHub Desktop.
Save kenichimiki/88a7c6f149d160b2f96d184922877d4a to your computer and use it in GitHub Desktop.
Post new tweet using TwitterOAuth
<?php
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
define("TARGET_TWEET",0);
##TARGET_TWEET 0 is newest record
$host_name = '127.0.0.1:3307';
$user_name = '@DB_User@';
$password = '@DB_Pass@';
$database_name ='@DB_Name@';
$table_name = '@DB_Table@';
function postTweet($value1, $value2) {
    $consumerKey = "@consumerKey@";
    $consumerSecret = "@consumerSecret@";
    $accessToken = "@accessToken@";
    $accessTokenSecret = "@accessTokenSecret@";
    $tweet_text = $value1.PHP_EOL.$value2;
    //文字数 URL=11.5文字、全角140文字、半角のみ280文字
    $url_char = strlen('https://www.miki-ie.com/mezamashi-jyanken/');
    $max_char = 280 + $url_char - 11.5;
    while(strlen($tweet_text) > $max_char){
        $tweet_text = mb_strrchr($tweet_text,'#',true,'UTF-8');
    }
    $twitter = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
    $result = $twitter->post(
     "statuses/update",
             array("status" => $tweet_text)
                );
    if($twitter->getLastHttpCode() == 200) {
        return true;
    } else {
        return false;
    }
}
$mysqli = new mysqli($host_name, $user_name, $password, $database_name);
if ($mysqli->connect_error) {
echo$mysqli->connect_error;
exit();
} else {
$mysqli->set_charset("utf8");
}
$table = "";
$sql = "SELECT id, times, tv, who FROM janken order by datetime desc limit 30";
if ($result = $mysqli->query($sql)) {
    $result = $mysqli->query($sql);
    $counter = 0;
while ($row = $result->fetch_assoc()) {
        if ($counter == TARGET_TWEET){
            $value1 = "【".$row["times"]."戦目】めざましじゃんけん結果速報:".PHP_EOL;
            if($row["tv"] == 1){
                $value1 = $value1."グー(Goo)"."でした。パーが勝ちです。";
            }elseif($row["tv"] == 2){
                $value1 = $value1."チョキ(Chyoki)"."でした。グーが勝ちです。";
            }else{
                $value1 = $value1."パー(Pa)"."でした。チョキが勝ちです。";
            }
            $value1 = $value1.PHP_EOL."今回の相手:".$row["who"];
            $who = $row["who"];
            $num_week = date('w', strtotime($row["datetime"]));
        }
        $counter++;
}
$result->close();
}
$mysqli->close();
$who = str_replace('さん','',$who);
$who = str_replace('アナウンサー','',$who);
$who = str_replace('リポーター','',$who);
$who = str_replace('キャスター','',$who);
$who = str_replace('生じゃんけん!','',$who);
//全角スペースを半角スペースに変換、句読点やカッコも半角スペースに変換
$who = str_replace(' ', ' ', $who);
$who = str_replace('、', ' ', $who);
$who = str_replace('(', ' ', $who);
$who = str_replace(')', ' ', $who);
//前後のスペース削除
$who = trim($who);
//連続する半角スペースを半角スペースひとつに変換
$who = preg_replace('/\s+/', ' ', $who);
//半角スペースで分割
$whos = explode(' ',$who);
$addhash = "";
if($num_week == 6) $addhash = " #めざましどようび";
foreach($whos as $this_who){
    $this_who = str_replace('-', '_', $this_who);
    $addhash = $addhash." #".$this_who;
}
$addhash2 = "";
$addhash_ary = array("機械学習", "AI", "深層学習", "DNN", "Darknet", "YOLO", "mikiie", "みきいえ", "ニューラルネットワーク");
shuffle($addhash_ary);
foreach($addhash_ary as $this_hash){
    $addhash2 = $addhash2." #".$this_hash;
}
$value2 = "By https://www.miki-ie.com/mezamashi-jyanken/".PHP_EOL." #めざましじゃんけん #めざましテレビ #人工知能".$addhash.$addhash2." ";
postTweet($value1, $value2);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment