Exemplo n.º 1
0
 /**
  * Retrieves user-submitted videos from YouTube and returns the
  * result as a JSON array.
  */
 public function getObservationDateVideos()
 {
     include_once HV_ROOT_DIR . '/../src/Database/MovieDatabase.php';
     include_once HV_ROOT_DIR . '/../src/Movie/HelioviewerMovie.php';
     include_once HV_ROOT_DIR . '/../lib/alphaID/alphaID.php';
     include_once HV_ROOT_DIR . '/../src/Helper/HelioviewerLayers.php';
     $movies = new Database_MovieDatabase();
     // Default options
     $defaults = array('num' => 10, 'skip' => 0, 'date' => getUTCDateString());
     $opts = array_replace($defaults, $this->_options);
     // Get a list of recent videos
     $videos = array();
     foreach ($movies->getSharedVideosByTime($opts['num'], $opts['skip'], $opts['date']) as $video) {
         $youtubeId = $video['youtubeId'];
         $movieId = (int) $video['movieId'];
         // Convert id
         $publicId = alphaID($movieId, false, 5, HV_MOVIE_ID_PASS);
         // Load movie
         $movie = new Movie_HelioviewerMovie($publicId);
         $layers = new Helper_HelioviewerLayers($video['dataSourceString']);
         $layersArray = $layers->toArray();
         $name = '';
         if (count($layersArray) > 0) {
             foreach ($layersArray as $layer) {
                 $name .= $layer['name'] . ', ';
             }
         }
         $name = substr($name, 0, -2);
         array_push($videos, array('id' => $publicId, 'url' => 'http://www.youtube.com/watch?v=' . $youtubeId, 'thumbnails' => array('small' => $video['thumbnail'], 'medium' => $video['thumbnail']), 'published' => $video['timestamp'], 'title' => $name . ' (' . $video['startDate'] . ' - ' . $video['endDate'] . ' UTC)', 'description' => $video['description'], 'keywords' => $video['keywords'], 'imageScale' => $video['imageScale'], 'dataSourceString' => $video['dataSourceString'], 'eventSourceString' => $video['eventSourceString'], 'movieLength' => $video['movieLength'], 'width' => $video['width'], 'height' => $video['height'], 'startDate' => $video['startDate'], 'endDate' => $video['endDate']));
     }
     $this->_printJSON(json_encode($videos));
 }
Exemplo n.º 2
0
    /**
     * Uploads a single video to YouTube
     *
     * @param int                           $id         Movie id
     * @param array                         $options    Movie options
     * @param Zend_Gdata_YouTube_VideoEntry $videoEntry A video entry object
     * describing the video to be uploaded
     *
     * Note: Content-length and Connection: close headers are sent so that
     * process can be run in the background without the user having to wait,
     * and the database entry will be updated even if the user closes the
     * browser window.
     *
     * See:
     * http://zulius.com/how-to/close-browser-connection-continue-execution/
     *
     * @return Zend_Gdata_YouTube_VideoEntry
     */
    private function _uploadVideoToYouTube($video, $filepath, $id, $title, $description, $tags, $share, $html)
    {
        include_once HV_ROOT_DIR . '/../src/Database/MovieDatabase.php';
        include_once HV_ROOT_DIR . '/../lib/alphaID/alphaID.php';
        $movies = new Database_MovieDatabase();
        // Add movie entry to YouTube table if entry does not already exist
        $movieId = alphaID($id, true, 5, HV_MOVIE_ID_PASS);
        if (!$movies->insertYouTubeMovie($movieId, $title, $description, $tags, $share)) {
            throw new Exception('Movie has already been uploaded. Please allow several minutes for your video to appear on YouTube.', 46);
        }
        // buffer all upcoming output
        ob_start();
        // let user know that upload is in progress
        if ($html) {
            ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Helioviewer.org - YouTube Upload In Progress</title>
    <link rel="shortcut icon" href="../favicon.ico">
    <meta charset="utf-8" />
</head>
<body style='text-align: center;'>
    <div style='margin-top: 200px;'>
        <span style='font-size: 32px;'>Upload processing</span><br />
        Your video should appear on YouTube in 1-2 minutes.
    </div>
</body>
<?php 
        } else {
            header('Content-type: application/json');
            echo json_encode(array('status' => 'upload in progress.'));
        }
        // get the size of the output
        $size = ob_get_length();
        // send headers to tell the browser to close the connection
        header('Content-Length: ' . $size);
        header('Connection: close');
        // flush all output
        ob_flush();
        ob_end_flush();
        flush();
        // close current session
        if (session_id()) {
            session_write_close();
        }
        // Begin upload
        try {
            // Specify the size of each chunk of data, in bytes. Set a higher value for
            // reliable connection as fewer chunks lead to faster uploads. Set a lower
            // value for better recovery on less reliable connections.
            $chunkSizeBytes = 1 * 1024 * 1024;
            // Setting the defer flag to true tells the client to return a request which can be called
            // with ->execute(); instead of making the API call immediately.
            $this->_httpClient->setDefer(true);
            // Create a request for the API's videos.insert method to create and upload the video.
            $insertRequest = $this->_youTube->videos->insert("status,snippet", $video);
            // Create a MediaFileUpload object for resumable uploads.
            $media = new Google_Http_MediaFileUpload($this->_httpClient, $insertRequest, 'video/*', null, true, $chunkSizeBytes);
            $media->setFileSize(filesize($filepath));
            // Read the media file and upload it chunk by chunk.
            $status = false;
            $handle = fopen($filepath, "rb");
            while (!$status && !feof($handle)) {
                $chunk = fread($handle, $chunkSizeBytes);
                $status = $media->nextChunk($chunk);
            }
            fclose($handle);
            // If you want to make other calls after the file upload, set setDefer back to false
            $this->_httpClient->setDefer(false);
        } catch (Zend_Gdata_App_HttpException $httpException) {
            throw $httpException;
        } catch (Zend_Gdata_App_Exception $e) {
            throw $e;
        }
        // Update database entry and return result
        $movies->updateYouTubeMovie($movieId, $status['id']);
        return $media;
    }