Skip to content

Commit

Permalink
Merge pull request #241 from neildaniels/watch-providers
Browse files Browse the repository at this point in the history
Add support for watch providers
  • Loading branch information
wtfzdotnet authored Sep 10, 2021
2 parents e800d43 + 322a2a0 commit e330e8f
Show file tree
Hide file tree
Showing 26 changed files with 3,491 additions and 8 deletions.
13 changes: 13 additions & 0 deletions lib/Tmdb/Api/Movies.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ public function getVideos($movie_id, array $parameters = [], array $headers = []
return $this->get('movie/' . $movie_id . '/videos', $parameters, $headers);
}

/**
* Get the watch providers (by region) for a specific movie id.
*
* @param $movie_id
* @param array $parameters
* @param array $headers
* @return mixed
*/
public function getWatchProviders($movie_id, array $parameters = [], array $headers = [])
{
return $this->get('movie/' . $movie_id . '/watch/providers', $parameters, $headers);
}

/**
* Get the external ids that we have stored for a movie.
*
Expand Down
13 changes: 13 additions & 0 deletions lib/Tmdb/Api/Tv.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ public function getVideos($tvshow_id, array $parameters = [], array $headers = [
return $this->get('tv/' . $tvshow_id . '/videos', $parameters, $headers);
}

/**
* Get the watch providers (by region) for a specific movie id.
*
* @param $movie_id
* @param array $parameters
* @param array $headers
* @return mixed
*/
public function getWatchProviders($tvshow_id, array $parameters = [], array $headers = [])
{
return $this->get('tv/' . $tvshow_id . '/watch/providers', $parameters, $headers);
}

/**
* Get the changes for a specific TV show id.
*
Expand Down
28 changes: 28 additions & 0 deletions lib/Tmdb/Factory/MovieFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Tmdb\Model\Common\Translation;
use Tmdb\Model\Company;
use Tmdb\Model\Movie;
use Tmdb\Model\Watch;

/**
* Class MovieFactory
Expand Down Expand Up @@ -206,6 +207,33 @@ public function create(array $data = []): ?AbstractModel
$movie->setReleaseDates($release_dates);
}

if (array_key_exists('watch/providers', $data) && array_key_exists('results', $data['watch/providers'])) {
$watchProviders = new GenericCollection();
foreach ($data['watch/providers']['results'] as $iso31661 => $countryWatchData) {
$countryWatchData['iso_3166_1'] = $iso31661;

foreach (['flatrate', 'rent', 'buy'] as $providerType) {
$typeProviders = new GenericCollection();
foreach ($countryWatchData[$providerType] ?? [] as $providerData) {
if (isset($providerData['provider_id'])) {
$providerData['id'] = $providerData['provider_id'];
}
if (isset($providerData['provider_name'])) {
$providerData['name'] = $providerData['provider_name'];
}

$providerData['iso_3166_1'] = $iso31661;
$providerData['type'] = $providerType;
$typeProviders->add(null, $this->hydrate(new Watch\Provider(), $providerData));
}
$countryWatchData[$providerType] = $typeProviders;
}

$watchProviders->add($iso31661, $this->hydrate(new Watch\Providers(), $countryWatchData));
}
$movie->setWatchProviders($watchProviders);
}

if (array_key_exists('videos', $data)) {
$movie->setVideos($this->getVideoFactory()->createCollection($data['videos']));
}
Expand Down
28 changes: 28 additions & 0 deletions lib/Tmdb/Factory/TvFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Tmdb\Model\Person\CastMember;
use Tmdb\Model\Person\CrewMember;
use Tmdb\Model\Tv;
use Tmdb\Model\Watch;

/**
* Class TvFactory
Expand Down Expand Up @@ -244,6 +245,33 @@ public function create(array $data = []): ?AbstractModel
$tvShow->setNetworks($this->getNetworkFactory()->createCollection($data['networks']));
}

if (array_key_exists('watch/providers', $data) && array_key_exists('results', $data['watch/providers'])) {
$watchProviders = new GenericCollection();
foreach ($data['watch/providers']['results'] as $iso31661 => $countryWatchData) {
$countryWatchData['iso_3166_1'] = $iso31661;

foreach (['flatrate', 'rent', 'buy'] as $providerType) {
$typeProviders = new GenericCollection();
foreach ($countryWatchData[$providerType] ?? [] as $providerData) {
if (isset($providerData['provider_id'])) {
$providerData['id'] = $providerData['provider_id'];
}
if (isset($providerData['provider_name'])) {
$providerData['name'] = $providerData['provider_name'];
}

$providerData['iso_3166_1'] = $iso31661;
$providerData['type'] = $providerType;
$typeProviders->add(null, $this->hydrate(new Watch\Provider(), $providerData));
}
$countryWatchData[$providerType] = $typeProviders;
}

$watchProviders->add($iso31661, $this->hydrate(new Watch\Providers(), $countryWatchData));
}
$tvShow->setWatchProviders($watchProviders);
}

if (array_key_exists('videos', $data) && $data['videos'] !== null) {
$tvShow->setVideos($this->getVideoFactory()->createCollection($data['videos']));
}
Expand Down
24 changes: 24 additions & 0 deletions lib/Tmdb/Model/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ class Movie extends AbstractModel
* @var int
*/
private $voteCount;
/**
* @var GenericCollection
*/
private $watchProviders;

