Skip to content

Commit

Permalink
Keyword limiting and Nico live(official) support
Browse files Browse the repository at this point in the history
* temporary streamer support (add column to streamer_table)
* official nico live streaming support as temporary, and limit by keywords
* tweet notification now limit by keywords
* give higher priority to programs that matches keywords at index page
  • Loading branch information
kazy111 committed Mar 18, 2012
1 parent 3bf0603 commit 450560e
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 31 deletions.
159 changes: 134 additions & 25 deletions check.php
Expand Up @@ -24,11 +24,17 @@ function start_tweet($data, $topic_flag = TRUE)
$hash = $GLOBALS['tweet_footer'];
$time = date(' [H:i]');
if($topic_flag){
$topic = preg_replace('/(https?|ftp)(:\/\/[[:alnum:]\+\$\;\?\.%,!#~*\/:@&=_-]+)/i', '$' , '/'.$data['topic']);
$topic = preg_replace('/(https?|ftp)(:\/\/[[:alnum:]\+\$\;\?\.%,!#~*\/:@&=_-]+)/i', '$' , '/'
.(trim($data['topic'])!='' ? trim($data['topic']) : trim($data['title'])));
}else{
$topic = '';
}

if($GLOBALS['limit_keywords'] != '' && preg_match('/'.$GLOBALS['limit_keywords'].'/', $topic) == 0 ) {
// when set limit keywords, tweet only keyword including
return;
}

$url = ' '.$site_url.'/view.php?id='.$data['sid'];

$cutlen = 140-strlen($hash)-mb_strlen($str, 'UTF-8')-strlen($url)-strlen($time);
Expand All @@ -47,11 +53,19 @@ function end_tweet($data, $topic_flag = TRUE)
$hash = $GLOBALS['tweet_footer'];
$time = date(' [H:i]');
if($topic_flag){
$topic = preg_replace('/(https?|ftp)(:\/\/[[:alnum:]\+\$\;\?\.%,!#~*\/:@&=_-]+)/i', '$' , '/'.$data['topic']);
$topic = preg_replace('/(https?|ftp)(:\/\/[[:alnum:]\+\$\;\?\.%,!#~*\/:@&=_-]+)/i', '$' , '/'
.(trim($data['topic'])!='' ? trim($data['topic']) : trim($data['title'])));
}else{
$topic = '';
}


if($GLOBALS['limit_keywords'] != '' && preg_match('/'.$GLOBALS['limit_keywords'].'/', $topic) == 0 ) {
// when set limit keywords, tweet only keyword including
return;
}


