2017-01-23 22:26:04 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\Models;
|
|
|
|
|
|
|
|
use App\Episode as Model;
|
2017-02-26 10:09:54 +01:00
|
|
|
use App\Item;
|
2017-01-23 22:26:04 +01:00
|
|
|
use App\Services\TMDB;
|
|
|
|
use App\Setting;
|
2017-03-04 02:01:50 +01:00
|
|
|
use Carbon\Carbon;
|
2017-01-23 22:26:04 +01:00
|
|
|
|
|
|
|
class EpisodeService {
|
|
|
|
|
|
|
|
private $model;
|
|
|
|
private $tmdb;
|
2017-02-26 10:09:54 +01:00
|
|
|
private $item;
|
2017-01-23 22:26:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Model $model
|
|
|
|
* @param TMDB $tmdb
|
2017-02-26 10:09:54 +01:00
|
|
|
* @param Item $item
|
|
|
|
* @internal param ItemService $itemService
|
|
|
|
* @internal param Item $item
|
2017-01-23 22:26:04 +01:00
|
|
|
*/
|
2017-02-26 10:09:54 +01:00
|
|
|
public function __construct(Model $model, TMDB $tmdb, Item $item)
|
2017-01-23 22:26:04 +01:00
|
|
|
{
|
|
|
|
$this->model = $model;
|
|
|
|
$this->tmdb = $tmdb;
|
2017-02-26 10:09:54 +01:00
|
|
|
$this->item = $item;
|
2017-01-23 22:26:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $item
|
|
|
|
*/
|
|
|
|
public function create($item)
|
|
|
|
{
|
2017-04-11 08:45:08 +02:00
|
|
|
// todo: rewrite this and make this more generic. we need to use this in refresh too, if a episode is not in database.
|
2017-01-23 22:26:04 +01:00
|
|
|
if($item->media_type == 'tv') {
|
|
|
|
$seasons = $this->tmdb->tvEpisodes($item->tmdb_id);
|
|
|
|
|
2017-04-11 08:45:08 +02:00
|
|
|
foreach($seasons as $season) {
|
|
|
|
$releaseSeason = Carbon::createFromFormat('Y-m-d', $season->air_date ?? '1970-12-1');
|
|
|
|
|
|
|
|
foreach($season->episodes as $episode) {
|
|
|
|
$releaseEpisode = Carbon::createFromFormat('Y-m-d', $episode->air_date ?? '1970-12-1');
|
|
|
|
|
|
|
|
$this->model->create([
|
|
|
|
'season_tmdb_id' => $season->id,
|
|
|
|
'episode_tmdb_id' => $episode->id,
|
|
|
|
'season_number' => $episode->season_number,
|
|
|
|
'episode_number' => $episode->episode_number,
|
|
|
|
'release_episode' => $releaseEpisode->getTimestamp(),
|
|
|
|
'release_season' => $releaseSeason->getTimestamp(),
|
|
|
|
'name' => $episode->name,
|
|
|
|
'tmdb_id' => $item->tmdb_id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 22:26:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all episodes by tmdb_id.
|
|
|
|
*
|
|
|
|
* @param $tmdbId
|
|
|
|
*/
|
|
|
|
public function remove($tmdbId)
|
|
|
|
{
|
|
|
|
$this->model->where('tmdb_id', $tmdbId)->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-21 21:20:42 +01:00
|
|
|
* Get all episodes of a tv show grouped by seasons,
|
|
|
|
* the data for the next unseen episode, which will be used in the modal as an indicator,
|
|
|
|
* and the setting option to check if spoiler protection is enabled.
|
2017-01-23 22:26:04 +01:00
|
|
|
*
|
|
|
|
* @param $tmdbId
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAllByTmdbId($tmdbId)
|
|
|
|
{
|
2017-03-04 02:01:50 +01:00
|
|
|
Carbon::setLocale(config('app.TRANSLATION'));
|
|
|
|
|
2017-07-20 16:45:19 +02:00
|
|
|
$episodes = $this->model->findByTmdbId($tmdbId)->get();
|
2017-02-21 21:20:42 +01:00
|
|
|
|
2017-01-23 22:26:04 +01:00
|
|
|
return [
|
2017-07-20 16:45:19 +02:00
|
|
|
'episodes' => $episodes->groupBy('season_number')->sortBy('episode_number'),
|
2017-02-21 21:20:42 +01:00
|
|
|
'next_episode' => $episodes->where('seen', 0)->first(),
|
2017-01-23 22:26:04 +01:00
|
|
|
'spoiler' => Setting::first()->episode_spoiler_protection,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-06 09:40:58 +01:00
|
|
|
* Set an episode as seen / unseen.
|
|
|
|
*
|
2017-01-23 22:26:04 +01:00
|
|
|
* @param $id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function toggleSeen($id)
|
|
|
|
{
|
|
|
|
$episode = $this->model->find($id);
|
|
|
|
|
2017-01-25 14:44:51 +01:00
|
|
|
if($episode) {
|
2017-02-26 10:09:54 +01:00
|
|
|
// Update the parent relation only if we mark the episode as seen.
|
|
|
|
if( ! $episode->seen) {
|
|
|
|
$this->item->updateLastSeenAt($episode->tmdb_id);
|
|
|
|
}
|
|
|
|
|
2017-01-25 14:44:51 +01:00
|
|
|
return $episode->update([
|
|
|
|
'seen' => ! $episode->seen,
|
|
|
|
]);
|
|
|
|
}
|
2017-01-23 22:26:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-06 09:40:58 +01:00
|
|
|
* Toggle all episodes of a season as seen / unseen.
|
|
|
|
*
|
2017-01-23 22:26:04 +01:00
|
|
|
* @param $tmdbId
|
|
|
|
* @param $season
|
|
|
|
* @param $seen
|
|
|
|
*/
|
|
|
|
public function toggleSeason($tmdbId, $season, $seen)
|
|
|
|
{
|
|
|
|
$episodes = $this->model->findSeason($tmdbId, $season)->get();
|
|
|
|
|
2017-02-26 10:09:54 +01:00
|
|
|
// Update the parent relation only if we mark the episode as seen.
|
|
|
|
if($seen) {
|
|
|
|
$this->item->updateLastSeenAt($episodes[0]->tmdb_id);
|
|
|
|
}
|
|
|
|
|
2017-01-23 22:26:04 +01:00
|
|
|
$episodes->each(function($episode) use ($seen) {
|
|
|
|
$episode->update([
|
|
|
|
'seen' => $seen,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
}
|
2017-01-25 14:44:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See if we can find a episode by src or tmdb_id.
|
|
|
|
* Or we search a specific episode in our database.
|
|
|
|
*
|
|
|
|
* @param $type
|
|
|
|
* @param $value
|
|
|
|
* @param null $episode
|
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
public function findBy($type, $value, $episode = null)
|
|
|
|
{
|
|
|
|
switch($type) {
|
|
|
|
case 'src':
|
|
|
|
return $this->model->findBySrc($value)->first();
|
2017-02-21 19:15:31 +01:00
|
|
|
case 'fp_name':
|
|
|
|
return $this->model->findByFPName($value)->first();
|
2017-01-25 14:44:51 +01:00
|
|
|
case 'tmdb_id':
|
|
|
|
return $this->model->findByTmdbId($value)->first();
|
|
|
|
case 'episode':
|
|
|
|
return $this->model->findSpecificEpisode($value, $episode)->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2017-01-24 16:11:47 +01:00
|
|
|
}
|