Skip to content

Commit

Permalink
Merge pull request #13 from cp6/1.6
Browse files Browse the repository at this point in the history
1.6
  • Loading branch information
cp6 authored Oct 14, 2021
2 parents 8d68cea + 87c2e73 commit 4ca5111
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 43 deletions.
37 changes: 36 additions & 1 deletion example.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,39 @@

//Delete folders (only works if folder empty)
echo $bunny->deleteFolder('pets/puppy_fluffy/');
echo $bunny->deleteFolder('pets/');
echo $bunny->deleteFolder('pets/');


/*
*
* Video stream API examples
*
*/

//List collections for library 1234
echo json_encode($bunny->getStreamCollections(1234));

//List videos for library 1234 and collection 886gce58-1482-416f-b908-fca0b60f49ba
$bunny->setStreamLibraryId(1234);
$bunny->setStreamCollectionGuid('886gce58-1482-416f-b908-fca0b60f49ba');
echo json_encode($bunny->listVideosForCollectionId());

//List video information individually
echo json_encode($bunny->getVideo(1234,'e6410005-d591-4a7e-a83d-6c1eef0fdc78'));

//Get array of resolutions for video
echo json_encode($bunny->videoResolutionsArray('e6410005-d591-4a7e-a83d-6c1eef0fdc78'));

//Get size of video
echo json_encode($bunny->videoSize('e6410005-d591-4a7e-a83d-6c1eef0fdc78', 'MB'));


//Create a video (prepare for upload)
echo json_encode($bunny->createVideo('title_for_the_video'));
//OR In collection
echo json_encode($bunny->createVideoForCollection('title_for_the_video'));
//These return information for the video. Importantly the video guid

//Upload the video file
echo json_encode($bunny->uploadVideo('a6e8483a-7538-4eb1-bb1f-6c1eef0fdc78', 'test_video.mp4'));
//Uploads test_video.mp4
139 changes: 97 additions & 42 deletions src/BunnyAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class BunnyAPI
private $connection;
private array $data;
private int $stream_library_id;
private string $stream_collection_guid = '';
private string $stream_video_guid = '';
private string $stream_collection_guid;
private string $stream_video_guid;

