Skip to content

Instantly share code, notes, and snippets.

@mikuta0407
Last active August 7, 2023 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikuta0407/955808cff9eb725a313a17286c43b558 to your computer and use it in GitHub Desktop.
Save mikuta0407/955808cff9eb725a313a17286c43b558 to your computer and use it in GitHub Desktop.
くちをひらくの更新通知ツイートBot(@kuchihira_bot)のソースコード
  • くちをひらくの更新通知Bot(@kuchihra_bot)のソースコードです
  • もちろんAPIキーとかは抜いてあります
  • phpです
  • PHP8.2 + Ubuntu 22.04で動作確認済みです
  • composerでabraham/twitteroauthを引っ張ってくる必要があります。
  • cronで動かしていい感じにしてます
    50 16 * * * php ~/kuchiwohiraku-bot/kuchibot.php >> ~/kuchiwohiraku-bot/log
    
{
"require": {
"abraham/twitteroauth": "^5.0"
}
}
# くちをひらくBot (16:50から取得を試み始める)
50 16 * * * php /path/to/kuchibot.php >> /path/to/kuchibot-log
<?php
require_once "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
// omnyfmのRSSのタイムゾーンがUTCなのでUTCにする
ini_set('date.timezone', 'UTC');
// 今日の日付を取得(数値として)
$today["year"] = (int)date("Y");
$today["month"] = (int)date("m");
$today["day"] = (int)date("d");
// 360回試行する。試行ごとに20秒sleepさせているので、最大約2時間試行。
for ($i = 1; $i <= 360; $i++ ) {
// omnyfmのRSSのXMLを取得+オブジェクトへ
$rssXML = file_get_contents("https://www.omnycontent.com/d/playlist/67122501-9b17-4d77-84bd-a93d00dc791e/bf2b4e95-c669-4e1c-abcf-a98c00a5f513/b923e360-dc05-438d-be85-a98c00a5f517/podcast.rss");
$rssObj = simplexml_load_string($rssXML);
// 最新の投稿を取得(普通の配列へ)
$item = json_decode(json_encode($rssObj->channel->item), true);
// 最新投稿のpubDate(投稿日)をdate_parseでパース
$latestPubDate = date_parse($item["pubDate"]);
// 一応ログ用に最新投稿のタイトルを出力しておく
echo $item["title"] . "\t";
// 投稿日が今日かどうかを判定
if (
$today["year"] == $latestPubDate["year"]
&& $today["month"] == $latestPubDate["month"]
&& $today["day"] == $latestPubDate["day"]
){
// true(=今日)ならforループを抜ける
break;
} else {
// false(=今日ではない=まだ前日分)なら20秒休んでもう一度ループ
echo "まだ更新されていません... i = $i\n";
sleep (20);
}
}
// もし$iが361=forが回り切った=ループ最終にインクリメントされてしまって361になった→2時間試行して更新が確認できなかったら諦める
// (個人Discord鯖のWebhookで通知t)
if ($i == 361) {
shell_exec("/mnt/DATA/library/notification.sh 'くちをひらくBot' '取得に失敗しました'");
die ("失敗しました") ;
}
// 投稿用に、pubDateの日付をJSTに変換
$tz = new DateTimeZone('Asia/Tokyo');
$date = new DateTime($item["pubDate"]);
$date->setTimezone($tz);
$dateStr = $date->format('Y/m/d H:i');
// 投稿文面生成
$body =
"『" . $item["title"] . "』 #くちをひらく" . "\n\n" .
"Web: https://omny.fm/shows/kuchiwohiraku/" . "\n" .
"Voicy: https://voicy.jp/channel/584" . "\n\n" .
"@yoshidahisanori @eriko_co_log @kuchiwohiraku" . "\n" .
$item["link"];
// 投稿(下記関数)
postTweet($body);
//var_dump($body);
// 投稿用関数
function postTweet ($body) {
// 一応ログ用に文面出力
echo $body;
// Twitterのアプリ用トークンとか(本当は環境変数的なのにしたいが、面倒なのでハードコーディング。頻繁に変えるものでもないので。)
$apiKey = "hoge";
$apiSecret = "fuga";
$accessToken = "piyo";
$accessTokenSecret = "neko";
// OAuth認証
$connection = new TwitterOAuth($apiKey, $apiSecret, $accessToken, $accessTokenSecret);
// APIバージョンをv2に指定
$connection->setApiVersion("2");
// 投稿
$result = $connection->post("tweets", ["text" => $body], true); # trueを忘れないように
// 結果のオブジェクトを配列に変換、ログ出力
$resultArray = json_decode(json_encode($result), true);
var_dump($resultArray);
// 投稿ができたかできなかったかを判定。ツイートIDがなかったらエラー。個人Discordに通知。
// /path/to/...は隠してるやつです
if (!isset($resultArray["data"]["id"])){
echo "Tweet Error!\n";
shell_exec("/path/to/notification.sh 'くちをひらくBot' '@mikuta0407 Tweet Error!'");
} else {
shell_exec("/path/to/notification.sh 'くちをひらくBot' 'Tweet Success'");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment