靠機器人救 Karma!由於 Karma 值的不穩定,乾脆實做一隻 Plurk 機器人好了,這次實做參考 [PHP] Official Plurk API 之PHP - cURL 使用教學 。
Update @ 2011/07/26:新增縮網址的程式碼,因為新聞連結長度超過 144 字數的限制。
整體上就是實做一個撈 Yahoo 新聞的小程式,理論上是要用 RSS 才對,但因為我只想看焦點新聞,所以就改撈他的首頁進行處理,可以自行更新 getNews 函數吧!而這隻程式可以搭配 Windows 的排程或是 Unix 的 Crontab 運作,而程式環境上還要求 PHP-Curl 的支援囉!由於它非常陽春,所以我連帳密錯誤都沒確認就是了 :P
<?php
$cookie_db = '/tmp/http_cookie';
$log_db = '/tmp/job_log';
$hash_db = '/tmp/http_yahoo';
$bin_grep = '/bin/grep';
$plurk_api_key = 'YOUR_PLURK_API_KEY';
$plurk_id = 'LOGIN_ID';
$plurk_passwd = 'LOGIN_PASSWORD';
if( file_exists( $log_db ) )
return;
$result = do_act(
'http://www.plurk.com/API/Users/login' ,
'POST' ,
array(
'api_key' => $plurk_api_key ,
'username' => $plurk_id ,
'password' => $plurk_passwd
) ,
$cookie_db
);
$source = getNews();
foreach( $source as $data )
{
if( !check_exists( $data['url'] ) )
{
$target_url = NULL;
if( strlen( $data['url'] ) < 100 )
$target_url = $data['url'];
else
$target_url = getTinyurl( $data['url'] );
if( !empty( $target_url ) )
{
$plurk = '[News]'.$target_url.' ('.$data['title'].')';
$result = do_act(
'http://www.plurk.com/API/Timeline/plurkAdd' ,
'POST' ,
array(
'api_key' => $plurk_api_key ,
'qualifier' => 'shares' , // loves , likes , shares , ...
'content' => $plurk
) ,
$cookie_db
);
}
put_db( $data['url'] );
}
}
exit;
function check_exists( $url )
{
global $hash_db , $bin_grep;
if( !file_exists( $hash_db ) )
return false;
$cmd = $bin_grep.' -c "'. md5($url) .'" '.$hash_db;
$result = shell_exec( $cmd );
$result = trim( $result );
//echo "[$cmd]\n[$result]\n";
return !empty( $result );
}
function put_db( $url )
{
global $hash_db , $log_db ;
if( !file_exists( $hash_db ) )
$fp = fopen( $hash_db , 'w' );
else
$fp = fopen( $hash_db , 'a' );
if( $fp )
{
fwrite( $fp , md5($url) . "\n" );
fclose( $fp );
}
else
file_put_contents( $log_db , "Error" );
}
function getNews()
{
global $log_db;
$out = array();
$raw = file_get_contents( 'http://tw.yahoo.com/' );
$pattern = '<label>';
$raw = stristr( $raw , $pattern );
if( empty( $raw ) )
{
file_put_contents( $log_db , "Parser Error 1" );
return $out;
}
$raw = substr( $raw , strlen( $pattern ) );
$pattern = '<ol>';
$finish = strpos( $raw , $pattern );
if( $finish < 0 )
{
file_put_contents( $log_db , "Parser Error 2" );
return $out;
}
$raw = substr( $raw , 0 , $finish );
if( empty( $raw ) )
return $out;
$pattern = '{<h3[^>]*>[^<]*<a href="(.*?)"[^>]*>(.*?)</a></h3>}is';
if( preg_match_all( $pattern , $raw , $matches ) )
{
for( $i=0 , $cnt=count( $matches[1] ) ; $i<$cnt ; ++$i )
{
array_push( $out , array(
'url' => strstr( $matches[1][$i] , 'http:' ) ,
'title' => $matches[2][$i] )
);
}
}
else
file_put_contents( $log_db , "Parser Error 3" );
return $out;
}
function do_act( $target_url , $type , $data , $cookie_file = NULL )
{
$ch = curl_init();
if( $type == 'GET' ) // GET
{
$target_url .= http_build_query( $data );
curl_setopt($ch, CURLOPT_URL, $target_url );
}
else // POST
{
curl_setopt( $ch , CURLOPT_URL , $target_url );
curl_setopt( $ch , CURLOPT_POST , true );
curl_setopt( $ch , CURLOPT_POSTFIELDS , http_build_query( $data ) );
}
if( isset( $cookie_file ) ) // cookie
{
curl_setopt( $ch , CURLOPT_COOKIEFILE , $cookie_file );
curl_setopt( $ch , CURLOPT_COOKIEJAR , $cookie_file );
}
curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true );
//curl_setopt( $ch , CURLOPT_FOLLOWLOCATION , true );
//curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , false );
$result = curl_exec( $ch );
curl_close( $ch );
return $result;
}
function getTinyurl( $url )
{
$new_url = @file_get_contents( 'http://tinyurl.com/api-create.php?url='.urlencode($url) );
$new_url = trim( $new_url );
if( !empty( $new_url ) )
return $new_url;
return NULL;
}
?>
所以,如果我是用java寫出一個和plurk溝通的程式
我要如何使他持續運作呢??
我是使用Windows 作業系統,可以請你告訴我,要如何使用"排程工作"來自動執行嗎?
我對作業系統的部分不熟悉,所以,可能要請你從基本的地方來解說,感謝你了!!
hi
請問如果是要擷取網站RSS的資料應該怎麼修改這份php?