view storeProgram.inc.php @ 143:42f6eb738567

mod: ÀѤ߻Ĥ·¤ò½¤Àµ
author epgrec@park.mda.or.jp <yoneda@recorder.localnet.mda.or.jp>
date Sun, 21 Mar 2010 21:54:03 +0900
parents 481e789605e3
children fa1b0995992b
line wrap: on
line source

<?php

function garbageClean() {
	// ä¸è¦ãªãƒ—ログラムã®å‰Šé™¤
	// 8日以上å‰ã®ãƒ—ログラムを消ã™
	$arr = array();
	$arr = DBRecord::createRecords(  PROGRAM_TBL, "WHERE endtime < subdate( now(), 8 )" );
	foreach( $arr as $val ) $val->delete();
	
	// 8日以上先ã®ãƒ‡ãƒ¼ã‚¿ãŒã‚ã‚Œã°æ¶ˆã™
	$arr = array();
	$arr = DBRecord::createRecords(  PROGRAM_TBL, "WHERE starttime  > adddate( now(), 8 ) ");
	foreach( $arr as $val ) $val->delete();

	// 30日以上å‰ã®ãƒ­ã‚°ã‚’消ã™
	$arr = array();
	$arr = DBRecord::createRecords(  LOG_TBL, "WHERE endtime < subdate( now(), 30 )" );
	foreach( $arr as $val ) $val->delete();
}

function doKeywordReservation() {
  // キーワード自動録画予約
 	$arr = array();
	$arr = Keyword::createKeywords();
	foreach( $arr as $val ) {
		try {
			$val->reservation();
			reclog( "getepg::キーワードID".$val->id."ã®éŒ²ç”»ãŒäºˆç´„ã•ã‚ŒãŸ");
		}
		catch( Exception $e ) {
			// 無視
		}
	}
}

