Ufak bir script için yazarken ihtiyaç duyduğum Türkçe film bilgileri alan bir sınıf aradım, bulamayınca sinemalar.com’dan temel film bilgilerini alan bir sınıf yazdım. Özellikleri:
- İsim, orjinal isim, puan, oy sayısı,tür, yıl, ülke, oyuncular, film özeti, poster ve video bilgilerini alır.
- Film afişini sunucuya kaydeder, sunucu üzerinden gösterir.
- Bir defa çekilen bilgiyi önbelleğe alır, böylece sayfa yüklenme süresinin uzamasına sebep olmaz.
Ayrıca özet ve oyuncu bilgilerinin ne kadarının alınacağınıda ayarlamaya olanak verir.
[box type=”info”]Yazı güncelliğini kaybettiği için sınıf yenilenmedi. WordPress eklentisi haline getirdim. Burdan inceleyebilirsiniz.[/box]
Öncelikle söylemeliyim ki kodların yarı İngilizce yarı Türkçe olmasının sabebi önce imdb sınıfı yapmakla başlamamdı.Hali hazırda imdb içinde bir sınıfım mevcut.
Hemen kodlara geçelim; (php dosyası şeklinde yazı sonunda mevcut)
<?php /* * sinemala.com sınıfı * Yazar : Sezer Çoban * Web : www.sezercoban.com * E-mail : admin@sezercoban.com * Girilen sinemalar.com adresinden temel film bilgilerini alarır. * Posterleri sunucuya çeker. * Önbellekleme sistemi ile sayfa yüklenme süresini kısaltır. * WordPress eklentisi için lütfen iletişime geçin. */ class SINEMALARException extends Exception {} class sinemalar { // herhangi bir bilgi alınamadıysa dönülecek değer public $strNotFound = "N/A"; //curl işlemi için timeout const SINEMALAR_TIMEOUT = 15; // sıralıklar için ayrac public $strSeperator = ' / '; // IMDb url. private $_strUrl = NULL; const SINEMALAR_DEBUG = false; // IMDb posters directory. private $_bolPoster = false; // cURL cookie file. private $_fCookie = false; // sinemalar cache directory. private $_bolCache = true; // sinemalar cache. private $_strCache = 0; // IMDb movie id. private $_strId = "";// matc ile bulunacasfalse; // Movie found? public $isReady = false; // Define root of this script. private $_strRoot = '';//C:\xampp\htdocs\sinema //düzenli ifadeler const SINEMALAR_TITLE = '~og:title" content="(.*)"~Ui'; const SINEMALAR_TITLE_ORIG = '~class="film_adi_orjinal sol">(.*)</h2>~Ui'; const SINEMALAR_RATING = '~<div class="p30 turuncu bold">(.*)<span class="~Ui'; const SINEMALAR_VOTES = '~<div class="detay_span">(.*)kişi oy verdi</div>~Ui'; const SINEMALAR_GENRE = '~<span class="detay_span">(.*)</span>~Ui'; const SINEMALAR_RELEASE_DATE= '~<span class="detay_span">(.*)</span>~Ui'; const SINEMALAR_COUNTRY = '~<span class="detay_span">(.*)</span>~Ui'; const SINEMALAR_CAST = '~Filmleri">(.*)</a>~Ui'; const SINEMALAR_PLOT = '~<p class="c333">(.*)</p>~Ui'; const SINEMALAR_POSTER = '~og:image" content="(.*)"~Ui'; const SINEMALAR_URL = '~http://(.*\.|.*)sinemalar.com/(f|F)ilm(\?|/)(..\d+)~i'; const SINEMALAR_VIDEO = '~<embed src="http://www(.*)"~Ui'; public function __construct($strUrl, $intCache = 1440) { @header("content-type: text/html; charset=utf-8"); sinemalar::startTimer(); sinemalar::checkUrl($strUrl); //klasor yolları icin root klasoru if (!$this->_strRoot) { $this->_strRoot = dirname(__FILE__); } //poster kaydetme için kontroller if (is_writable($this->_strRoot . '/posters/') || mkdir($this->_strRoot . '/posters/')) { $this->_bolPoster = true; if (sinemalar::SINEMALAR_DEBUG) echo '<b>Poster sunucuya çekme kullanılabilir.</b> <br>'; } //cache yapabiliyormuyuz if (is_writable($this->_strRoot . '/cache/') || mkdir($this->_strRoot . '/cache/')) { $this->_bolCache = true; if (sinemalar::SINEMALAR_DEBUG) echo '<b>Cache kullanılabilir.</b> <br>'; } else { throw new SINEMALARException($this->_strRoot . '/cache/ is not writable!'); } // curl bileşeni yüklümü ? if (!function_exists('curl_init')) { throw new SINEMALARException('Uygulamanın çalışabilmesi için cUrl bileşenini aktifleştirmeniz gerekli.'); } // Debug only. if (sinemalar::SINEMALAR_DEBUG) { error_reporting(-1); ini_set('display_errors', 1); echo '<b>Çalıştırılıyor: </b> sinemalar::fetchUrl<br>'; } $this->_intCache = (int)$intCache; sinemalar::fetchUrl($strUrl); $this->_strUrl = $strUrl; } private function fetchUrl($strUrl) { // Check if there is a cache we can use. $fCache = $this->_strRoot . '/cache/' . md5($this->_strUrl) . '.cache'; if (file_exists($fCache)) { $bolUseCache = true; $intChanged = filemtime($fCache); $intNow = time(); $intDiff = round(abs($intNow - $intChanged) / 60); if ($intDiff > $this->_intCache) { $bolUseCache = false; if (sinemalar::SINEMALAR_DEBUG) echo '<b>Cache\'nin süresi dolmuş:</b>Tarih:'.$intChanged .', bitme süresi: '.$this->_intCache.'<br>'; } if (sinemalar::SINEMALAR_DEBUG) echo '<b>Cache yolu: </b>' . $this->_strRoot . '/cache/' . md5($this->_strUrl) . '.cache .<br>'; } else { $bolUseCache = false; } if ($bolUseCache) { $this->_strSource = file_get_contents($fCache); if (sinemalar::SINEMALAR_DEBUG) echo '<b> ' . $this->_strId . ' için cache kullanılıyor.</b><br>'; return true; } else { if (function_exists('sys_get_temp_dir')) { $this->_fCookie = tempnam(sys_get_temp_dir(), 'sinemalar'); if (sinemalar::SINEMALAR_DEBUG) echo '<b>Cookie yolu : </b> ' . $this->_fCookie. '<br>'; } $oCurl = curl_init($strUrl); if (sinemalar::SINEMALAR_DEBUG) echo '<b>cUrl Çalıştırılıyor. </b> <br>'; curl_setopt_array($oCurl, array ( CURLOPT_VERBOSE => FALSE, CURLOPT_HEADER => FALSE, CURLOPT_FRESH_CONNECT => TRUE, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_TIMEOUT => sinemalar::SINEMALAR_TIMEOUT, CURLOPT_CONNECTTIMEOUT => 0, CURLOPT_REFERER => 'http://www.google.com', CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)', CURLOPT_FOLLOWLOCATION => true )); $strOutput = curl_exec($oCurl); $arrInfo = curl_getinfo($oCurl); curl_close($oCurl); if ($arrInfo['http_code'] != 200 && $arrInfo['http_code'] != 302) { return $this->isReady = false; if (sinemalar::SINEMALAR_DEBUG) echo '<b>cUrl için hata oluştu : </b>'.$arrInfo['http_code'].' <br>'; return false; } $this->_strSource = $strOutput; file_put_contents($fCache, $this->_strSource); if (sinemalar::SINEMALAR_DEBUG) echo '<b>cUrl başarıyla çalıştırıldı. Cache atandı. </b>'.$fCache.'<br>'; //aldık atadık aga // eski cookie silinebilir if ($this->_fCookie) { unlink($this->_fCookie); if (sinemalar::SINEMALAR_DEBUG) echo '<b>Cookie dosyası silindi: </b>'.$this->_fCookie.' <br>'; } } } private function checkUrl($strUrl){ if ($strId = sinemalar::matchRegex($strUrl, sinemalar::SINEMALAR_URL, 4)) { $this->_strId = preg_replace('~[\D]~', '', $strId); $this->_strUrl = 'http://www.sinemalar.com/film/' . $this->_strId . '/'; $this->isReady = true; return false; } $this->isReady = false; } //içeriği eşleştirip array a atar private function matchRegex($strContent, $strRegex, $intIndex = null) { preg_match_all($strRegex, $strContent, $arrMatches); if ($arrMatches === FALSE) return false; if ($intIndex != null && is_int($intIndex)) { if ($arrMatches[$intIndex]) { return $arrMatches[$intIndex][0]; } return false; } return $arrMatches; } //içeriği kısaltır public function getShortText($strText, $intLength = 100) { $strText = trim($strText) . ' '; $strText = substr($strText, 0, $intLength); $strText = substr($strText, 0, strrpos($strText, ' ')); return $strText . '…'; } //resimleri sunucuya çeker private function saveImage($_strUrl) { $_strUrl = trim($_strUrl); if (preg_match('/afisYok/', $_strUrl) && file_exists('posters/not-found.jpg')) { return 'posters/not-found.jpg'; } $strFilename = $this->_strRoot . '/posters/' . $this->_strId . '.jpg'; if (file_exists($strFilename)) { return 'posters/' . $this->_strId . '.jpg'; } $oCurl = curl_init($_strUrl); curl_setopt_array($oCurl, array ( CURLOPT_VERBOSE => FALSE, CURLOPT_HEADER => FALSE, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_TIMEOUT => sinemalar::SINEMALAR_TIMEOUT, CURLOPT_CONNECTTIMEOUT => 0, CURLOPT_REFERER => $_strUrl, CURLOPT_BINARYTRANSFER => TRUE)); $sOutput = curl_exec($oCurl); $arrInfo = curl_getinfo($oCurl); curl_close($oCurl); if ($arrInfo['http_code'] != 200 && $arrInfo['http_code'] != 302) { return $_strUrl; } $oFile = fopen($strFilename, 'x'); fwrite($oFile, $sOutput); fclose($oFile); return 'posters/' . $this->_strId . '.jpg'; } //işlem süresini hesaplar private function startTimer() { global $startTime; $msure = microtime (); $msure = explode (' ', $msure ); $msure = $msure[1] + $msure[0]; $startTime = $msure; } //işlem süresini yazdırır public function executionTime() { global $startTime; $msure = microtime (); $msure = explode (' ', $msure); $msure = $msure[1] + $msure[0]; $bitis = $msure; $toplam = round (($bitis - $startTime), 5); return $toplam; } //sinema başlığını alır public function getTitle() { if ($this->isReady) { if ($strReturn = $this->matchRegex($this->_strSource, sinemalar::SINEMALAR_TITLE, 1)) { return trim($strReturn); } } return $this->strNotFound; } //sinemanın orjinal adını alır public function getOrigTitle() { if ($this->isReady) { if ($strReturn = $this->matchRegex($this->_strSource, sinemalar::SINEMALAR_TITLE_ORIG, 1)) { return trim($strReturn); } } return $this->strNotFound; } // sinemanın puanını alır public function getRating() { if ($this->isReady) { if ($strReturn = $this->matchRegex($this->_strSource, sinemalar::SINEMALAR_RATING, 1)) { return trim($strReturn); } } return $this->strNotFound; } // sinemanın toplam oy sayısını alır public function getVotes() { if ($this->isReady) { if ($strReturn = $this->matchRegex($this->_strSource, sinemalar::SINEMALAR_VOTES, 1)) { return trim($strReturn); } } return $this->strNotFound; } //filmin türünü alır public function getGenre() { if ($this->isReady) { $strExplode = explode('detay_table_head"><div>Tür:',$this->_strSource); $strExplode1 = explode('</tr>',$strExplode[1]); $strExplode2 = $strExplode1[0]; $arrReturned = $this->matchRegex($strExplode2, sinemalar::SINEMALAR_GENRE); if (count($arrReturned[1])) { foreach ($arrReturned[1] as $strName) { $arrReturn[] = trim($strName); } return implode($this->strSeperator, array_unique($arrReturn)); } } return $this->strNotFound; } // sinemanın yılını alır sayısını alır public function getYear() { if ($this->isReady) { if ($strReturn = $this->matchRegex($this->_strSource, sinemalar::SINEMALAR_RELEASE_DATE, 1)) { return trim($strReturn); } } return $this->strNotFound; } // sinemanın ülkesini alır public function getCountry() { if ($this->isReady) { $strExplode = explode('detay_table_head"><div>Yapım:',$this->_strSource); $strExplode1 = explode('</tr>',$strExplode[1]); $strExplode2 = $strExplode1[0]; $arrReturned = $this->matchRegex($strExplode2, sinemalar::SINEMALAR_COUNTRY); if (count($arrReturned[0])) { foreach ($arrReturned[0] as $strName) { if(!strrpos(trim($strName),"1") && !strrpos(trim($strName),"2")) { $arrReturn[] = trim($strName); } } if(count($arrReturn)) { return implode($this->strSeperator, array_unique($arrReturn)); } } } return $this->strNotFound; } // sinemanın oyuncularını alır alır alır public function getCast($intLimit = 20) { if ($this->isReady) { $strExplode = explode('detay_table_head"><div>Oyuncular:',$this->_strSource); $strExplode1 = explode('</tr>',$strExplode[1]); $strExplode2 = $strExplode1[0]; $arrReturned = $this->matchRegex($strExplode2, sinemalar::SINEMALAR_CAST); if (count($arrReturned[1])) { foreach ($arrReturned[1] as $i=>$strName) { if ($i >= $intLimit) { break; } $arrReturn[] = trim($strName); } return implode($this->strSeperator, array_unique($arrReturn)); } } return $this->strNotFound; } // sinemanın özetini alır public function getPlot($intLimit = 0) { if ($this->isReady) { if ($strReturn = $this->matchRegex(preg_replace('/\s/', ' ', $this->_strSource), sinemalar::SINEMALAR_PLOT, 1)) { if ($intLimit) { return $this->getShortText($strReturn, $intLimit); } return trim($strReturn); } } return $this->strNotFound; } //filmin posterini alır public function getPoster() { if ($this->isReady) { if ($strReturn = $this->matchRegex($this->_strSource, sinemalar::SINEMALAR_POSTER, 1)) { if ($strLocal = $this->saveImage($strReturn)) { return $strLocal; } return trim($strReturn); } } return $this->strNotFound; } // sinemalar url sini alır public function getUrl() { if ($this->isReady) { return trim($this->_strUrl); } return $this->strNotFound; } public function getVideo() { if ($this->isReady) { if ($strReturn = $this->matchRegex($this->_strSource, sinemalar::SINEMALAR_VIDEO, 1)) { return "http://www" .trim($strReturn); } } return $this->strNotFound; } } ?>
Fonksiyonlar gayet açık, geliştirmeye müsait.Kullanımı da şu şekilde :
$sinema = new sinemalar("http://www.sinemalar.com/film/134880/cehennem-melekleri-2") $baslik = $sinema->getTitle(); $ozet = $sinema->getPlot(150); $oyuncular = $sinema->getCast(5);
Özeti alrken verdiğiğimiz 150 değeri özetten alınacak karakter sayısını, oyuncularda verdiğimiz 5 ise oyuncuların ilk kaçının alınacağını belirtiyor.
Sınıfın sıkıştırılmış halini buradan indirebilirsiniz. GitHub Profilime de Burdan göz atabilirsiniz
WordPress eklentiside mevcut, sınırlı kullanım ile yakın zamanda paylaşacağım.
Herhangi bir soru/sorun ya da öneriniz olursa yorum yazmaktan çekinmeyin.
Eyvallah hocam büyük bir uğraştan kurtardın beni.
Çalıştırmadım ben sorun ne olabilir sizce hiç bir şey çıkmıyor
Sınıfı çağırdığınız kodları kısmını paylaşabilir misiniz? Sinemalar alt yapıda değişiklik yaptıysa eğer sınıf çalışmıyor olabilir. test edip geri dönüş yapacağım. iyi çalışmalar.
Bir kaçtane denedim kod ama olmadı.Olmazı gereken kodları yazarsınız birde öyle deneyeyim geri döndüğün içinde teşekkürler…
merhaba,
okul işlerinden çok yoğunum ilgilenemedim. Test ettim, sinemalar alt yapısını değiştirdiği için çalışmıyor. Fırsat buldugum ilk anda hemen duzenleyip tekrar paylaşacağım.
Şu adresteki sınıf sanırım bu.
https://github.com/FabianBeiner/PHP-IMDB-Grabber/blob/master/imdb.class.php
Hocam yokmu şunun çalışanı… varsa ne sevaba girersin varya beklemedeyim…