Skip to content

Instantly share code, notes, and snippets.

@dogancelik
Last active October 2, 2015 13:28
Show Gist options
  • Save dogancelik/2250256 to your computer and use it in GitHub Desktop.
Save dogancelik/2250256 to your computer and use it in GitHub Desktop.
(PHP) TheTVDB API Example
<?php
/*
* Written by: Doğan Çelik
* Description: Exports a list of episode names from a specified season from a specified series from TheTVDB
* A demo is available at http://dogancelik.com/dizi/
* How to use: Request this page bolum.php?d=NAME_OF_THE_SERIES&s=SEASON_NUMBER
*/
//error_reporting(E_ALL);
function diziNumarasi($dizi)
{
$xml = simplexml_load_file('http://www.thetvdb.com/api/GetSeries.php?seriesname='.urlencode($dizi)); //file_get_contents();
$xpath = $xml->xpath('/Data/Series/seriesid');
$id = $xpath[0];
if ($id>0) {return $id;} else {return false;}
}
function xmlOku($dosya,$sezon)
{
//dosya kontrol
if (file_exists($dosya))
{
$xml = simplexml_load_file($dosya);
$xpath = $xml->xpath('/Data/Episode[SeasonNumber='.$sezon.']/EpisodeName');
}
//xpath kontrol
if (isset($xpath) && $xpath != FALSE)
{
$sonuc = '';
foreach($xpath as $title)
$sonuc .= trim($title).PHP_EOL;
return $sonuc;
}
}
function xmlYaz($url,$dosya)
{
$xml = simplexml_load_file($url);
$handle = fopen($dosya,'w');
fwrite($handle,$xml->asXML());
fclose($handle);
}
function bolumAdi($dizi_adi,$sezon,$bolum = null)
{
$dizi_num = diziNumarasi($dizi_adi);
if ($dizi_num !== FALSE)
{
$urls = Array
(
"http://www.thetvdb.com/api/F0A9519B01D1C096/series/$dizi_num/default/$sezon/$bolum",
"http://www.thetvdb.com/api/F0A9519B01D1C096/series/$dizi_num/all/"
);
$dosya_adi = sprintf('xml/%s_%02d.xml',$dizi_num,$sezon);
//dosya kontrol yoksa yeni indir
if (!file_exists($dosya_adi))
xmlYaz($urls[1],$dosya_adi);
//eğer şimdiki zaman - 30 dk, dosyanın değiştirilme zamanından büyükse yeni bir tane indir
if (file_exists($dosya_adi) && time() - 30 * 60 > filemtime($dosya_adi))
echo xmlOku($dosya_adi,$sezon);
//eski dosyayı kullan
else
echo xmlOku($dosya_adi,$sezon);
increase();
}
}
function increase($dosya = 'count')
{
$sayi = intval(@file_get_contents($dosya)) + 1;
file_put_contents($dosya, $sayi);
}
bolumAdi($_GET['d'],$_GET['s']);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment