/** * 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]); } }
public static function getDmaCode($dma_name = null) { $cache_key = self::getDmaMapCacheKey(); self::setDmaMapCache(); if (empty($dma_name)) { return Redis::hgetall($cache_key); } else { return Redis::hget($cache_key, $dma_name); } }
/** * GET, /live. Fetches SpaceXStats Live. * * @return \Illuminate\View\View */ public function live() { $isAuthed = Auth::check() && Auth::user()->isLaunchController() || Auth::isAdmin(); $js = ['auth' => $isAuthed, 'mission' => Mission::future()->first(), 'isActive' => Redis::get('live:active') == true, 'updates' => collect(Redis::lrange('live:updates', 0, -1))->map(function ($update) { return json_decode($update); }), 'countdown' => Redis::hgetall('live:countdown'), 'title' => Redis::get('live:title'), 'reddit' => Redis::hgetall('live:reddit'), 'sections' => json_decode(Redis::get('live:sections')), 'resources' => json_decode(Redis::get('live:resources')), 'description' => Redis::hgetall('live:description'), 'streams' => ['spacex' => json_decode(Redis::hget('live:streams', 'spacex')), 'spacexClean' => json_decode(Redis::hget('live:streams', 'spacexClean')), 'nasa' => json_decode(Redis::hget('live:streams', 'nasa'))], 'status' => ['text' => Redis::get('live:status')]]; if ($isAuthed) { $js['cannedResponses'] = Redis::hgetall('live:cannedResponses'); } JavaScript::put($js); return view('live'); }
/** * Execute the job. * * @return void */ public function handle() { // Rerender content $templatedOutput = view('templates.livethreadcontents')->with(['updates' => collect(Redis::lrange('live:updates', 0, -1))->reverse()->map(function ($update) { return json_decode($update); })])->render(); // Connect to Reddit $reddit = new Reddit(Config::get('services.reddit.username'), Config::get('services.reddit.password'), Config::get('services.reddit.id'), Config::get('services.reddit.secret')); $reddit->setUserAgent('ElongatedMuskrat bot by u/EchoLogic. Runs various /r/SpaceX-related tasks.'); // Update Thread $reddit->thing(Redis::hget('live:reddit', 'thing'))->edit($templatedOutput); }
/** * Constructs a human readable relative timestamp relative to when the event is being counted down to. * If a launch is paused, we will instead show a timestamp which reads "Paused". * * @internal * @return string */ private function constructTimestamp() { // Check if paused if (!Redis::hget('live:countdown', 'isPaused')) { $countdownTo = Carbon::createFromFormat('Y-m-d H:i:s', Redis::hget('live:countdown', 'to')); $diffInSeconds = $this->createdAt->diffInSeconds($countdownTo, false); $absDiffInSeconds = abs($diffInSeconds); $sign = $diffInSeconds < 0 ? '+' : '-'; if ($absDiffInSeconds > 86400) { $days = floor($absDiffInSeconds / 86400); $hours = intval(round($absDiffInSeconds % 86400 / 3600)); $timestamp = "T{$sign}{$days}d"; if ($hours !== 0) { if ($hours === 60) { $days++; $timestamp = "T{$sign}{$days}d"; } else { $timestamp .= " {$hours}h"; } } } elseif ($absDiffInSeconds > 3600) { $hours = floor($absDiffInSeconds / 3600); $minutes = intval(round($absDiffInSeconds % 3600 / 60)); $timestamp = "T{$sign}{$hours}h"; if ($minutes !== 0) { if ($minutes === 60) { $hours++; $timestamp = "T{$sign}{$hours}d"; } else { $timestamp .= " {$minutes}m"; } } } elseif ($absDiffInSeconds > 60) { $minutes = floor($absDiffInSeconds / 60); $seconds = intval(round($absDiffInSeconds % 60)); $timestamp = "T{$sign}{$minutes}m"; if ($seconds !== 0) { if ($seconds === 60) { $minutes++; $timestamp = "T{$sign}{$minutes}m"; } else { $timestamp .= " {$seconds}s"; } } } else { $timestamp = "T{$sign}{$absDiffInSeconds}s"; } return $timestamp; } return 'Paused'; }
/** * Create a new event instance. * * @param $isActive * @param null $videoId */ public function __construct($videos) { foreach ($videos as $videoName => $videoId) { // Update the Redis parameters $spacexLivestream = json_decode(Redis::hget('live:streams', $videoName)); if ($spacexLivestream === null) { $spacexLivestream = new \stdClass(); } $spacexLivestream->isActive = true; $spacexLivestream->isAvailable = true; $spacexLivestream->youtubeVideoId = $videoId; Redis::hset('live:streams', $videoName, json_encode($spacexLivestream)); } $this->videos = $videos; }
public static function getDmaCode($location = null) { if (empty($location)) { return null; } $field = str_replace([',', ' '], ':', strtolower(urldecode($location))); $cache_key = self::getCacheKey(); self::setCache(); $code = Redis::hget($cache_key, $field); if (empty($code)) { return null; } else { return $code; } }