Skip to content

Commit 4ca5111

Browse files
authored
Merge pull request #13 from cp6/1.6
1.6
2 parents 8d68cea + 87c2e73 commit 4ca5111

File tree

2 files changed

+133
-43
lines changed

2 files changed

+133
-43
lines changed

example.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,39 @@
108108

109109
//Delete folders (only works if folder empty)
110110
echo $bunny->deleteFolder('pets/puppy_fluffy/');
111-
echo $bunny->deleteFolder('pets/');
111+
echo $bunny->deleteFolder('pets/');
112+
113+
114+
/*
115+
*
116+
* Video stream API examples
117+
*
118+
*/
119+
120+
//List collections for library 1234
121+
echo json_encode($bunny->getStreamCollections(1234));
122+
123+
//List videos for library 1234 and collection 886gce58-1482-416f-b908-fca0b60f49ba
124+
$bunny->setStreamLibraryId(1234);
125+
$bunny->setStreamCollectionGuid('886gce58-1482-416f-b908-fca0b60f49ba');
126+
echo json_encode($bunny->listVideosForCollectionId());
127+
128+
//List video information individually
129+
echo json_encode($bunny->getVideo(1234,'e6410005-d591-4a7e-a83d-6c1eef0fdc78'));
130+
131+
//Get array of resolutions for video
132+
echo json_encode($bunny->videoResolutionsArray('e6410005-d591-4a7e-a83d-6c1eef0fdc78'));
133+
134+
//Get size of video
135+
echo json_encode($bunny->videoSize('e6410005-d591-4a7e-a83d-6c1eef0fdc78', 'MB'));
136+
137+
138+
//Create a video (prepare for upload)
139+
echo json_encode($bunny->createVideo('title_for_the_video'));
140+
//OR In collection
141+
echo json_encode($bunny->createVideoForCollection('title_for_the_video'));
142+
//These return information for the video. Importantly the video guid
143+
144+
//Upload the video file
145+
echo json_encode($bunny->uploadVideo('a6e8483a-7538-4eb1-bb1f-6c1eef0fdc78', 'test_video.mp4'));
146+
//Uploads test_video.mp4

src/BunnyAPI.php

Lines changed: 97 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class BunnyAPI
2323
private $connection;
2424
private array $data;
2525
private int $stream_library_id;
26-
private string $stream_collection_guid = '';
27-
private string $stream_video_guid = '';
26+
private string $stream_collection_guid;
27+
private string $stream_video_guid;
2828

2929
public function __construct(int $execution_time = 240, bool $json_header = false)
3030
{
@@ -711,7 +711,7 @@ public function costCalculator(int $bytes): array
711711
* Bunny net video stream section
712712
*
713713
*/
714-
//Library -> collection -> video
714+
//Stream library -> collection -> video
715715
public function setStreamLibraryId(int $library_id): void
716716
{
717717
$this->stream_library_id = $library_id;
@@ -732,85 +732,140 @@ public function getVideoCollections(): array
732732
return $this->APIcall('GET', "library/{$this->stream_library_id}/collections", [], false, true);
733733
}
734734

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

743-
public function getStreamForCollection(int $library_id = 0, string $collection_guid = ''): array
741+
public function getStreamForCollection(): array
744742
{
745-
if ($library_id === 0) {
746-
$library_id = $this->stream_library_id;
747-
}
748-
if (empty($collection_guid)) {
749-
$collection_guid = $this->stream_collection_guid;
750-
}
751-
return $this->APIcall('GET', "library/$library_id/collections/$collection_guid", [], false, true);
743+
$this->checkStreamLibraryIdSet();
744+
$this->checkStreamCollectionGuidSet();
745+
return $this->APIcall('GET', "library/{$this->stream_library_id}/collections/" . $this->stream_collection_guid, [], false, true);
752746
}
753747

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

759-
public function deleteCollection(int $library_id, string $collection_id): array
754+
public function deleteCollection(): array
760755
{
761-
return $this->APIcall('DELETE', "library/$library_id/collections/$collection_id", [], false, true);
756+
$this->checkStreamLibraryIdSet();
757+
$this->checkStreamCollectionGuidSet();
758+
return $this->APIcall('DELETE', "library/{$this->stream_library_id}/collections/" . $this->stream_collection_guid, [], false, true);
762759
}
763760

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

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

777-
public function getVideo(int $library_id, string $video_guid): array
773+
public function listVideosForCollectionId(int $page = 1, int $items_pp = 100, string $order_by = 'date'): array
778774
{
779-
return $this->APIcall('GET', "library/$library_id/videos/$video_guid", [], false, true);
775+
$this->checkStreamLibraryIdSet();
776+
$this->checkStreamCollectionGuidSet();
777+
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);
780778
}
781779

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

