Location: PHPKode > scripts > CSFD Parser > csfd.class.php
<?
/*
* CSFD Parser
* @author  Zemistr, http://www.zemistr.eu
* @version 1.5
* @copyright 2010-2011, Zemistr
*/

@header("Content-Type: text/html; charset=UTF-8");

class CSFD {
	function LoadData($url, $allow_info = false){
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL, $url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0);
		curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
		curl_setopt($ch, CURLOPT_USERAGENT, 'CSFD Parser');
        curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
		$data = curl_exec($ch);
		$info = curl_getinfo($ch);
		curl_close($ch);
		
		if($info['redirect_count'] > 1){
			$data = $this->LoadData($info['url'].'?text=1');
		}
		
		return $allow_info ? array($info, $data) : $data;
	}
	
	function Show($url, $allow_images = false, $allow_trailers = false, $allow_comments = false) {
		if(is_numeric($url)){
			$url = 'http://www.csfd.cz/film/'. (int) $url;
		}else{
			preg_match('/film\/([0-9]+)/', $url, $id);
			$url = 'http://www.csfd.cz/film/'.$id[1];
		}
	
		$data = $this->LoadData($url.'?text=1', true);
		$info = $data[0];
		$data = $data[1];
		
		preg_match('/location\.replace\(\'\/film\/([^\']+)\'\)/i', $data, $redirect);

		if($redirect[1] != ''){
			$data = $this->LoadData('http://www.csfd.cz/film/'.$redirect[1].'/?text=1', true);
			$info = $data[0];
			$data = $data[1];
		}
		
		preg_match('/(http:\/\/www\.csfd\.cz\/film\/([0-9]+)-([^\/]+)\/)/', $info['url'], $id);
		preg_match_all('/background="(http:\/\/img\.csfd\.cz\/posters\/[0-9]+\/[^"]+)"/i', $this->LoadData('http://www.csfd.cz/plakaty.php?id='.$id[2]), $posters);
		preg_match('/f;display:inline">\n         (.*)<\/h1>/isU', $data, $name);
		preg_match_all('/<table cellpadding=\'0\' cellspacing=\'0\' border=\'0\' style=\'color:#000000;font-weight:bold;font-size:12px\'><tr><td width=\'30\'><img src=\'(.*)\' vspace=\'2\' ><\/td><td>(.*)<\/td><\/tr><\/table>/isU', $data, $names);
		$other_names = Array();
		for($num = 0; $num < count($names[0]); $num++){
			$other_names[$num] = Array('image' => $names[1][$num], 'name' => strip_tags($names[2][$num]));
		}

		preg_match('/(imdb\.com\/title\/tt([0-9]*))/is', $data, $imdb);
		preg_match('/<td style=\'padding:10px;text-align:center;font-weight:bold;font-size:36px;color:white;background-color:#.{6}\'>\s+(.*)%\s+<\/td>/isU', $data, $rating);
		preg_match('/hodnocení<br>\(([0-9]*)\)<\/a>/isU', $data, $rating_count);
		preg_match('/<br><b>([^<]*)&nbsp;<br>([^<]*)<\/b>/isU', $data, $movie_info);
		$state_year = explode(', ', $movie_info[2]);
		preg_match('/ie:<\/b> (.*)<br>/isU', $data, $direction);
		preg_match('/<br><b>Hrají: <\/b>(.*)          <\/div>/isU', $data, $actors);
		
		if($allow_comments){
			preg_match_all('/<span class=\'nick\'>(.*)<\/span>\n  <p class=\'komentar_font\'>(.*)<\/p>/isU', $data, $comments);
			$user_comments = Array();
			for($num = 0; $num < count($comments[0]); $num++){
				preg_match('/<span class=\'nick\'>([^<]*) (.*)\n  <p class=\'komentar_font\'>(.*)<\/p>/sim', $comments[0][$num], $out);
				
				$user_comments[$num] = Array('user' => $out[1], 'rating' => (substr_count($out[2] ,'alt="*"') != 0 ? (substr_count($out[2] ,'alt="*"')) : (strpos($out[2], 'odpad!') !== false ? '-1' : 0)), 'comment' => $out[3]);
			}
		}

		preg_match('/<div style=\'float:left;width:425px;padding-top:10px;font-weight:normal\'>(.*)<\/div>/isU', $data, $description);
		if($allow_images){
			$images = Array();
			$image_page_data = $this->LoadData($url.'/galerie/?page=1');
			$find_select = preg_match('/<select[^>]+>(.+)<\/select></i', $image_page_data, $image_page_count);
			preg_match_all('/<option value="([0-9]+)"(\sselected)?>[0-9\-]+<\/option>/i', $image_page_count[1], $image_pages);
			
			preg_match_all('/background="(http:\/\/img\.csfd\.cz\/photos\/filmy\/[0-9]+\/[^"]+)"/i', $image_page_data, $image_data_first_page);
			foreach($image_data_first_page[1] as $n => $value){
				array_push($images, $value);
			}
			
			for($num = 1; $num < count($image_pages[1]); $num++){
				preg_match_all('/background="(http:\/\/img\.csfd\.cz\/photos\/filmy\/[0-9]+\/[^"]+)"/i', $this->LoadData($url.'/galerie/?page='.$image_pages[1][$num]), $image_data);
				foreach($image_data[1] as $n => $value){
					array_push($images, $value);
				}
			}
		}
		if($allow_trailers){
			$videos = Array();
			preg_match_all('/<option value="([0-9]+)">([^<]+)<\/option>/i', $this->LoadData($url.'/videa/?trailer_select='), $video_pages);
			
			for($num = 0; $num < count($video_pages[1]); $num++){
				$video_page_data = $this->LoadData($url.'/videa/?trailer_select='.$video_pages[1][$num]);
				preg_match('/vp\.addFile\(\'(http:\/\/img\.csfd\.cz\/[^\']+)\'\)/i', $video_page_data, $video_url);
				preg_match('/\.setSize\(([0-9]+), ([0-9]+)\);/i', $video_page_data, $video_data);
				array_push($videos, array(
					'id' => $video_pages[1][$num],
					'name' => $video_pages[2][$num],
					'url' => $video_url[1],
					'width' => $video_data[1],
					'height' => $video_data[2]
				));
			}
		}
		if($id[2] != ''){
			$return = Array();
			$return['id'] = $id[2];
			$return['url'] = $id[1];
			$return['imdb'] = $imdb[1] != '' ? 'http://www.'.$imdb[1].'/' : '';
			$return['name'] = $name[1];
			$return['names'] = $other_names;
			$return['posters'] = $posters[1];
			$return['rating'] = trim($rating[1]);
			$return['rating_count'] = $rating_count[1];
			$return['genre'] = explode(' / ', $movie_info[1]);
			$return['state'] = explode(' / ', $state_year[0]);
			$return['year'] = $state_year[1];
			$return['length'] = $state_year[2];
			$return['direction'] = explode(', ', str_replace('&nbsp;', ' ', strip_tags($direction[1])));
			$return['actors'] = explode(', ', str_replace('&nbsp;', ' ', strip_tags($actors[1])));
			$return['description'] = strip_tags($description[1]);
			$return['images'] = $images;
			$return['videos'] = $videos;
			$return['comments'] = $user_comments;
		}
		return $return;
	}
	function Find($text){
		$data = $this->LoadData('http://www.csfd.cz/hledani-filmu-hercu-reziseru-ve-filmove-databazi/?search='.urlencode($text));
		preg_match_all('/<td class=\'nadpis_black\' bgcolor=\'#DDDDDD\'>(.*)<\/td>/isU', $data, $lang);
		preg_match_all('/<table cellpadding="0" cellspacing="0" border="0" align="center" width="100%" style=\'font-size:12px\'>(.*)<\/table>/isU', $data, $result);
		
		$results = Array();
		for($num = 0; $num < count($lang[0]); $num++){
			preg_match_all('/(<a href="\/film\/(.*)\/" style="font-size:12px">(.*)<\/a>(\s\(([^<]+)\))?)<br>/isU', $result[1][$num], $movies_list);
			
			$movies = Array();
			for($num_m = 0; $num_m < count($movies_list[0]); $num_m++){
				$year_and_info = explode(') (', $movies_list[5][$num_m]);
				if(is_Numeric($year_and_info[0])){
					$year = $year_and_info[0];
					$info = $year_and_info[1];
				}else{
					$year = '';
					$info = $year_and_info[0];
				}
				$movies[$num_m] = Array(
					'url' => $movies_list[2][$num_m],
					'name' => $movies_list[3][$num_m],
					'year' => $year,
					'info' => $info,
					'data' => $movies_list[1][$num_m]
				);
			}
			$results[$num] = Array('where' => ucfirst($lang[1][$num]), 'movies' => $movies);
		}
		
		return $results;
	}
}
?>			'data' => $movies_list[1][$num_m]
				);
			}
			$results[$num] = Array('where' => ucfirst($lang[1][$num]), 'movies' => $movies);
		}
		
		return $results;
	}
}
?>
Return current item: CSFD Parser