Skip to content

Commit

Permalink
Merge pull request #19 from gbirke/zip_download
Browse files Browse the repository at this point in the history
Add support for ZIP download
  • Loading branch information
Jérôme Poskin committed Nov 22, 2014
2 parents 86f6d25 + 74dd750 commit 278c9e7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
9 changes: 6 additions & 3 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
$serverTime = $tvdb->getServerTime();
// Search for a show
$data = $tvdb->getSeries('Walking Dead');

// Use the first show found and get the S01E01 episode
$episode = $tvdb->getEpisode($data[0]->id, 1, 1, 'en');
var_dump($episode);
Expand All @@ -25,6 +26,8 @@
*/

/*
$shows = $tvdb->search('Walking Dead');
$episodes = $tvdb->getEpisodes($shows->Series->id, 'fr');
echo count($episodes);*/
// Get full series and episode info
$episodes = $tvdb->getSerieEpisodes(153021, 'fr', Client::FORMAT_ZIP);
var_dump($episodes["episodes"]);
printf ("(%d Episodes)\n", count($episodes["episodes"]));
*/
40 changes: 39 additions & 1 deletion src/Moinax/TvDb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ public function getSerieEpisodes($serieId, $language = null, $format = self::FOR
$data = $this->fetchXml('series/' . $serieId . '/all/' . $language . '.' . $format);
break;
case self::FORMAT_ZIP:
if (!in_array('zip', stream_get_wrappers())) {
throw new \ErrorException('Your PHP does nort support ZIP stream wrappers');
}
$data = $this->fetchZIP('series/' . $serieId . '/all/' . $language . '.' . $format, array(), self::GET, $language.".xml");
break;
default:
throw new \ErrorException('Unsupported format');
break;
Expand Down Expand Up @@ -351,6 +356,39 @@ protected function fetchXml($function, $params = array(), $method = self::GET)
return $simpleXml;
}

/**
* Fetches data via curl and returns result
*
* @access protected
* @param string $function The function used to fetch data in zip
* @param array $params
* @param string $method
* @param string $file The file to extract from the ZIP
* @return string The data
*/
protected function fetchZIP($function, $params = array(), $method = self::GET, $file=null)
{
if (strpos($function, '.php') > 0) { // no need of api key for php calls
$url = $this->getMirror(self::MIRROR_TYPE_ZIP) . '/api/' . $function;
} else {
$url = $this->getMirror(self::MIRROR_TYPE_ZIP) . '/api/' . $this->apiKey . '/' . $function;
}

$zipName = tempnam(sys_get_temp_dir(), "tvdb-");
$zip = fopen($zipName, "w");
fwrite($zip, $this->httpClient->fetch($url, $params, $method));
fclose($zip);
if (is_null($file)) {
$file = $this->getDefaultLanguage().".xml";
}
$dataPath = "zip://".$zipName."#".$file;
$data = file_get_contents($dataPath);

$simpleXml = $this->getXml($data);

return $simpleXml;
}

/**
* Fetch data with curl
*
Expand Down Expand Up @@ -391,7 +429,7 @@ protected function getXml($data)
throw new XmlException(implode("\n", $errors));
}
}
throw new XmlException('Xml file cound not be loaded');
throw new XmlException('Xml file could not be loaded');
}

return $simpleXml;
Expand Down

0 comments on commit 278c9e7

Please sign in to comment.