787-
public function createVideo(int $library_id, string $video_title, string $collection_guid = ''): array
786+
public function deleteVideo(string $video_guid): array
788787
{
789-
if (!empty($collection_guid)) {
790-
return $this->APIcall('POST', "library/$library_id/videos?title=$video_title&collectionId=$collection_guid", [], false, true);
791-
}
792-
return $this->APIcall('POST', "library/$library_id/videos?title=$video_title", [], false, true);
788+
$this->checkStreamLibraryIdSet();
789+
return $this->APIcall('DELETE', "library/{$this->stream_library_id}/videos/$video_guid", [], false, true);
790+
}
791+
792+
public function createVideo(string $video_title): array
793+
{
794+
$this->checkStreamLibraryIdSet();
795+
return $this->APIcall('POST', "library/{$this->stream_library_id}/videos", array("title" => $video_title), false, true);
796+
}
797+
798+
public function createVideoForCollection(string $video_title): array
799+
{
800+
$this->checkStreamLibraryIdSet();
801+
$this->checkStreamCollectionGuidSet();
802+
return $this->APIcall('POST', "library/{$this->stream_library_id}/videos", array("title" => $video_title, "collectionId" => $this->stream_collection_guid), false, true);
793803
}
794804

795-
public function uploadVideo(int $library_id, string $video_guid, string $video_to_upload): array
805+
public function uploadVideo(string $video_guid, string $video_to_upload): array
796806
{
797807
//Need to use createVideo() first to get video guid
798-
return $this->APIcall('PUT', "library/$library_id/videos/$video_guid", array('file' => $video_to_upload), false, true);
808+
$this->checkStreamLibraryIdSet();
809+
return $this->APIcall('PUT', "library/{$this->stream_library_id}/videos/" . $video_guid, array('file' => $video_to_upload), false, true);
810+
811+
}
812+
813+
public function setThumbnail(string $video_guid, string $thumbnail_url): array
814+
{
815+
$this->checkStreamLibraryIdSet();
816+
return $this->APIcall('POST', "library/{$this->stream_library_id}/videos/$video_guid/thumbnail?$thumbnail_url", [], false, true);
817+
818+
}
819+
820+
public function addCaptions(string $video_guid, string $srclang, string $label, string $captions_file): array
821+
{
822+
$this->checkStreamLibraryIdSet();
823+
return $this->APIcall('POST', "library/{$this->stream_library_id}/videos/$video_guid/captions/$srclang?label=$label&captionsFile=$captions_file", [], false, true);
799824
}
800825

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

806-
public function addCaptions(int $library_id, string $video_guid, string $srclang, string $label, string $captions_file): array
832+
public function videoResolutionsArray(string $video_guid): array
833+
{
834+
$this->checkStreamLibraryIdSet();
835+
$data = $this->APIcall('GET', "library/{$this->stream_library_id}/videos/$video_guid", [], false, true);
836+
return explode(",", $data['availableResolutions']);
837+
}
838+
839+
840+
public function videoSize(string $video_guid, string $size_type = 'MB', bool $format = false, float $decimals = 2): float
807841
{
808-
return $this->APIcall('POST', "library/$library_id/videos/$video_guid/captions/$srclang?label=$label&captionsFile=$captions_file", [], false, true);
842+
$this->checkStreamLibraryIdSet();
843+
$data = $this->APIcall('GET', "library/{$this->stream_library_id}/videos/$video_guid", [], false, true);
844+
return $this->convertBytes($data['storageSize'], $size_type, $format, $decimals);
845+
}
846+
847+
private function checkStreamLibraryIdSet(): void
848+
{
849+
try {
850+
if (!isset($this->stream_library_id)) {
851+
throw new BunnyAPIException("You must set the stream library id first. Use setStreamLibraryId()");
852+
}
853+
} catch (BunnyAPIException $e) {//display error message
854+
echo $e->errorMessage();
855+
exit;
856+
}
809857
}
810858

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

816871
}

0 commit comments

Comments
 (0)