I have this code which automatically checks for new seasons and episodes of a show, however it also imports season 0 (Special Seasons) and I'm currently trying to work out how to make it not import season 0.
Below is the code, simply enough it checks what exists then pulls through any missing episodes and their respective season, however as my original script doesn't pull through season 0 this keeps updating them with that season.
defined('BASEPATH') or exit('No direct script access allowed');
class Api extends Controller
{
public function process()
{
$AuthUser = $this->getVariable("AuthUser");
$Route = $this->getVariable("Route");
$this->{$Route->params->api}();
}
public function tmdb() {
$Route = $this->getVariable("Route");
$Settings = $this->getVariable("Settings");
if($Route->params->action == 'auto') {
$Posts = $this->db->from('posts')->where('type','serie')->all();
foreach ($Posts as $Post) {
// Guzzle Get
$Client = new \GuzzleHttp\Client();
$Response = $Client->request(
'GET',
'https://api.themoviedb.org/3/tv/'.$Post['imdb_id'].'?api_key=' . get($Settings,'data.tmdb_api','api') . '&language='.get($Settings,'data.tmdb_language','api')
);
$Listing = json_decode($Response->getBody() , true);
// Season
$iSeason = 1;
foreach ($Listing['seasons'] as $Season) {
$CheckSeason = $this->db->from('posts_season')->where('content_id',$Post['id'])->where('name',$Season['season_number'])->first();
if(empty($CheckSeason['id'])) {
if($Season['season_number'] !== '0') {
$dataarray = array(
"content_id" => $Post['id'],
"name" => $Season['season_number']
);
$this->db->insert('posts_season')->set($dataarray);
$SeasonId = $this->db->lastId();
}
} else {
$SeasonId = $CheckSeason['id'];
}
$iSeason ;
$SeasonNumber = Input::cleaner(filter_var(trim($Season['season_number']), FILTER_SANITIZE_NUMBER_INT));
// Episode
$Episodes = $Client->request(
'GET',
'https://api.themoviedb.org/3/tv/' . $Post['imdb_id'] . '/season/'.$Season['season_number'].'?api_key=' . get($Settings,'data.tmdb_api','api') . '&language='.get($Settings,'data.tmdb_language','api')
);
$Episodes = json_decode($Episodes->getBody() , true);
foreach ($Episodes['episodes'] as $Episode) {
$CheckEpisode = $this->db->from('posts_episode')->where('content_id',$Post['id'])->where('season_id',$SeasonId)->where('name',Input::cleaner($Episode['episode_number']))->first();
if(empty($CheckEpisode)) {
$Image = 'https://image.tmdb.org/t/p/original/'.$Episode['still_path'];
$Data = array(
'name' => Input::cleaner($Episode['episode_number']),
'self' => Input::seo($Episode['episode_number']),
'description' => Input::cleaner($Episode['name']),
'season_id' => $SeasonId,
'content_id' => $Post['id'],
'release_date' => $Episode['air_date'],
'image' => $Image,
'status' => 1,
'created' => date('Y-m-d H:i:s')
);
$this->db->insert('posts_episode')->set($Data);
$EpisodeId = $this->db->lastId();
$EpisodeVideo = array(
'content_id' => $Post['id'],
'episode_id' => $EpisodeId,
'name' => 'Watch Now',
'service_id' => '0',
'language_id' => '0',
'player' => '0',
'sortable' => '0'
);
$this->db->insert('posts_video')->set($EpisodeVideo);
echo $Post['self'] . '-' . $Season['season_number'] . '-' . $Episode['episode_number'] . '<br>';
}
}
}
}
}
}
}
I have tried using commonly found answers on here such as:
if($Season['season_number'] == '0') { } else { ... }
if($Season['season_number'] !== '0') { ... } else {}
if ($Season['season_number'] < 0) continue;
I did get a method with is skipping the first foreach result to work, however not all shows have a season zero so this would result in it skipping season 1.
And a few others without success, any pointer in the right direction or an explanation on what I'm doing wrong would be perfect!
CodePudding user response:
if(empty($CheckSeason['id'])) {
if((int)$Season['season_number'] == 0) { continue; }
$dataarray = array(
"content_id" => $Post['id'],
"name" => $Season['season_number']
);
$this->db->insert('posts_season')->set($dataarray);
$SeasonId = $this->db->lastId();
} else {
$SeasonId = $CheckSeason['id'];
}
Here, try this. This is called a guarded clause so you don't need to wrap the entire thing in a If bracket. the break; will skip the code inside the parent If bracket. Also in your earlier example, you were comparing it to string type, while in the docs it is registered as integer
CodePudding user response:
If
if( (int)$Season['season_number'] < 1 ) {
}
doesn't do the magic, the content of $Season['season_number'] may be not as expected. Have you checked it?