diff --git a/backend/app/Http/Controllers/SettingController.php b/backend/app/Http/Controllers/SettingController.php index bed45cf..d6cd7fe 100644 --- a/backend/app/Http/Controllers/SettingController.php +++ b/backend/app/Http/Controllers/SettingController.php @@ -93,8 +93,9 @@ return version_compare($this->version, $lastestVersion, '<') ? 'true' : 'false'; } + /** - * Parse full genre list of all movies in database and save them. + * Parse full genre list of all movies and tv shows in our database and save them. * * @param TMDB $tmdb */ @@ -104,18 +105,13 @@ $items = $this->item->all(); - foreach($items as $item) { - if( ! $item->genre) { - $data = []; - $genres = $tmdb->movie($item->tmdb_id)->genres; - foreach($genres as $genre) { - $data[] = $genre->name; - } + $items->each(function($item) use ($tmdb) { + $genres = $tmdb->details($item->tmdb_id, $item->media_type)->genres; + $data = collect($genres)->pluck('name')->all(); - $item->genre = implode($data, ', '); - $item->save(); - } - } + $item->genre = implode($data, ', '); + $item->save(); + }); } /** diff --git a/backend/app/Services/TMDB.php b/backend/app/Services/TMDB.php index d20a18a..507ce8b 100644 --- a/backend/app/Services/TMDB.php +++ b/backend/app/Services/TMDB.php @@ -163,14 +163,15 @@ } /** - * Get full movie details. + * Get full movie or tv details. * - * @param $tmdb_id + * @param $tmdbId + * @param $mediaType * @return mixed */ - public function movie($tmdb_id) + public function details($tmdbId, $mediaType) { - $response = $this->client->get($this->base . '/3/movie/' . $tmdb_id, [ + $response = $this->client->get($this->base . '/3/' . $mediaType . '/' . $tmdbId, [ 'query' => [ 'api_key' => $this->apiKey, 'language' => strtolower($this->translation) @@ -183,7 +184,7 @@ // After 10 seconds the TMDB request limit is resetted. sleep(10); - return $this->movie($tmdb_id); + return $this->details($tmdbId, $mediaType); } /** diff --git a/backend/tests/Setting/SettingTest.php b/backend/tests/Setting/SettingTest.php index 2a0b115..c7bee0c 100644 --- a/backend/tests/Setting/SettingTest.php +++ b/backend/tests/Setting/SettingTest.php @@ -1,6 +1,11 @@ assertEquals(0, $setting2->show_date); $this->assertEquals(0, $setting2->episode_spoiler_protection); } + + /** @test */ + public function it_should_update_genre_for_a_movie() + { + $this->createMovie(); + + $this->createGuzzleMock($this->tmdbFixtures('movie_details')); + + $withoutGenre = Item::find(1); + $this->actingAs($this->user)->json('PATCH', 'api/update-genre'); + $withGenre = Item::find(1); + + $this->assertEmpty($withoutGenre->genre); + $this->assertNotEmpty($withGenre->genre); + } + + /** @test */ + public function it_should_update_genre_for_a_tv_show() + { + $this->createTv(); + + $this->createGuzzleMock($this->tmdbFixtures('tv_details')); + + $withoutGenre = Item::find(1); + $this->actingAs($this->user)->json('PATCH', 'api/update-genre'); + $withGenre = Item::find(1); + + $this->assertEmpty($withoutGenre->genre); + $this->assertNotEmpty($withGenre->genre); + } + + private function createGuzzleMock($fixture) + { + $mock = new MockHandler([ + new Response(200, ['X-RateLimit-Remaining' => [40]], $fixture), + ]); + + $handler = HandlerStack::create($mock); + $this->app->instance(Client::class, new Client(['handler' => $handler])); + } } diff --git a/backend/tests/fixtures/tmdb/movie_details.json b/backend/tests/fixtures/tmdb/movie_details.json new file mode 100644 index 0000000..6e133f2 --- /dev/null +++ b/backend/tests/fixtures/tmdb/movie_details.json @@ -0,0 +1,79 @@ +{ + "adult": false, + "backdrop_path": "/5SX2rgKXZ7NVmAJR5z5LprqSXKa.jpg", + "belongs_to_collection": null, + "budget": 160000000, + "genres": [ + { + "id": 12, + "name": "Adventure" + }, + { + "id": 14, + "name": "Fantasy" + }, + { + "id": 28, + "name": "Action" + } + ], + "homepage": "http://www.warcraft-themovie.com/", + "id": 68735, + "imdb_id": "tt0803096", + "original_language": "en", + "original_title": "Warcraft", + "overview": "The peaceful realm of Azeroth stands on the brink of war as its civilization faces a fearsome race of invaders: orc warriors fleeing their dying home to colonize another. As a portal opens to connect the two worlds, one army faces destruction and the other faces extinction. From opposing sides, two heroes are set on a collision course that will decide the fate of their family, their people, and their home.", + "popularity": 4.823419, + "poster_path": "/ckrTPz6FZ35L5ybjqvkLWzzSLO7.jpg", + "production_companies": [ + { + "name": "Universal Pictures", + "id": 33 + }, + { + "name": "Atlas Entertainment", + "id": 507 + }, + { + "name": "Blizzard Entertainment", + "id": 16028 + }, + { + "name": "Legendary Entertainment", + "id": 84419 + } + ], + "production_countries": [ + { + "iso_3166_1": "CA", + "name": "Canada" + }, + { + "iso_3166_1": "CN", + "name": "China" + }, + { + "iso_3166_1": "JP", + "name": "Japan" + }, + { + "iso_3166_1": "US", + "name": "United States of America" + } + ], + "release_date": "2016-05-25", + "revenue": 433537548, + "runtime": 123, + "spoken_languages": [ + { + "iso_639_1": "en", + "name": "English" + } + ], + "status": "Released", + "tagline": "Two worlds. One home.", + "title": "Warcraft", + "video": false, + "vote_average": 6.2, + "vote_count": 1265 +} \ No newline at end of file diff --git a/backend/tests/fixtures/tmdb/tv_details.json b/backend/tests/fixtures/tmdb/tv_details.json new file mode 100644 index 0000000..ee8eb83 --- /dev/null +++ b/backend/tests/fixtures/tmdb/tv_details.json @@ -0,0 +1,143 @@ +{ + "backdrop_path": "/aKz3lXU71wqdslC1IYRC3yHD6yw.jpg", + "created_by": [ + { + "id": 9813, + "name": "David Benioff", + "profile_path": "/8CuuNIKMzMUL1NKOPv9AqEwM7og.jpg" + }, + { + "id": 228068, + "name": "D. B. Weiss", + "profile_path": "/caUAtilEe06OwOjoQY3B7BgpARi.jpg" + } + ], + "episode_run_time": [ + 60 + ], + "first_air_date": "2011-04-17", + "genres": [ + { + "id": 10759, + "name": "Action & Adventure" + }, + { + "id": 18, + "name": "Drama" + }, + { + "id": 10765, + "name": "Sci-Fi & Fantasy" + } + ], + "homepage": "http://www.hbo.com/game-of-thrones", + "id": 1399, + "in_production": true, + "languages": [ + "es", + "en", + "de" + ], + "last_air_date": "2017-06-25", + "name": "Game of Thrones", + "networks": [ + { + "id": 49, + "name": "HBO" + } + ], + "number_of_episodes": 61, + "number_of_seasons": 7, + "origin_country": [ + "US" + ], + "original_language": "en", + "original_name": "Game of Thrones", + "overview": "Seven noble families fight for control of the mythical land of Westeros. Friction between the houses leads to full-scale war. All while a very ancient evil awakens in the farthest north. Amidst the war, a neglected military order of misfits, the Night's Watch, is all that stands between the realms of men and icy horrors beyond.", + "popularity": 35.712295, + "poster_path": "/jIhL6mlT7AblhbHJgEoiBIOUVl1.jpg", + "production_companies": [ + { + "name": "Home Box Office (HBO)", + "id": 3268 + }, + { + "name": "Generator Entertainment", + "id": 5820 + }, + { + "name": "Television 360", + "id": 12525 + }, + { + "name": "Bighead Littlehead", + "id": 12526 + }, + { + "name": "Revolution Sun Studios", + "id": 76043 + } + ], + "seasons": [ + { + "air_date": "2010-12-05", + "episode_count": 13, + "id": 3627, + "poster_path": "/kMTcwNRfFKCZ0O2OaBZS0nZ2AIe.jpg", + "season_number": 0 + }, + { + "air_date": "2011-04-17", + "episode_count": 10, + "id": 3624, + "poster_path": "/olJ6ivXxCMq3cfujo1IRw30OrsQ.jpg", + "season_number": 1 + }, + { + "air_date": "2012-04-01", + "episode_count": 10, + "id": 3625, + "poster_path": "/3U8IVLqitMHMuEAgkuz8qReguHd.jpg", + "season_number": 2 + }, + { + "air_date": "2013-03-31", + "episode_count": 10, + "id": 3626, + "poster_path": "/eVWAat0GqF6s5LLThrI7ClpKr96.jpg", + "season_number": 3 + }, + { + "air_date": "2014-04-06", + "episode_count": 10, + "id": 3628, + "poster_path": "/dniQ7zw3mbLJkd1U0gdFEh4b24O.jpg", + "season_number": 4 + }, + { + "air_date": "2015-04-12", + "episode_count": 10, + "id": 62090, + "poster_path": "/yKOltUHsp9X7dXWIm0hNGcIQa4G.jpg", + "season_number": 5 + }, + { + "air_date": "2016-04-24", + "episode_count": 10, + "id": 71881, + "poster_path": "/zvYrzLMfPIenxoq2jFY4eExbRv8.jpg", + "season_number": 6 + }, + { + "air_date": "2017-06-25", + "episode_count": 1, + "id": 81266, + "poster_path": null, + "season_number": 7 + } + ], + "status": "Returning Series", + "type": "Scripted", + "vote_average": 7.9, + "vote_count": 1528 +} \ No newline at end of file diff --git a/client/app/components/Content/Settings.vue b/client/app/components/Content/Settings.vue index 58047e3..1f07bf4 100644 --- a/client/app/components/Content/Settings.vue +++ b/client/app/components/Content/Settings.vue @@ -32,7 +32,6 @@