/** * Execute the console command. * * @return mixed */ public function handle() { $youtube = new Client(); $searchResponse = json_decode($youtube->get('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' . $this->channelID . '&eventType=live&type=video&key=' . Config::get('services.youtube.key'))->getBody()); $isLive = $searchResponse->pageInfo->totalResults != 0; // Determine the total number of viewers if ($isLive) { $videoResponse = json_decode($youtube->get('https://www.googleapis.com/youtube/v3/videos?part=liveStreamingDetails&id=' . $searchResponse->items[0]->id->videoId . '&key=' . Config::get('services.youtube.key'))->getBody()); $viewers = $videoResponse->items[0]->liveStreamingDetails->concurrentViewers; } else { $viewers = 0; } // If the livestream is active now, and wasn't before, or vice versa, send an event if ($isLive && (Redis::hget('webcast', 'isLive') == 'false' || !Redis::hexists('webcast', 'isLive'))) { // Grab all the relevant SpaceX youtube Livestreams, and create an event $videos = $this->getMultipleYoutubeLivestreams($videoId); // $searchResponse->items[0]->id->videoId event(new WebcastStartedEvent($videos)); } elseif (!$isLive && Redis::hget('webcast', 'isLive') == 'true') { // turn off the spacex webcast event(new WebcastEndedEvent("spacex", false)); } // Set the Redis properties Redis::hmset('webcast', 'isLive', $isLive === true ? 'true' : 'false', 'viewers', $viewers); // Add to Database if livestream is active if ($isLive) { WebcastStatus::create(['viewers' => $viewers]); } }