$cutlen = 140-strlen($hash)-mb_strlen($str, 'UTF-8')-strlen($time);
if(mb_strlen($topic, 'UTF-8') > $cutlen)
$topic = mb_substr($topic, 0, $cutlen-1, 'UTF-8').'…';
Expand Down Expand Up @@ -91,6 +105,7 @@ function check()
check_ustream();
check_justin();
check_stickam();
check_nicolive_official();
check_nicolive();
check_twitcasting();
check_own3d();
Expand Down Expand Up @@ -419,7 +434,7 @@ function set_opt( $ch, $cookie_file ){



global $manager, $chs, $sid_chs, $file_path, $nico_loginid, $nico_loginpw;
global $manager, $chs, $sid_chs, $file_path, $nico_loginid, $nico_loginpw, $site_url;

$ids = array();
$hash = array();
Expand Down Expand Up @@ -488,36 +503,80 @@ function set_opt( $ch, $cookie_file ){

$pid = $hash[$name];


// get community image
$api_url = 'http://com.nicovideo.jp/community/getplayerstatus/' . $name; // apiのURL
$ch = curl_seting( $api_url );

curl_setopt( $ch, CURLOPT_COOKIEFILE, $cookie_file );
$ch = set_opt( $ch, $cookie_file );

$ret = curl_exec( $ch );
curl_close($ch);

log_print($api_url);
try{
preg_match('/http:\/\/icon\.nimg\.jp\/community\/co[0-9]+\.jpg\?[0-9]+/', $ret, $matches);
$thumb = $matches[0];
}catch(Exception $e){
print $e->getMessage();
continue;
// judge live status
if(isset($xml->error)){ // error
// if api error, then retry
if( $xml->error->code == 'server_error' || $xml->error->code == 'notlogin'
|| $xml->error->code == 'full' || $xml->error->code == 'unknown' ){
continue;
}

// retry 3 time
if(intval($chs[$hash[$name]]['offline_count']) > 1){
}else{
$manager->increment_offline_count($hash[$name]);
continue;
}
}elseif( $xml->stream->archive == 1){
// offline
}else{
$live_st = TRUE;
$title = $xml->stream->title . ' / ' . $xml->stream->description;
$chs[$pid]['title'] = $title;
}


// cache image
$cachefile = $file_path.'/classes/compiled/'.$name.'.jpg';

// if not exists or 1day older
if( !file_exists($cachefile) || ( $live_st && filectime($cachefile) + 86400 < time() ) ){

log_print('get community image...');

// judge live status
if(isset($xml->error) || $xml->stream->archive == 1){ // error
}else{
$live_st = TRUE;
$title = $xml->stream->title . ' / ' . $xml->stream->description;
$thumb = 'http://icon.nimg.jp/community/'.$name.'.jpg?'.$xml->stream->base_time;
// get community image
$api_url = 'http://com.nicovideo.jp/community/' . $name; // apiのURL
$ch = curl_seting( $api_url );

curl_setopt( $ch, CURLOPT_COOKIEFILE, $cookie_file );
$ch = set_opt( $ch, $cookie_file );

$ret = curl_exec( $ch );
curl_close($ch);

log_print($api_url);
try{
if( preg_match('/http:\/\/icon\.nimg\.jp\/community\/(.+\/)?(?P<cm>co[0-9]+\.jpg)\?[0-9]+/', $ret, $matches) ){
$thumb = $matches[0];


print_r($matches);
$api_url = $matches[0];
$ch = curl_seting( $api_url );
curl_setopt( $ch, CURLOPT_COOKIEFILE, $cookie_file );
$ch = set_opt( $ch, $cookie_file );
$ret = curl_exec( $ch );
curl_close($ch);
log_print($api_url);

if(strpos($ret, '<?xml') === FALSE && strlen($ret) > 0){
$fp = fopen($cachefile, "w");
fwrite($fp, $ret);
fclose($fp);
}
}
}catch(Exception $e){
print $e->getMessage();
continue;
}
}
$thumb = $site_url.'/classes/compiled/'.$name.'.jpg';





// save status change
$change_flag = ($chs[$pid]['live']=='t' || $chs[$pid]['live']=='1') ^ $live_st;
Expand Down Expand Up @@ -546,6 +605,56 @@ function set_opt( $ch, $cookie_file ){
}



// check nicolive(official) function
// ** register programs to temporary
function check_nicolive_official()
{

global $manager;

// when set no limit_keywords, return this function
if ($GLOBALS['limit_keywords'] == '') {
return;
}

log_print('start checking nicolive(official).');


$url = 'http://live.nicovideo.jp/rss';

log_print($url);
try{
$xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
if (! $xml ) throw new Exception('URL open failed : '.$url);

// clear temporary
$manager->clear_temporary();

// add to temporary
$items = $xml->channel->item;
if ($items) {
for ($i = 0; $i < count($items); $i++) {
//set online
$check = $items[$i]->title;
$desc = addslashes(strip_tags($items[$i]->description));
if (preg_match('/'.$GLOBALS['limit_keywords'].'/', $check) > 0){
log_print('register to temporary '.$items[$i]->title);
$manager->register($items[$i]->title, $desc,
2, '', 3, $items[$i]->guid, NULL, 1);
}
}
}
}catch(Exception $e){
print $e->getMessage();
continue;
}

log_print('finish checking nicolive(official).');
}



function check_twitcasting()
{

Expand Down
3 changes: 3 additions & 0 deletions classes/database/IDataManager.php
Expand Up @@ -42,6 +42,9 @@ function set_tag($data);
function initialize_db();
function delete_db();
function register_onece($name, $room, $chat_type, $ust_id, $jus_id, $ust_no, $desc);
function register($name, $desc, $chat_type, $room, $pg_type, $pg_id, $opt_no, $temporary = 0);
function clear_temporary();
function truncate_increment_counter();
function query($sql);
}

Expand Down
69 changes: 66 additions & 3 deletions classes/database/MySQLDataManager.php
Expand Up @@ -70,7 +70,7 @@ function update_program($pid, $live, $viewer, $change_flag, $thumb, $title=null)
}
$sql = 'update program_table set live = '.$sql_live.' , viewer = '.$viewer
. $sql_time.', thumbnail = \''. $thumb .'\', offline_count = 0 '
. ($title != null ? ' ,title = \''. $title .'\' ' : '')
. ($title != null ? ' ,title = \''. addslashes($title) .'\' ' : '')
. ' where id = '.$pid;
print($sql);
$this->db->query($sql);
Expand Down Expand Up @@ -354,6 +354,7 @@ function initialize_db() {
.'wiki INT,'
.'tag TEXT DEFAULT \'\','
.'enable SMALLINT DEFAULT 1,'
.'temporary SMALLINT DEFAULT 0,'
.'PRIMARY KEY (id))');

$this->try_query('CREATE TABLE program_table ('
Expand Down Expand Up @@ -415,8 +416,8 @@ function register_onece($name, $room, $chat_type, $ust_id, $jus_id, $ust_no, $de
$this->db->begin();

try{
$this->db->query('insert into streamer_table (name, description) values '
.'(\''.$name.'\', \''.$desc.'\')');
$this->db->query('insert into streamer_table (name, description, twitter, url, wiki, enable) values '
.'(\''.$name.'\', \''.$desc.'\', NULL, NULL, NULL, 1)');
$sid = mysql_insert_id();// MySQL

$tmp = $this->db->query_ex('select id from chat_table where type='
Expand All @@ -439,10 +440,72 @@ function register_onece($name, $room, $chat_type, $ust_id, $jus_id, $ust_no, $de

$this->db->commit();
}catch(Exception $e){
print $e->getMessage();
$this->db->rollback();
}

}


function register($name, $desc, $chat_type, $room, $pg_type, $pg_id, $opt_no, $temporary = 0){
$this->db->begin();

try{
$this->db->query('insert into streamer_table (name, description, twitter, url, wiki, enable, temporary) values '
.'(\''.$name.'\', \''.$desc.'\', NULL, NULL, NULL, 1, '.$temporary.')');
$sid = mysql_insert_id();// MySQL

$tmp = $this->db->query_ex('select id from chat_table where type='
.$chat_type.' and room=\''.$room.'\'');
if(is_null($tmp) || !is_numeric($tmp['id'])){
$this->db->query('insert into chat_table (room, type) values '
.'(\''.$room.'\', '.$chat_type.')');
$cid = mysql_insert_id();// MySQL
}else
$cid = $tmp['id'];

$this->db->query('insert into program_table (streamer_id, chat_id, type, ch_name, optional_id)'
.' values ('.$sid.', '.$cid.', '.$pg_type.', \''.$pg_id.'\',\''.$opt_no.'\')');

$this->db->commit();
}catch(Exception $e){
print $e->getMessage();
$this->db->rollback();
}

}

function clear_temporary() {
try{
$sql = 'select id from streamer_table where temporary = 1';
$result = $this->db->query($sql);

while($arr = $this->db->fetch($result)){
$this->delete_streamer($arr['id']);
}
$this->truncate_increment_counter();
$this->db->commit();
}catch(Exception $e){
print $e->getMessage();
$this->db->rollback();
}

}

function truncate_increment_counter() {

function _truncate($db, $table_name, $column) {
$result = $db->query_ex('select MAX('.$column.') as id from '.$table_name);
if(!is_null($result) && is_numeric($result['id'])){
$next_id = $result['id'] + 1;
$db->query('alter table '.$table_name.' auto_increment = '.$next_id);
}
}
_truncate($this->db, 'streamer_table', 'id');
_truncate($this->db, 'program_table', 'id');
_truncate($this->db, 'chat_table', 'id');

}
}

?>

0 comments on commit 450560e

Please sign in to comment.