示例#1
0
 public function monitorChannels()
 {
     foreach ($this->channels->getItems() as $index => $channel) {
         try {
             $API_URL = $this->youtube->getApi('playlistItems.list');
             $params = array('playlistId' => $channel["upload_playlist"], 'part' => 'id, snippet, contentDetails, status', 'maxResults' => 1);
             $apiData = $this->youtube->api_get($API_URL, $params);
             $items = $this->youtube->decodeList($apiData);
             if (!isset($items[0])) {
                 throw new ExistenceException("Youtube returned a empty array...?");
             }
             $item = $items[0];
             //convert from old format to new?
             if (isset($channel["last_video"])) {
                 $channel["last_videos"] = [$channel["last_video"]];
                 unset($channel["last_video"]);
             }
             //first run?
             if (!isset($channel["last_videos"])) {
                 $channel["last_videos"] = [$item->contentDetails->videoId];
             }
             if (!in_array($item->contentDetails->videoId, $channel["last_videos"])) {
                 //we've got one!
                 $success = $this->reddit->submit($channel["subreddit"], "link", $item->snippet->title, $this->config->prepend . $item->contentDetails->videoId);
                 if ($success) {
                     $this->info("Submitted \"{$item->snippet->title}\" from {$channel['channel']} to /r/{$channel['subreddit']}");
                 } else {
                     $this->error("Failed submitting \"{$item->snippet->title}\" from {$channel['channel']} to /r/{$channel['subreddit']}");
                 }
                 //update last video
                 $channel["last_videos"][] = $item->contentDetails->videoId;
             }
             $channel["last_check"] = time();
         } catch (\Exception $e) {
             $this->error("Error while getting playlist {$channel['upload_playlist']} for channel {$channel['channel_id']} to /r/{$channel['subreddit']}:\n{$e->getMessage()}");
         }
         $this->channels->setItem($index, $channel);
     }
 }