/**
* Constructor
Expand All @@ -255,6 +259,7 @@ public function __construct()
$this->recommendations = new GenericCollection();
$this->translations = new GenericCollection();
$this->videos = new Videos();
$this->watchProviders = new GenericCollection();
}

/**
Expand Down Expand Up @@ -1032,4 +1037,23 @@ public function setVideos($videos)

return $this;
}

/**
* @return GenericCollection
*/
public function getWatchProviders(): GenericCollection
{
return $this->watchProviders;
}

/**
* @param GenericCollection $watchProviders
* @return $this
*/
public function setWatchProviders($watchProviders)
{
$this->watchProviders = $watchProviders;

return $this;
}
}
1 change: 1 addition & 0 deletions lib/Tmdb/Model/Movie/QueryParameter/AppendToResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ final class AppendToResponse extends BaseAppendToResponse
public const LISTS = 'lists';
public const CHANGES = 'changes';
public const VIDEOS = 'videos';
public const WATCH_PROVIDERS = 'watch/providers';
}
43 changes: 43 additions & 0 deletions lib/Tmdb/Model/Query/Discover/DiscoverMoviesQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,49 @@ public function language($language)
return $this;
}

/**
* An ISO 3166-1 code. Combine this filter with with_watch_providers in order to filter your results by a specific watch provider in a specific region.
*
* @param string $watchRegion
* @return $this
*/
public function watchRegion($watchRegion)
{
$this->set('watch_region', $watchRegion);

return $this;
}

/**
* Only include movies with the specified watch providers. Combine with watch_region.
*
* @param array|string $watchProviders
* @param int $mode
* @return $this
*/
public function withWatchProviders($watchProviders, $mode = self::MODE_OR)
{
$this->set('with_watch_providers', $this->with($watchProviders, $mode));

return $this;
}

/**
* Only include movies with the specified monetization types. Combine with watch_region.
*
* Allowed Values: flatrate, free, ads, rent, buy
*
* @param array|string $watchProviders
* @param int $mode
* @return $this
*/
public function withWatchMonetizationTypes($watchProviders, $mode = self::MODE_OR)
{
$this->set('with_watch_monetization_types', $this->with($watchProviders, $mode));

return $this;
}

/**
* Minimum value is 1, expected value is an integer.
*
Expand Down
103 changes: 103 additions & 0 deletions lib/Tmdb/Model/Query/Discover/DiscoverTvQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Tmdb\Model\Query\Discover;

use DateTime;
use Tmdb\Model\AbstractModel;
use Tmdb\Model\Collection\QueryParametersCollection;

/**
Expand All @@ -23,6 +24,12 @@
*/
class DiscoverTvQuery extends QueryParametersCollection
{
/** Transform args to an AND query */
public const MODE_AND = 0;

/** Transform args to an OR query */
public const MODE_OR = 1;

/**
* Minimum value is 1, expected value is an integer.
*
Expand All @@ -49,6 +56,49 @@ public function language($language)
return $this;
}

/**
* An ISO 3166-1 code. Combine this filter with with_watch_providers in order to filter your results by a specific watch provider in a specific region.
*
* @param string $watchRegion
* @return $this
*/
public function watchRegion($watchRegion)
{
$this->set('watch_region', $watchRegion);

return $this;
}

/**
* Only include movies with the specified watch providers. Combine with watch_region.
*
* @param array|string $watchProviders
* @param int $mode
* @return $this
*/
public function withWatchProviders($watchProviders, $mode = self::MODE_OR)
{
$this->set('with_watch_providers', $this->with($watchProviders, $mode));

return $this;
}

/**
* Only include movies with the specified monetization types. Combine with watch_region.
*
* Allowed Values: flatrate, free, ads, rent, buy
*
* @param array|string $watchProviders
* @param int $mode
* @return $this
*/
public function withWatchMonetizationTypes($watchProviders, $mode = self::MODE_OR)
{
$this->set('with_watch_monetization_types', $this->with($watchProviders, $mode));

return $this;
}

/**
* Available options are vote_average.desc, vote_average.asc, first_air_date.desc,
* first_air_date.asc, popularity.desc, popularity.asc
Expand Down Expand Up @@ -109,6 +159,44 @@ public function voteAverageGte($average)
return $this;
}

/**
* Format the with compatible parameters.
*
* @param array|string $with
* @param int $mode
*
* @return null|string
*/
protected function with($with = null, $mode = self::MODE_OR): ?string
{
if ($with instanceof GenericCollection) {
$with = $with->toArray();
}

if (is_array($with)) {
return $this->andWith((array)$with, $mode);
}

return $with;
}

/**
* Creates an and query to combine an AND or an OR expression.
*
* @param array $with
* @param int $mode
* @return string
*/
protected function andWith(array $with, $mode)
{
return (
implode(
$mode === self::MODE_OR ? '|' : ',',
array_map([$this, 'normalize'], $with)
)
);
}

/**
* Creates an OR query for genres
*
Expand Down Expand Up @@ -227,4 +315,19 @@ public function withNetworksAnd(array $networks = [])
implode(',', $networks)
);
}

/**
* Extract object id's if an collection was passed on.
*
* @param $mixed
* @return mixed
*/
protected function normalize($mixed)
{
if (is_object($mixed) && $mixed instanceof AbstractModel) {
return $mixed->getId();
}

return $mixed;
}
}
Loading

0 comments on commit e330e8f

Please sign in to comment.