コード例 #1
0
ファイル: YouTube.php プロジェクト: Lavoaster/PVLive
 public static function fetch($url, $params = array())
 {
     $di = \Phalcon\Di::getDefault();
     $config = $di->get('config');
     // Set up Google Client
     $gclient_api_key = $config->apis->google_apis_key;
     $gclient_app_name = $config->application->name;
     if (empty($gclient_api_key)) {
         return null;
     }
     $gclient = new \Google_Client();
     $gclient->setApplicationName($gclient_app_name);
     $gclient->setDeveloperKey($gclient_api_key);
     $yt_client = new \PVL\Service\YouTube($gclient);
     // Retrieve account info from URL processor.
     $account_info = self::getAccount($url);
     // For "User" account types, use "Uploads" playlist.
     if ($account_info['type'] == 'user') {
         $data = $yt_client->channels->listChannels('id,contentDetails', array('forUsername' => $account_info['id'], 'maxResults' => 1));
         if ($data) {
             $playlist_id = $data['items'][0]['contentDetails']['relatedPlaylists']['uploads'];
         }
     } else {
         $playlist_id = $account_info['id'];
     }
     if (empty($playlist_id)) {
         return null;
     }
     $data = $yt_client->getPlaylistItems($playlist_id);
     $news_items = array();
     foreach ((array) $data as $item) {
         if ($item['status']['privacyStatus'] !== 'public') {
             continue;
         }
         if ($item['status']['uploadStatus'] !== 'processed') {
             continue;
         }
         $embed_src = 'http://www.youtube.com/watch?v=' . $item['id'];
         $news_items[] = array('guid' => 'youtube_' . md5($item['id']), 'timestamp' => strtotime($item['snippet']['publishedAt']), 'title' => $item['snippet']['title'], 'body' => $item['snippet']['description'], 'web_url' => $embed_src, 'thumbnail_url' => \PVL\Service\YouTube::getThumbnail($item['snippet']['thumbnails'], 'medium'), 'banner_url' => \PVL\Service\YouTube::getThumbnail($item['snippet']['thumbnails'], 'large'), 'author' => $item['snippet']['channelTitle']);
     }
     return $news_items;
 }
コード例 #2
0
 /**
  * Process an individual convention archive row.
  * @param ConventionArchive $row
  */
 public static function process(ConventionArchive $row)
 {
     $di = \Phalcon\Di::getDefault();
     $em = $di->get('em');
     $config = $di->get('config');
     // Set up Google Client
     $gclient_api_key = $config->apis->google_apis_key;
     $gclient_app_name = $config->application->name;
     if (empty($gclient_api_key)) {
         return null;
     }
     $gclient = new \Google_Client();
     $gclient->setApplicationName($gclient_app_name);
     $gclient->setDeveloperKey($gclient_api_key);
     $yt_client = new YouTube($gclient);
     $url = $row->web_url;
     if (empty($row->playlist_id)) {
         switch ($row->type) {
             case "yt_playlist":
                 $url_parts = \PVL\Utilities::parseUrl($url);
                 $playlist_id = $url_parts['query_arr']['list'];
                 if (!$playlist_id) {
                     break;
                 }
                 // Clear existing related items.
                 $em->createQuery('DELETE FROM Entity\\ConventionArchive ca WHERE ca.playlist_id = :id')->setParameter('id', $row->id)->execute();
                 $data = $yt_client->playlists->listPlaylists('id,snippet', array('id' => $playlist_id, 'maxResults' => 1));
                 if ($data) {
                     $playlist = $data['items'][0]['snippet'];
                     $row->name = $playlist['title'];
                     $row->description = $playlist['description'];
                     $row->thumbnail_url = YouTube::getThumbnail($playlist['thumbnails']);
                 }
                 // Get playlist contents.
                 $data = $yt_client->getPlaylistItems($playlist_id);
                 foreach ((array) $data as $item) {
                     $row_name = self::filterName($row, $item['snippet']['title']);
                     $row_thumb = YouTube::getThumbnail($item['snippet']['thumbnails']);
                     // Apply name/thumbnail filtering to sub-videos.
                     if (!empty($row_name) && !empty($row_thumb)) {
                         $child_row = new ConventionArchive();
                         $child_row->convention = $row->convention;
                         $child_row->playlist_id = $row->id;
                         $child_row->type = 'yt_video';
                         $child_row->folder = $row->folder;
                         $child_row->name = $row_name;
                         $child_row->description = $item['snippet']['description'];
                         $child_row->web_url = 'http://www.youtube.com/watch?v=' . $item['id'];
                         $child_row->thumbnail_url = $row_thumb;
                         $em->persist($child_row);
                     }
                 }
                 $row->synchronized_at = time();
                 $em->persist($row);
                 break;
             case "yt_video":
             default:
                 // Pull video ID from any URL format.
                 if (preg_match('%(?:youtube(?:-nocookie)?\\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\\.be/)([^"&?/ ]{11})%i', $url, $match)) {
                     $video_id = $match[1];
                 } else {
                     break;
                 }
                 // Reformat video URL to match standard format.
                 $row->web_url = 'http://www.youtube.com/watch?v=' . $video_id;
                 // Pull data from API.
                 $data = $yt_client->videos->listVideos('snippet,contentDetails', array('id' => $video_id, 'maxResults' => 1));
                 if ($data) {
                     $video = $data['items'][0]['snippet'];
                     $row->name = self::filterName($row, $video['title']);
                     $row->description = $video['description'];
                     $row->thumbnail_url = YouTube::getThumbnail($video['thumbnails']);
                     $row->synchronized_at = time();
                     $em->persist($row);
                 }
                 break;
         }
     }
     $em->flush();
 }