Files
newznab-tmux/newznab/libraries/TraktAPI.php
T
2015-10-21 14:17:40 +02:00

251 lines
6.2 KiB
PHP

<?php
namespace newznab\libraries;
use newznab\utility\Utility;
/**
* Class TraktAPI
* Retrive info from the Trakt API.
*/
Class TraktAPI {
const API_URL = 'https://api-v2launch.trakt.tv/';
/**
* Construct. Set up API key.
*
* @param array $options Class instances.
*
* @access public
*/
public function __construct(array $options = [])
{
$defaults = [
'clientID' => '',
'headers' => ''
];
$options += $defaults;
$this->clientID = $options['clientID'];
$this->requestHeaders = $options['headers'];
}
/**
* Fetches summary from trakt.tv for the TV show using the title/season/episode.
*
* @param string $title
* @param string $season
* @param string $ep
* @param string $type
*
* @return array|bool
* @see http://docs.trakt.apiary.io/#reference/episodes/summary/get-a-single-episode-for-a-show
*
* @access public
*/
public function episodeSummary($title = '', $season = '', $ep = '', $type = 'min')
{
switch($type) {
case 'full':
case 'images':
case 'full,images':
$extended = $type;
break;
default:
$extended = 'min';
}
$array = $this->getJsonArray(
self::API_URL . 'shows/' .
str_replace([' ', '_', '.'], '-', $title) .
'/seasons/' .
str_replace(['S', 's'], '', $season) .
'/episodes/' .
str_replace(['E', 'e'], '', $ep),
$extended
);
if (!$array) {
return false;
}
return $array;
}
/**
* Fetches summary from trakt.tv for the movie.
* Accept a title (the-big-lebowski-1998), a IMDB id, or a TMDB id.
*
* @param string $movie Title or IMDB id.
* @param string $type imdbID: Return only the IMDB ID (returns string)
* full: Return all extended properties (minus images). (returns array)
* images: Return extended images properties (returns array)
* full,images: Return all extended properties (plus images). (returns array)
*
* @see http://docs.trakt.apiary.io/#reference/movies/summary/get-a-movie
*
* @return bool|array|string
*
* @access public
*/
public function movieSummary($movie = '', $type = 'imdbID')
{
switch($type) {
case 'full':
case 'images':
case 'full,images':
$extended = $type;
break;
case 'imdbID':
default:
$extended = 'min';
}
$array = $this->getJsonArray(
self::API_URL . 'movies/' . str_replace([' ', '_', '.'], '-', str_replace(['(', ')'], '', $movie)),
$extended
);
if (!$array) {
return false;
} else if ($type === 'imdbID' && isset($array['ids']['imdb'])) {
return $array['ids']['imdb'];
}
return $array;
}
/**
* Fetches shows calendar from trakt.tv .
*
* @param string $start Start date of calendar ie. 2015-09-01.Default value is today.
* @param int $days Number of days to lookup ahead. Default value is 7 days
*
* @return array|bool
* @see http://docs.trakt.apiary.io/#reference/calendars/all-shows/get-shows
*
* @access public
*/
public function getCalendar($start = '', $days = 7)
{
$array = $this->getJsonArray(
self::API_URL . 'calendars/all/shows/' . $start . '/' . $days
);
if (!$array) {
return false;
}
return $array;
}
/**
* Fetches weekend box office data from trakt.tv, updated every monday.
*
* @return array|bool
* @see http://docs.trakt.apiary.io/#reference/movies/box-office/get-the-weekend-box-office
*
* @access public
*/
public function getBoxOffice()
{
$array = $this->getJsonArray(
self::API_URL . 'movies/boxoffice'
);
if (!$array) {
return false;
}
return $array;
}
/**
* Fetches summary from trakt.tv for the show.
* Accept a trakt slug (game-of-thrones), a IMDB id, or Trakt id.
*
* @param string $show Title or IMDB id.
* @param string $type full: Return all extended properties (minus images). (returns array)
* images: Return extended images properties (returns array)
* full,images: Return all extended properties (plus images). (returns array)
*
* @see http://docs.trakt.apiary.io/#reference/shows/summary/get-a-single-show
*
* @return bool|array|string
*
* @access public
*/
public function showSummary($show = '', $type = 'full')
{
$showUrl = self::API_URL . 'shows/' . str_replace([' ', '_', '.'], '-', str_replace(['(', ')'], '', $show));
switch($type) {
case 'images':
case 'full,images':
$extended = $type;
break;
case 'full':
default:
$extended = 'full';
}
$array = $this->getJsonArray($showUrl, $extended);
if (!$array) {
return false;
}
return $array;
}
/**
* Fetches summary from trakt.tv for the show by doing a search.
* Accepts a search string
*
* @param string $show title
* @param string $type show
*
* @see http://docs.trakt.apiary.io/#reference/search/get-text-query-results
*
* @return bool|array|string
*
* @access public
*/
public function showSearch($show = '', $type = 'show')
{
$searchUrl = self::API_URL . 'search?query=' . str_replace([' ', '_', '.'], '-', str_replace(['(', ')'], '', $show)) . '&type=' . $type;
$array = $this->getJsonArray($searchUrl, '');
if (!$array) {
return false;
}
return $array;
}
/**
* Download JSON from Trakt, convert to array.
*
* @param string $URI URI to download.
* @param string $extended Extended info from trakt tv.
* Valid values:
* 'min' Returns enough info to match locally. (Default)
* 'images' Minimal info and all images.
* 'full' Complete info for an item.
* 'full,images' Complete info and all images.
*
* @return bool|mixed
*/
private function getJsonArray($URI, $extended = 'min')
{
if ($extended === '') {
$extendedString = '';
} else {
$extendedString = "?extended=" . $extended;
}
if (!empty($this->clientID)) {
$json = Utility::getUrl([
'url' => $URI . $extendedString,
'requestheaders' => $this->requestHeaders
]
);
if ($json !== false) {
$json = json_decode($json, true);
if (!is_array($json) || (isset($json['status']) && $json['status'] === 'failure')) {
return false;
}
return $json;
}
}
return false;
}
}