[ニコニコ動画リプライ]
<?php
// twitteroauth.phpを読み込む。パスはあなたが置いた適切な場所に変更してください
require_once("twitteroauth.php");
//OAuthキーを入力
$consumer_key = "";
$consumer_secret = "";
$access_token = "";
$access_token_secret = "";
//最後に読み込んだリプライを保存するtxtファイル(パーミッションを書き込み可に)
$filename = "";
$host = "https://api.twitter.com/1.1/statuses/mentions_timeline.json"; //返信一覧
$fp = @fopen($filename,'rb') or die("ファイルが開けません");
flock($fp, LOCK_EX);
$line = fgets($fp, 64);
fclose($fp);
// OAuthオブジェクト生成
$to = new TwitterOAuth($consumer_key,$consumer_secret,$access_token,$access_token_secret);
//保存しておいたステータスid取得。このid以降の返信を読み込む。なかったり読み込めなかったら最新1件
if(!empty($line)){
$req = $to->OAuthRequest($host,"GET",array("since_id"=>$line));
}else{
$req = $to->OAuthRequest($host,"GET",array("count"=>"1"));
}
//json文字列をオブジェクトに代入する
$JSON = json_decode($req);
//フレンドリストのオブジェクト作成
$host = "https://api.twitter.com/1.1/friends/ids.json";
$req = $to->OAuthRequest($host,"GET",array());
$friends = json_decode($req);
$friendslist = array();
foreach($friends->ids as $item){
$friendslist[] = (String)$item;
}
//順に返信
$i = count($JSON)-1;
$j = 0;
while($i >= $j) {
$var = $JSON[$i]->text;
$com = ereg_replace("\@$username", "", $var);
$reply_name = $JSON[$i]->user->screen_name;
$name = $JSON[$i]->user->name;
$id = $JSON[$i]->id;
//ゴチャゴチャするので、返信のセリフ組み合わせはユーザー定義関数で。
$message = serif($reply_name,$name,$com,$id);
//そのままポストの関数へ…
if($message){
$result = $to->OAuthRequest("https://api.twitter.com/1.1/statuses/update.json","POST",array("status"=>$message,"in_reply_to_status_id"=>"$id"));
print $result;
}
$i--;
}
//最終発言リプライのidを保存してる部分。先頭を調べて…
$string = $JSON[0]->id;
//空じゃなかったら書き込み
if(!empty($string)){
$dat = (string)$string;
file_put_contents($filename,$dat,LOCK_EX);
}
//これでメインの処理は終わり
function getTinyUrl($url) {
return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
}
//セリフ生成のユーザー定義関数
function serif($reply_name,$name,$com,$id){
$break_flag = false;
$today = date("Y/m/d l H:i:s");//デバッグ用の日付表示(なくてよい)
//ニコニコ動画
$sort_shitei = array( // ソート用配列
"コメントが新しい"=>"?","コメントが古い"=>"?order=a&","再生数が多い"=>"?sort=v&","再生数が少ない"=>"?sort=v&order=a&","コメントが多い"=>"?sort=r&","コメントが少ない"=>"?sort=r&order=a&","マイリスト数が多い"=>"?sort=m&","マイリスト数が少ない"=>"?sort=m&order=a&","投稿日時が新しい"=>"?sort=f&","投稿日時が古い"=>"?sort=f&order=a&","再生時間が長い"=>"?sort=l&","再生時間が短い"=>"?sort=l&order=a&" );
foreach( $sort_shitei as $sort_ad => $sort){
if( preg_match( "/".$sort_ad."/u", $com ) ){
$sortName = $sort_ad ;
break;
}
}
if( empty($sortName) ){
$sortName = "コメントが新しい";
}
$tag_shitei = array( // 対応するタグ設定
"初見ブルースでロックマン9","クイント","ロックマンキラー","未来からの挑戦者","バトル&チェイス","プラグマン","魔界塔士","夢をみる島","ロックマン" );
foreach( $tag_shitei as $tag){
if( preg_match( "/".$tag."/u", $com ) ){
$tagName = $tag ;
break;
}
}
if( empty($tagName) ){
$tagName = "ロックマン";
//指定がなかった場合のデフォルトタグ
}
$tag2 = urlencode($tag);
$nico_url = "http://www.nicovideo.jp/tag/".$tag2."".$sort."rss=atom";
$tag_url = "http://www.nicovideo.jp/tag/".$tag2;
$nicoxml = simplexml_load_file($nico_url);
$nicotitle = $nicoxml->entry[0]->title;
$dougasuu = $nicoxml->subtitle;
$nico_time = $nicoxml->entry[0]->content->div->p[2]->small->strong[0];
$nicoad = $nicoxml->entry[0]->link['href'];
$nicoad2 = str_replace("http://www.nicovideo.jp/watch/","http://nico.ms/",$nicoad);
$tagurl = getTinyUrl($tag_url);
//上記のフラグが成立していればスルー、していなければ語句のマッチを探す
if($break_flag === false){
//配列を用意
$douga=array("「".$tagName."」タグの".$sortName."動画は「".$nicotitle."」(".$nico_time.") ".$nicoad2." みたいだね。");
$tagurl=array("".$dougasuu."のアドレスは ".$tagurl." だよ。何か面白いものでも見つかるといいね。");
//特定語句に反応する部分
if($reply_name != null){
$reply_name = "@$reply_name";
if(stristr($com, "RT")){
$return = null;
}else if(preg_match("/教えて/u",$com)){
if(preg_match("/タグ/u",$com)){
$key = array_rand($tagurl);
$post = $tagurl[$key];
$return = $reply_name." ".$post;
}else if(preg_match("/動画/u",$com)){
$key = array_rand($douga);
$post = $douga[$key];
$return = $reply_name." ".$post;
}
}
}
//戻り値
return $return;
}else{
//$reply_nameが空のときはnullになる
return null;
}
}
?>
一覧に戻る |