function storeProgram( $type, $xmlfile ) {
	global $BS_CHANNEL_MAP, $GR_CHANNEL_MAP, $CS_CHANNEL_MAP;
	
	// ãƒãƒ£ãƒ³ãƒãƒ«ãƒžãƒƒãƒ—ファイルã®æº–å‚™
	$map = array();
	if( $type == "BS" ) $map = $BS_CHANNEL_MAP;
	else if( $type == "GR") $map = $GR_CHANNEL_MAP;
	else if( $type == "CS") $map = $CS_CHANNEL_MAP;
	
	// XML parse
  	$xml = @simplexml_load_file( $xmlfile );
	if( $xml === false ) {
		reclog( "getepg:: 正常ãª".$xmlfile."ãŒä½œæˆã•ã‚Œãªã‹ã£ãŸæ¨¡æ§˜(放é€é–“帯ã§ãªã„ãªã‚‰å•é¡Œã‚ã‚Šã¾ã›ã‚“)" );
		return;	// XMLãŒèª­ã¿å–ã‚Œãªã„ãªã‚‰ä½•ã‚‚ã—ãªã„
	}
	// channel抽出
	foreach( $xml->channel as $ch ) {
		$disc = $ch['id'];
	 try {
		// ãƒãƒ£ãƒ³ãƒãƒ«ãƒ‡ãƒ¼ã‚¿ã‚’探ã™
		$num = DBRecord::countRecords( CHANNEL_TBL , "WHERE channel_disc = '" . $disc ."'" );
		if( $num == 0 ) {
			if( array_key_exists( "$disc", $map ) ) {
				// ãƒãƒ£ãƒ³ãƒãƒ«ãƒ‡ãƒ¼ã‚¿ãŒãªã„ãªã‚‰æ–°è¦ä½œæˆ
				$rec = new DBRecord( CHANNEL_TBL );
				$rec->type = $type;
				$rec->channel = $map["$disc"];
				$rec->channel_disc = $disc;
				$rec->name = $ch->{'display-name'};
			}
		}
		else {
			// 存在ã—ãŸå ´åˆã‚‚ã€ã¨ã‚Šã‚ãˆãšãƒãƒ£ãƒ³ãƒãƒ«åã¯æ›´æ–°ã™ã‚‹
			$rec = new DBRecord(CHANNEL_TBL, "channel_disc", $disc );
			$rec->name = $ch->{'display-name'};
		}
	 }
	 catch( Exception $e ) {
		reclog( "getepg::DBã®æŽ¥ç¶šã¾ãŸã¯ãƒãƒ£ãƒ³ãƒãƒ«ãƒ†ãƒ¼ãƒ–ルã®æ›¸ãè¾¼ã¿ã«å¤±æ•—", E_ERROR );
	 }
	}
	// channel 終了
	
	// programme å–å¾—
	
	foreach( $xml->programme as $program ) {
		$channel_rec = null;
		
		$channel_disc = $program['channel']; 
		if( ! array_key_exists( "$channel_disc", $map ) ) continue;
		$channel = $map["$channel_disc"];
		
		try {
			$channel_rec = new DBRecord(CHANNEL_TBL, "channel_disc", "$channel_disc" );
		}
		catch( Exception $e ) {
			reclog( "getepg::ãƒãƒ£ãƒ³ãƒãƒ«ãƒ¬ã‚³ãƒ¼ãƒ‰ $channel_disc ãŒç™ºè¦‹ã§ããªã„", E_ERROR );
		}
		if( $channel_rec == null ) continue;	// ã‚ã‚Šå¾—ãªã„ã“ã¨ãŒèµ·ããŸ
		if( $channel_rec->skip == 1 ) continue;	// å—ä¿¡ã—ãªã„ãƒãƒ£ãƒ³ãƒãƒ«
		
		$starttime = str_replace(" +0900", '', $program['start'] );
		$endtime = str_replace( " +0900", '', $program['stop'] );
		$title = $program->title;
		$desc = $program->desc;
		$cat_ja = "";
		$cat_en = "";
		foreach( $program->category as $cat ) {
			if( $cat['lang'] == "ja_JP" ) $cat_ja = $cat;
			if( $cat['lang'] == "en" ) $cat_en = $cat;
		}
		$program_disc = md5( $channel_disc . $starttime . $endtime );
		// printf( "%s %s %s %s %s %s %s \n", $program_disc, $channel, $starttime, $endtime, $title, $desc, $cat_ja );
		
		// カテゴリ登録
		
		$cat_rec = null;
		try {
			// カテゴリを処ç†ã™ã‚‹
			$category_disc = md5( $cat_ja . $cat_en );
			$num = DBRecord::countRecords(CATEGORY_TBL, "WHERE category_disc = '".$category_disc."'" );
			if( $num == 0 ) {
				// æ–°è¦ã‚«ãƒ†ã‚´ãƒªã®è¿½åŠ 
				$cat_rec = new DBRecord( CATEGORY_TBL );
				$cat_rec->name_jp = $cat_ja;
				$cat_rec->name_en = $cat_en;
				$cat_rec->category_disc = $category_disc;
				reclog("getepg:: æ–°è¦ã‚«ãƒ†ã‚´ãƒª".$cat_ja."を追加" );
			}
			else
				$cat_rec = new DBRecord(CATEGORY_TBL, "category_disc" , $category_disc );
		}
		catch( Exception $e ) {
			reclog("getepg:: カテゴリテーブルã®ã‚¢ã‚¯ã‚»ã‚¹ã«å¤±æ•—ã—ãŸæ¨¡æ§˜", E_ERROR );
			reclog("getepg:: ".$e->getMessage()."" ,E_ERROR );
			exit( $e->getMessage() );
		}
		
		// プログラム登録
		
		try {
			//
			$num = DBRecord::countRecords(PROGRAM_TBL, "WHERE program_disc = '".$program_disc."'" );
			if( $num == 0 ) {
				// æ–°è¦ç•ªçµ„
				// é‡è¤‡ãƒã‚§ãƒƒã‚¯ åŒæ™‚間帯ã«ã‚る番組
				$options = "WHERE channel_disc = '".$channel_disc."' ".
					"AND starttime < '". $endtime ."' AND endtime > '".$starttime."'";
				$battings = DBRecord::countRecords(PROGRAM_TBL, $options );
				if( $battings > 0 ) {
					// é‡è¤‡ç™ºç”Ÿï¼ãŠãらã放映時間ã®å¤‰æ›´
					$records = DBRecord::createRecords(PROGRAM_TBL, $options);
					foreach( $records as $rec ) {
						// 自動録画予約ã•ã‚ŒãŸç•ªçµ„ã¯æ”¾æ˜ æ™‚間変更ã¨åŒæ™‚ã«ã„ã£ãŸã‚“削除ã™ã‚‹
						try {
							$reserve = new DBRecord(RESERVE_TBL, "program_id", $rec->id );
							if( $reserve->autorec ) {
								reclog( "getepg::録画ID".$reserve->id.":".$reserve->type.$reserve->channel.$reserve->title."ã¯æ™‚間変更ã®å¯èƒ½æ€§ãŒã‚り予約å–り消ã—" );
								Reservation::cancel( $reserve->id );
							}
						}
						catch( Exception $e ) {
							// 無視
						}
						// 番組削除
						reclog( "getepg::放é€æ™‚é–“é‡è¤‡ãŒç™ºç”Ÿã—ãŸç•ªçµ„ID".$rec->id." ".$rec->type.$rec->channel.$rec->title."を削除" );
						$rec->delete();
					}
				}
				// //
				$rec = new DBRecord( PROGRAM_TBL );
				$rec->channel_disc = $channel_disc;
				$rec->channel_id = $channel_rec->id;
				$rec->type = $type;
				$rec->channel = $channel_rec->channel;
				$rec->title = $title;
				$rec->description = $desc;
				$rec->category_id = $cat_rec->id;
				$rec->starttime = $starttime;
				$rec->endtime = $endtime;
				$rec->program_disc = $program_disc;
			}
			else {
				// 番組内容更新
				$rec = new DBRecord( PROGRAM_TBL, "program_disc", $program_disc );
				$rec->title = $title;
				$rec->description = $desc;
				$rec->category_id = $cat_rec->id;
				
				try {
					$reserve = new DBRecord( RESERVE_TBL, "program_id", $rec->id );
					if( $reserve->dirty == 0 ) {
						$reserve->title = $title;
						$reserve->description = $desc;
						reclog( "getepg:: 予約ID".$reserve->id."ã®EPG情報ãŒæ›´æ–°ã•ã‚ŒãŸ" );
					}
				}
				catch( Exception $e ) {
					// 無視ã™ã‚‹
				}
			}
		}
		catch(Exception $e) {
			reclog( "getepg:: プログラムテーブルã«å•é¡ŒãŒç”Ÿã˜ãŸæ¨¡æ§˜", E_ERROR );
			reclog( "getepg:: ".$e->getMessage()."" , E_ERROR);
			exit( $e->getMessage() );
		}
	}
	// Programmeå–得完了
}
?>