public function __construct(int $execution_time = 240, bool $json_header = false)
{
Expand Down Expand Up @@ -711,7 +711,7 @@ public function costCalculator(int $bytes): array
* Bunny net video stream section
*
*/
//Library -> collection -> video
//Stream library -> collection -> video
public function setStreamLibraryId(int $library_id): void
{
$this->stream_library_id = $library_id;
Expand All @@ -732,85 +732,140 @@ public function getVideoCollections(): array
return $this->APIcall('GET', "library/{$this->stream_library_id}/collections", [], false, true);
}

public function getStreamCollections(int $library_id = 0, int $page = 1, int $items_pp = 100, string $order_by = 'date'): array
public function getStreamCollections(int $page = 1, int $items_pp = 100, string $order_by = 'date'): array
{
if ($library_id === 0) {
$library_id = $this->stream_library_id;
}
return $this->APIcall('GET', "library/$library_id/collections?page=$page&itemsPerPage=$items_pp&orderBy=$order_by", [], false, true);
$this->checkStreamLibraryIdSet();
return $this->APIcall('GET', "library/{$this->stream_library_id}/collections?page=$page&itemsPerPage=$items_pp&orderBy=$order_by", [], false, true);
}

public function getStreamForCollection(int $library_id = 0, string $collection_guid = ''): array
public function getStreamForCollection(): array
{
if ($library_id === 0) {
$library_id = $this->stream_library_id;
}
if (empty($collection_guid)) {
$collection_guid = $this->stream_collection_guid;
}
return $this->APIcall('GET', "library/$library_id/collections/$collection_guid", [], false, true);
$this->checkStreamLibraryIdSet();
$this->checkStreamCollectionGuidSet();
return $this->APIcall('GET', "library/{$this->stream_library_id}/collections/" . $this->stream_collection_guid, [], false, true);
}

public function updateCollection(int $library_id, string $collection_guid, string $video_library_id, int $video_count, int $total_size): array
public function updateCollection(string $updated_collection_name): array
{
return $this->APIcall('POST', "library/$library_id/collections/$collection_guid", array("videoLibraryId" => $video_library_id, "videoCount" => $video_count, "totalSize" => $total_size), false, true);
$this->checkStreamLibraryIdSet();
return $this->APIcall('POST', "library/{$this->stream_library_id}/collections/" . $this->stream_collection_guid, array("name" => $updated_collection_name), false, true);
}

public function deleteCollection(int $library_id, string $collection_id): array
public function deleteCollection(): array
{
return $this->APIcall('DELETE', "library/$library_id/collections/$collection_id", [], false, true);
$this->checkStreamLibraryIdSet();
$this->checkStreamCollectionGuidSet();
return $this->APIcall('DELETE', "library/{$this->stream_library_id}/collections/" . $this->stream_collection_guid, [], false, true);
}

public function createCollection(int $library_id, string $video_library_id, int $video_count, int $total_size): array
public function createCollection(string $new_collection_name): array
{
return $this->APIcall('POST', "library/$library_id/collections", array("videoLibraryId" => $video_library_id, "videoCount" => $video_count, "totalSize" => $total_size), false, true);
$this->checkStreamLibraryIdSet();
return $this->APIcall('POST', "library/{$this->stream_library_id}/collections", array("name" => $new_collection_name), false, true);
}

public function listVideos(int $page = 1, int $items_pp = 100, string $order_by = 'date'): array
{
if (!isset($this->stream_library_id)) {
return array('response' => 'fail', 'action' => __FUNCTION__, 'message' => 'You must set library id with: setStreamLibraryId()');
}
$this->checkStreamLibraryIdSet();
return $this->APIcall('GET', "library/{$this->stream_library_id}/videos?page=$page&itemsPerPage=$items_pp&orderBy=$order_by", [], false, true);
}

public function getVideo(int $library_id, string $video_guid): array
public function listVideosForCollectionId(int $page = 1, int $items_pp = 100, string $order_by = 'date'): array
{
return $this->APIcall('GET', "library/$library_id/videos/$video_guid", [], false, true);
$this->checkStreamLibraryIdSet();
$this->checkStreamCollectionGuidSet();
return $this->APIcall('GET', "library/{$this->stream_library_id}/videos?collection={$this->stream_collection_guid}&page=$page&itemsPerPage=$items_pp&orderBy=$order_by", [], false, true);
}

public function deleteVideo(int $library_id, string $video_guid): array
public function getVideo(string $video_guid): array
{
return $this->APIcall('DELETE', "library/$library_id/videos/$video_guid", [], false, true);
$this->checkStreamLibraryIdSet();
return $this->APIcall('GET', "library/{$this->stream_library_id}/videos/$video_guid", [], false, true);
}

public function createVideo(int $library_id, string $video_title, string $collection_guid = ''): array
public function deleteVideo(string $video_guid): array
{
if (!empty($collection_guid)) {
return $this->APIcall('POST', "library/$library_id/videos?title=$video_title&collectionId=$collection_guid", [], false, true);
}
return $this->APIcall('POST', "library/$library_id/videos?title=$video_title", [], false, true);
$this->checkStreamLibraryIdSet();
return $this->APIcall('DELETE', "library/{$this->stream_library_id}/videos/$video_guid", [], false, true);
}

public function createVideo(string $video_title): array
{
$this->checkStreamLibraryIdSet();
return $this->APIcall('POST', "library/{$this->stream_library_id}/videos", array("title" => $video_title), false, true);
}

public function createVideoForCollection(string $video_title): array
{
$this->checkStreamLibraryIdSet();
$this->checkStreamCollectionGuidSet();
return $this->APIcall('POST', "library/{$this->stream_library_id}/videos", array("title" => $video_title, "collectionId" => $this->stream_collection_guid), false, true);
}

public function uploadVideo(int $library_id, string $video_guid, string $video_to_upload): array
public function uploadVideo(string $video_guid, string $video_to_upload): array
{
//Need to use createVideo() first to get video guid
return $this->APIcall('PUT', "library/$library_id/videos/$video_guid", array('file' => $video_to_upload), false, true);
$this->checkStreamLibraryIdSet();
return $this->APIcall('PUT', "library/{$this->stream_library_id}/videos/" . $video_guid, array('file' => $video_to_upload), false, true);

}

public function setThumbnail(string $video_guid, string $thumbnail_url): array
{
$this->checkStreamLibraryIdSet();
return $this->APIcall('POST', "library/{$this->stream_library_id}/videos/$video_guid/thumbnail?$thumbnail_url", [], false, true);

}

public function addCaptions(string $video_guid, string $srclang, string $label, string $captions_file): array
{
$this->checkStreamLibraryIdSet();
return $this->APIcall('POST', "library/{$this->stream_library_id}/videos/$video_guid/captions/$srclang?label=$label&captionsFile=$captions_file", [], false, true);
}

public function setThumbnail(int $library_id, string $video_guid, string $thumbnail_url): array
public function deleteCaptions(string $video_guid, string $srclang): array
{
return $this->APIcall('POST', "library/$library_id/videos/$video_guid/thumbnail?$thumbnail_url", [], false, true);
$this->checkStreamLibraryIdSet();
return $this->APIcall('DELETE', "library/{$this->stream_library_id}/videos/$video_guid/captions/$srclang", [], false, true);
}

public function addCaptions(int $library_id, string $video_guid, string $srclang, string $label, string $captions_file): array
public function videoResolutionsArray(string $video_guid): array
{
$this->checkStreamLibraryIdSet();
$data = $this->APIcall('GET', "library/{$this->stream_library_id}/videos/$video_guid", [], false, true);
return explode(",", $data['availableResolutions']);
}


public function videoSize(string $video_guid, string $size_type = 'MB', bool $format = false, float $decimals = 2): float
{
return $this->APIcall('POST', "library/$library_id/videos/$video_guid/captions/$srclang?label=$label&captionsFile=$captions_file", [], false, true);
$this->checkStreamLibraryIdSet();
$data = $this->APIcall('GET', "library/{$this->stream_library_id}/videos/$video_guid", [], false, true);
return $this->convertBytes($data['storageSize'], $size_type, $format, $decimals);
}

private function checkStreamLibraryIdSet(): void
{
try {
if (!isset($this->stream_library_id)) {
throw new BunnyAPIException("You must set the stream library id first. Use setStreamLibraryId()");
}
} catch (BunnyAPIException $e) {//display error message
echo $e->errorMessage();
exit;
}
}

public function deleteCaptions(int $library_id, string $video_guid, string $srclang): array
private function checkStreamCollectionGuidSet(): void
{
return $this->APIcall('DELETE', "library/$library_id/videos/$video_guid/captions/$srclang", [], false, true);
try {
if (!isset($this->stream_collection_guid)) {
throw new BunnyAPIException("You must set the stream collection guid first. Use setStreamCollectionGuid()");
}
} catch (BunnyAPIException $e) {//display error message
echo $e->errorMessage();
exit;
}
}

}

0 comments on commit 4ca5111

Please sign in to comment.