From 5097fc1b538f756b69abdf1201c54d18a0d59590 Mon Sep 17 00:00:00 2001 From: Tim Steiner <tsteiner2@unl.edu> Date: Wed, 30 Jul 2008 21:22:58 +0000 Subject: [PATCH] Add PodcastProducer libraries to UNL Library --- library/Unl/Service/PodcastProducer.php | 180 ++++++++++++++++++ .../Unl/Service/PodcastProducer/Camera.php | 44 +++++ .../Unl/Service/PodcastProducer/Workflow.php | 58 ++++++ 3 files changed, 282 insertions(+) create mode 100644 library/Unl/Service/PodcastProducer.php create mode 100644 library/Unl/Service/PodcastProducer/Camera.php create mode 100644 library/Unl/Service/PodcastProducer/Workflow.php diff --git a/library/Unl/Service/PodcastProducer.php b/library/Unl/Service/PodcastProducer.php new file mode 100644 index 00000000..32fc3eca --- /dev/null +++ b/library/Unl/Service/PodcastProducer.php @@ -0,0 +1,180 @@ +<?php + +class Unl_Service_PodcastProducer extends Zend_Service_Abstract +{ + /** + * Base URI for the podcast producer server + * + * @var Zend_Uri_Http + */ + protected $_uri; + + protected $_curl; + + public function __construct($hostname, $port = 8170) + { + $this->_uri = Zend_Uri::factory('https'); + $this->_uri->setHost($hostname); + $this->_uri->setPort($port); + + $this->_curl = curl_init(); + curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($this->_curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($this->_curl, CURLOPT_SSL_VERIFYHOST, false); + } + + public function authenticate($username, $password) + { + curl_setopt($this->_curl, CURLOPT_USERPWD, "$username:$password"); + curl_setopt($this->_curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); + } + + public function getWorkflows() + { + $uri = clone $this->_uri; + $uri->setPath('/podcastproducer/workflows'); + + curl_setopt($this->_curl, CURLOPT_URL, $uri->__toString()); + $response = curl_exec($this->_curl); + if ($response === false) { + echo curl_error($this->_curl); + } + + return Unl_Service_PodcastProducer_Workflow::fromXML($response); + } + + public function getCameras() + { + $uri = clone $this->_uri; + $uri->setPath('/podcastproducer/cameras'); + + curl_setopt($this->_curl, CURLOPT_URL, $uri->__toString()); + $response = curl_exec($this->_curl); + if ($response === false) { + echo curl_error($this->_curl); + } + + return Unl_Service_PodcastProducer_Camera::fromXML($response); + } + + public function getCameraPreview(Unl_Service_PodcastProducer_Camera $camera) + { + curl_setopt($this->_curl, CURLOPT_URL, $camera->getPreviewUrl()); + $response = curl_exec($this->_curl); + return $response; + } + + public function createForFile(Unl_Service_PodcastProducer_Workflow $workflow, $filePath) + { + $fileContents = file_get_contents($filePath); + $fileSize = strlen($fileContents); + $fileName = basename($filePath); + + $uri = clone $this->_uri; + $uri->setPath('/podcastproducer/submissions/create_for_file'); + + curl_setopt($this->_curl, CURLOPT_URL, $uri->__toString()); + curl_setopt($this->_curl, CURLOPT_POST, true); + curl_setopt($this->_curl, CURLOPT_POSTFIELDS, 'workflow_name='. urlencode($workflow->getName()) . '&submission_type=file'); + $response = curl_exec($this->_curl); + if ($response === false) { + echo curl_error($this->_curl); + } + + //header('Content-type: text/xml'); + echo $response; + + $r = DOMDocument::loadXML($response); + $xPath = new DOMXPath($r); + $recordingUUID = $xPath->query('recording_uuid')->item(0)->textContent; + $uploadUri = $xPath->query('https_upload_url')->item(0)->textContent; + $UUID = $xPath->query('uuid')->item(0)->textContent; + + curl_setopt($this->_curl, CURLOPT_URL, $uploadUri); + curl_setopt($this->_curl, CURLOPT_HTTPHEADER, array( + 'File-name: ' . $fileName, + 'Recording-UUID: ' . $recordingUUID + )); + curl_setopt($this->_curl, CURLOPT_POST, true); + curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $fileContents); + $response = curl_exec($this->_curl); + + echo $response . "\n\n\n"; + + curl_setopt($this->_curl, CURLOPT_URL, $uploadUri); + curl_setopt($this->_curl, CURLOPT_HTTPHEADER, array( + 'File-name: user_metadata.plist', + 'Recording-UUID: ' . $recordingUUID + )); + curl_setopt($this->_curl, CURLOPT_POST, true); + curl_setopt($this->_curl, CURLOPT_POSTFIELDS, ''); + $response = curl_exec($this->_curl); + + echo $response . "\n\n\n"; + + $uri->setPath('/podcastproducer/submissions/complete'); + curl_setopt($this->_curl, CURLOPT_URL, $uri->__toString()); + curl_setopt($this->_curl, CURLOPT_HTTPHEADER, array()); + curl_setopt($this->_curl, CURLOPT_POST, true); + curl_setopt($this->_curl, CURLOPT_POSTFIELDS, + 'submission_uuid=' . $UUID + . '&submitted_files=user_metadata.plist:0;' . $fileName . ':' . $fileSize + . '&primary_content_file=' . $fileName + . '&recording_uuid=' . $recordingUUID + ); + $response = curl_exec($this->_curl); + + echo $response; + } + + public function startCamera(Unl_Service_PodcastProducer_Camera $camera, $delay = 3) + { + $uri = clone $this->_uri; + $uri->setPath('/podcastproducer/cameras/start'); + + curl_setopt($this->_curl, CURLOPT_URL, $uri->__toString()); + curl_setopt($this->_curl, CURLOPT_HTTPHEADER, array()); + curl_setopt($this->_curl, CURLOPT_POST, true); + curl_setopt($this->_curl, CURLOPT_POSTFIELDS, 'delay='. intval($delay) . '&camera_name=' . urlencode($camera->getName())); + $response = curl_exec($this->_curl); + if ($response === false) { + echo curl_error($this->_curl); + } + } + + public function pauseCamera(Unl_Service_PodcastProducer_Camera $camera) + { + $uri = clone $this->_uri; + $uri->setPath('/podcastproducer/cameras/pause'); + + curl_setopt($this->_curl, CURLOPT_URL, $uri->__toString()); + curl_setopt($this->_curl, CURLOPT_HTTPHEADER, array()); + curl_setopt($this->_curl, CURLOPT_POST, true); + curl_setopt($this->_curl, CURLOPT_POSTFIELDS, 'camera_name=' . urlencode($camera->getName())); + $response = curl_exec($this->_curl); + if ($response === false) { + echo curl_error($this->_curl); + } + + } + + public function stopCamera(Unl_Service_PodcastProducer_Camera $camera, Unl_Service_PodcastProducer_Workflow $workflow, $metadata = array()) + { + $uri = clone $this->_uri; + $uri->setPath('/podcastproducer/cameras/stop'); + + curl_setopt($this->_curl, CURLOPT_URL, $uri->__toString()); + curl_setopt($this->_curl, CURLOPT_HTTPHEADER, array()); + curl_setopt($this->_curl, CURLOPT_POST, true); + $postData = 'workflow_name='. urlencode($workflow->getName()) + . '&camera_name=' . urlencode($camera->getName()); + foreach ($metadata as $key => $value) { + $postData .= '&' . urlencode($key) . '=' . urlencode($value); + } + curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $postData); + $response = curl_exec($this->_curl); + if ($response === false) { + echo curl_error($this->_curl); + } + } +} \ No newline at end of file diff --git a/library/Unl/Service/PodcastProducer/Camera.php b/library/Unl/Service/PodcastProducer/Camera.php new file mode 100644 index 00000000..a700f25d --- /dev/null +++ b/library/Unl/Service/PodcastProducer/Camera.php @@ -0,0 +1,44 @@ +<?php + +class Unl_Service_PodcastProducer_Camera +{ + protected $_name; + protected $_uuid; + protected $_previewUrl; + + static public function fromXML($xml) + { + $dom = DOMDocument::loadXML($xml); + $xPath = new DOMXPath($dom); + if ($xPath->query('/podcast_producer_result/status')->item(0)->textContent != 'success') { + return; + } + $plist = Unl_Plist::plistToArray($xml); + $cameras = array(); + foreach ($plist['cameras'] as $data) { + $camera = new self(); + $camera->_name = $data['name']; + $camera->_uuid = $data['uuid']; + $camera->_previewUrl = $data['preview_url']; + $cameras[] = $camera; + } + return $cameras; + } + + protected function __construct() {} + + public function getName() + { + return $this->_name; + } + + public function getUuid() + { + return $this->_uuid; + } + + public function getPreviewUrl() + { + return $this->_previewUrl; + } +} \ No newline at end of file diff --git a/library/Unl/Service/PodcastProducer/Workflow.php b/library/Unl/Service/PodcastProducer/Workflow.php new file mode 100644 index 00000000..be65b968 --- /dev/null +++ b/library/Unl/Service/PodcastProducer/Workflow.php @@ -0,0 +1,58 @@ +<?php + +class Unl_Service_PodcastProducer_Workflow +{ + protected $_name; + protected $_title; + protected $_description; + protected $_uuid; + protected $_userRequirements = array(); + + static public function fromXML($xml) + { + $dom = DOMDocument::loadXML($xml); + $xPath = new DOMXPath($dom); + if ($xPath->query('/podcast_producer_result/status')->item(0)->textContent != 'success') { + return; + } + $plist = Unl_Plist::plistToArray($xml); + $workflows = array(); + foreach ($plist['workflows'] as $data) { + $workflow = new self(); + $workflow->_name = $data['name']; + $workflow->_title = $data['title']; + $workflow->_description = $data['description']; + $workflow->_uuid = $data['uuid']; + $workflow->_userRequirements = $data['user_requirements']; + $workflows[] = $workflow; + } + return $workflows; + } + + protected function __construct() {} + + public function getName() + { + return $this->_name; + } + + public function getTitle() + { + return $this->_title; + } + + public function getDescription() + { + return $this->_description; + } + + public function getUuid() + { + return $this->_uuid; + } + + public function getUserRequirements() + { + return $this->_userRequirements; + } +} \ No newline at end of file -- GitLab