phpã§twitterã®botãä½ãï¼TLã«åå¿ä¿®æ£çï¼
昨日の記事ã®ç¶ãã§ãã
TLã®ç¹å®èªå¥ã«åå¿ãããã@ä»ãã®postã¨RTã«ã¯åå¿ããªããã¨ããä¿®æ£ç¹ããã¨ãeregããpreg_matchw使ãããã«ã
ã¨ããããã½ã¼ã¹
<?php $username = ""; $password = ""; //ææ°ã®TLã®ã¹ãã¼ã¿ã¹IDãä¿åãã¦ãããã¡ã¤ã«ã§ãããã¼ããã·ã§ã³ã¯æ¸ãè¾¼ã¿å¯ã«ã $filename = "./tmp/last.dat"; //éå»ã®éãã¦åå¾ $fp = @fopen($filename,'rb') or die("ãã¡ã¤ã«ãéãã¾ãã"); flock($fp, LOCK_EX); $line = fgets($fp, 64); fclose($fp); //èªåããã©ãã¼ãã¦ããfriendsã®ãªã¹ãã§ãã $host = "http://twitter.com/statuses/friends_timeline.xml"; //ã¦ã¼ã¶ã¼ã®çºè¨ä¸è¦§ //ä¸çªæåã®æããã¡ã¤ã«ãå£ããæãææ°1件ã®ã¿çºè¨ãåå¾ãã if(!empty($line)){ $last_id=$line; $host .="?since_id=".$last_id; } else{ $host .="?count=1"; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $host); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); $result = curl_exec($ch); curl_close($ch); $XML = simplexml_load_string($result); $i = count($XML)-1; $j = 0; while($i >= $j) { $var = $XML->status[$i]->text; $reply_name = $XML->status[$i]->user->screen_name; $name = $XML->status[$i]->user->name; if($reply_name != $username){ if( !preg_match ( "/@[a-zA-Z0-9-_]{3,}/", $var ) ){ if( preg_match ( "/ããã|å¯ã/u", $var )){ $message = "@".$reply_name." ãããã¿ï¼"; tweet($message ,$username,$password); } if(preg_match ( "/ãã¯ã|ããã/u", $var )){ $message = "@".$reply_name." ãã¯ããï¼"; tweet($message ,$username,$password); } if(preg_match ( "/ãã ãã¾/u", $var )){ $message = "@".$reply_name." ãããã"; tweet($message ,$username,$password); } } } $i--; } //ãã¡ã¤ã«ã«ææ°idæ¸ã込㿠$last_id = $XML->status[0]->id; $dat = (string)$last_id; file_put_contents($filename,$dat,LOCK_EX); //ãã¹ãé¨åã®é¢æ° function tweet($message ,$username,$password) { $message =urlencode($message); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://twitter.com/statuses/update.xml'); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "status=$message "); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); $buffer = curl_exec($ch); curl_close($ch); } ?>
解説ã§ãã
if( !preg_match ( "/@[a-zA-Z0-9-_]{3,}/", $var ) ){
ããã§ã@[ã¹ã¯ãªã¼ã³ãã¼ã ]ã®å
¥ã£ãçºè¨ãå¦ããå¤æãã¦ãã¾ãã
æ£è¦è¡¨ç¾ã§ã¾ãå
é ã«@ããã£ã¦ããã®ãã¨ä¸ã«ãã³[]ã§å²ã£ãa-zA-Z0-9ã¨ç¶ãã¾ãã
ä¸ã«ãã³ã¯ç¯å²ãªãã§ãããå¾ããã¤ãã³ã¨ã¢ã³ãã¼ãã¼ã使ãããï¼ãï¼ãã¤ãã³ä½¿ããã£ãï¼
ã¤ã¾ãåè§è±æ°ãã©ã¹-_ã
ãã®å¾ã«æ¥ã¦ãã®ã大ã«ãã³{}ãæ°åã®3ãã«ã³ãï¼,ï¼ã¨æ¥ã¦ããããã¯åã®åè§è±æ°ã¨ãã3åç¶ãã£ã¦æå³ã§ããã
ã¦ã¼ã¶ã¼ãã¼ã ã§ã¨ããæå°ã®åæ°ã3ã£ã½ãã®ã§ã
ããã§ãæä¸ã«@[ã¹ã¯ãªã¼ã³ãã¼ã ]ã®ããããå¦ãå¤å®ã§ãã¾ããã
ã¡ãªã¿ã«RTãèãã¦ããã®ã§ãããRTã«ã¯ãããããã®@[ã¹ã¯ãªã¼ã³ãã¼ã ]ãã¤ãã¦ãã¾ããããRTã®ã¯ããããè¨è¿°ããªãã¦ã大ä¸å¤«ã§ãããã
次ã«ãpreg_matchã§ãã
if( preg_match ( "/ããã|å¯ã/u", $var )){
ããã§åã®eregã¨éãã®ã¯ã¨ã¹ã±ã¼ã/ã®åå¨ã¨ãæ«å°¾ã®ã¢ã«ãã¡ãããuãåºåãã®|ã
ã¨ã¹ã±ã¼ãã¯åãæ¸ããã®ã§ãä»åã¯ãã®å¾ã«ããuã
ãã¡ãã®è¨è¿°ã詳ããã§ãã
http://ifs.seesaa.net/article/26300967.html
pregç³»ã®æ£è¦è¡¨ç¾ã«ãã¿ã¼ã³ä¿®é£¾åãuããæå®ããã¨ããã¿ã¼ã³æååãUTF-8ã§ã¨ã³ã³ã¼ããããæååã¨ãã¦æ±ãããããã«ãªãã¾ã
twitterã®æåã³ã¼ãã¯utf-8ããã®ã¹ã¯ãªãããå½ç¶utf-8ã§æ¸ããã¦ããã®ã§ããããã£ã¦ãã¯ããªã®ã«ãããããªãç¶æ
ãé·ãç¶ãã¦ã
ãªãã§ããªï¼ã£ã¦çåããã£ã¨ãã£ããã§ããï¼ããã§è¦æã ã£ãã£ã¦ã®ãããããï¼
ãã®ããæ¹ã§æ°·è§£ãã¾ããã
æå¾ã®åºåã|ã§ãããåã®ããæ¹ãªããããã¨æãã¾ããorã¿ãããªãã®ã§ããã
ããããå¯ããã©ã¡ããæ¥ã¦ããããããã¨ãããã¨ã§ããã¡ãªã¿ã«ã«ãã³ã¯ã¤ãã¦ãã¤ããªãã¦ãåä½ä¸ç·ã§ããã
éãããã¾ãã¡ããããªããã§ãããä½µç¨ããæã¨ãã ãã§ããããã¹ããã»ã³ã詳ããæ¹ææãã¦ãã ããã¨å¬ããâ¦ï¼äººä»»ãï¼
ä»ã¾ã§ã®è¨èã«åå¿ããç³»ã®ã¹ã¯ãªããããæ¸ãæãããèªåãªãã«æ¸ãã¦ã¿ãã®ãä¸èã ã¨æãã¾ããæ¯éãããã¦ãã ããã
æ£è¦è¡¨ç¾ã¯æ¬å½ã«å¥¥ãæ·±ãã§ãã使ãããªãã°botã¯ç²¾å·§ã«ãªãã§ããããããã以å¤ã«ãå¹çããããªãã§ããããã
ç§ãåå¼·ãã¦ããã¾ãï¼