/** * */ public function run() { SapphireTransactionManager::getInstance()->transaction(function () { $pageToken = null; while (true) { if ($this->videosUpdated >= SummitVideoApp::config()->popular_video_limit) { break; } // Prevent an infinite loop if the YouTube service is acting strange if ($this->sanityCheck === 5) { $e = new Exeception('Task has run too many times. Seems to be an infinite loop. Could be something wrong with the YouTube service?'); SS_Log::log($e, SS_Log::ERR); throw $e; } try { $response = $this->api->getPopularVideos($pageToken); } catch (\Exception $e) { SS_Log::log("YouTube Search failed" . $e->getMessage(), SS_Log::ERR); } $this->sanityCheck++; $body = $response->getBody()->getContents(); $data = Convert::json2array($body); $nextPageToken = @$data['nextPageToken']; $ids = []; foreach ($data['items'] as $item) { if ($item['id']['kind'] === 'youtube#video') { $ids[] = $item['id']['videoId']; } } try { $response = $this->api->getVideoStatsById($ids); } catch (\Exception $e) { SS_Log::log("YouTube video stats failed" . $e->getMessage(), SS_Log::ERR); } $body = $response->getBody()->getContents(); $data = Convert::json2array($body); foreach ($data['items'] as $v) { $video = PresentationVideo::get()->filter(['YouTubeID' => $v['id']])->first(); if ($video) { $video->Views = $v['statistics']['viewCount']; $video->write(); $this->videosUpdated++; } } // If there are no more pages, then bail if ($nextPageToken === $pageToken) { break; } $pageToken = $nextPageToken; } echo "{$this->videosUpdated} videos updated.\n"; }); }
/** * */ public function run() { SapphireTransactionManager::getInstance()->transaction(function () { $unprocessedVideos = PresentationVideo::get()->filter(['Processed' => false])->limit(50); if (!$unprocessedVideos->exists()) { return; } $summit = Summit::get_active(); $dateUTC = $summit->convertDateFromTimeZone2UTC(SS_DateTime::now()->Rfc2822()); $dateUTCTimestamp = strtotime($dateUTC); $maxAge = SummitVideoApp::config()->abandon_unprocessed_videos_after; $ids = []; foreach ($unprocessedVideos as $video) { $age = $dateUTCTimestamp - strtotime($video->DateUploaded); if ($age > $maxAge) { SS_Log::log("Video {$video->Title} has been unprocessed for a long time. ({$age} seconds). It should be deleted.", SS_Log::WARN); continue; } $ids[] = $video->YouTubeID; } try { $response = $this->api->getVideoStatusById($ids); } catch (\Exception $e) { SS_Log::log("YouTube check for status failed" . $e->getMessage(), SS_Log::ERR); return; } $body = $response->getBody()->getContents(); $data = Convert::json2array($body); $items = $data['items']; if (empty($items)) { echo "No videos are marked as processing. Exiting.\n"; return; } foreach ($items as $item) { $currentStatus = $item['status']['uploadStatus']; if ($currentStatus == 'processed') { $video = PresentationVideo::get()->filter(['YouTubeID' => $item['id']])->first(); if (!$video) { SS_Log::log("Tried to update processing status for " . $item['id'] . " but no PresentationVideo with that YouTubeID was found.", SS_Log::WARN); continue; } $video->Processed = true; $video->write(); $this->videosUpdated++; } } echo "{$this->videosUpdated} videos updated.\n"; }); }
/** * @param $id * @return array */ public function getVideoDetail($id) { $video = PresentationVideo::get()->filter(['Presentation.Slug' => $id, 'DisplayOnSite' => true, 'Processed' => true])->first(); if (!$video) { $video = PresentationVideo::get()->filter(['ID' => $id, 'DisplayOnSite' => true, 'Processed' => true])->first(); } if ($video) { $cutoff = time() - SummitVideoApp::config()->video_view_staleness; $videoStaleness = strtotime($video->ViewsLastUpdated); // Refresh the views if they're not of acceptable staleness if (!$video->ViewsLastUpdated || $videoStaleness < $cutoff) { // Set the last updated regardless of the outcome, so we don't get // unthrottled failures. $video->ViewsLastUpdated = SS_DateTime::now()->Rfc2822(); try { $response = $this->youTube->getVideoStatsById($video->YouTubeID); if ($response) { $data = Convert::json2array($response->getBody()->getContents()); if (!empty($data['items'])) { $videoData = $data['items'][0]; $video->Views = $videoData['statistics']['viewCount']; } } } catch (Exception $e) { SS_Log::log("Summit video app tried to get video {$video->YouTubeID}: {$e->getMessage()}", SS_Log::ERR); } $video->write(); } $json = $this->createVideoJSON($video); $json['description'] = $video->Presentation()->ShortDescription ?: $video->Presentation()->Description; return $json; } }
/** * @param bool $absolute * @return null|string */ public function getLink($absolute = true) { $page = SummitVideoApp::get()->first(); if ($page) { if ($absolute) { return $page->getAbsoluteLiveLink(false) . $this->Presentation()->Summit()->Slug . '/' . $this->Presentation()->Slug; } return $page->RelativeLink(false) . $this->Presentation()->Summit()->Slug . '/' . $this->Presentation()->Slug; } }
/** * @return mixed */ public function getJSONConfig() { $config = ['baseURL' => rtrim($this->Link(), '/'), 'initialState' => $this->getInitialState(), 'pollInterval' => SummitVideoApp::config()->video_poll_interval, 'securityToken' => SecurityToken::inst()->getValue()]; return Convert::array2json($config); }