From d8b18b5b0a65835b7acb7e40e9082d3621f819db Mon Sep 17 00:00:00 2001 From: Ethan Finlay Date: Wed, 14 Dec 2022 15:36:42 -0500 Subject: [PATCH 1/3] adding check for youtube credits --- src/Rule/VideoScan.php | 6 ++++++ src/Video/Youtube.php | 10 ++++++++-- tests/YoutubeTest.php | 8 ++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Rule/VideoScan.php b/src/Rule/VideoScan.php index c335983..17be85e 100644 --- a/src/Rule/VideoScan.php +++ b/src/Rule/VideoScan.php @@ -14,6 +14,7 @@ class VideoScan extends BaseRule const FAILED_CONNECTION = -1; const FAILED_CAPTIONS = 0; const SUCCESSFUL_CAPTIONS = 1; + const NO_API_CREDITS = -2; public $youtube = null; public $vimeo = null; @@ -40,6 +41,11 @@ public function check() $captions = $this->getCaptionData($url, $provider); + if (self::NO_API_CREDITS === $captions) { + $this->setError('Out of Youtube API credits'); + continue; + } + if (self::FAILED_CONNECTION === $captions) { $this->setError('Failed provider API connection.'); continue; diff --git a/src/Video/Youtube.php b/src/Video/Youtube.php index 6cfc7b1..abb17e4 100644 --- a/src/Video/Youtube.php +++ b/src/Video/Youtube.php @@ -9,6 +9,7 @@ class Youtube const YOUTUBE_FAILED_REQUEST = -1; const YOUTUBE_FAIL = 0; const YOUTUBE_SUCCESS = 1; + const YOUTUBE_NO_CREDITS = -2; private $regex = array( '@youtube\.com/embed/([^"\&\? ]+)@i', @@ -127,13 +128,18 @@ function getVideoData($link_url) $url = $this->search_url . $youtube_id . '&key=' . $this->api_key; $response = $this->client->request('GET', $url); + $result = json_decode($response->getBody()); + // validate response if ($response->getStatusCode() >= 400) { + foreach ($result->error->errors as $error) { + if ($error->reason === "quotaExceeded") { + return self::YOUTUBE_NO_CREDITS; + } + } return self::YOUTUBE_FAILED_REQUEST; } - $result = json_decode($response->getBody()); - if (!empty($result->error)) { return self::YOUTUBE_FAILED_REQUEST; } diff --git a/tests/YoutubeTest.php b/tests/YoutubeTest.php index 104da6b..43a808c 100644 --- a/tests/YoutubeTest.php +++ b/tests/YoutubeTest.php @@ -163,4 +163,12 @@ public function testCaptionsAutoGeneratedSuccess(){ $this->assertEquals($youtube->captionsAutoGenerated($response), YouTube::YOUTUBE_SUCCESS); } + // public function testNoApiCredits(){ + // $client = new \GuzzleHttp\Client(['http_errors' => false]); + // $youtube = new Youtube($client, 'en', 'AIzaSyChmQQw6pZ1H2j4bqid4Ael46mYaNSy7Jg'); + // $url = "https://www.youtube.com/watch?v=WTGn88ZL-pg"; + + // $this->assertEquals($youtube->getVideoData($url), YouTube::YOUTUBE_NO_CREDITS); + // } + } \ No newline at end of file From 45160fdbc3baa6abde097e2dfd140484d2086e4a Mon Sep 17 00:00:00 2001 From: Ethan Finlay Date: Wed, 14 Dec 2022 15:54:49 -0500 Subject: [PATCH 2/3] adding test for youtube credits running out --- tests/VideoScanTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/VideoScanTest.php b/tests/VideoScanTest.php index 0295885..955a7d9 100644 --- a/tests/VideoScanTest.php +++ b/tests/VideoScanTest.php @@ -184,4 +184,30 @@ public function testCheckInvalidKaltura() $this->assertEquals(0, $ruleMock->check(), 'No issues when API connection fails.'); $this->assertCount(1, $ruleMock->getErrors(), 'One error found when API connection fails.'); } + + public function testCheckNoApiCreditsYoutube() + { + $html = ''; + $dom = new \DOMDocument('1.0', 'utf-8'); + $dom->loadHTML($html); + $options = [ + 'vimeoApiKey' => 'test', + 'youtubeApiKey' => 'test', + 'kalturaApiKey' => 'test', + 'kalturaUsername' => 'test' + ]; + $response = VideoScan::NO_API_CREDITS; + + $ruleMock = $this->getMockBuilder(VideoScan::class) + ->setConstructorArgs([$dom, $options]) + ->onlyMethods(array('getCaptionData')) + ->getMock(); + + $ruleMock->expects($this->once()) + ->method('getCaptionData') + ->will($this->returnValue($response)); + + $this->assertEquals(0, $ruleMock->check(), 'No issues when credits run out.'); + $this->assertCount(1, $ruleMock->getErrors(), 'One error found when credits run out.'); + } } \ No newline at end of file From 922998690ab988d6d2fcaadcdd69ce1aa4d54c70 Mon Sep 17 00:00:00 2001 From: Ethan Finlay Date: Mon, 19 Dec 2022 12:38:09 -0500 Subject: [PATCH 3/3] adding concept of provider name --- src/Rule/VideoScan.php | 4 ++-- src/Video/Kaltura.php | 5 +++++ src/Video/Vimeo.php | 5 +++++ src/Video/Youtube.php | 5 +++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Rule/VideoScan.php b/src/Rule/VideoScan.php index 17be85e..d61a3e7 100644 --- a/src/Rule/VideoScan.php +++ b/src/Rule/VideoScan.php @@ -42,12 +42,12 @@ public function check() $captions = $this->getCaptionData($url, $provider); if (self::NO_API_CREDITS === $captions) { - $this->setError('Out of Youtube API credits'); + $this->setError("{$provider->getName()} videos cannot be scanned at this time. Please try again at a later time."); continue; } if (self::FAILED_CONNECTION === $captions) { - $this->setError('Failed provider API connection.'); + $this->setError("Failed {$provider->getName()} API connection."); continue; } diff --git a/src/Video/Kaltura.php b/src/Video/Kaltura.php index 8a7c98a..73e24d2 100644 --- a/src/Video/Kaltura.php +++ b/src/Video/Kaltura.php @@ -15,6 +15,7 @@ class Kaltura { const KALTURA_FAILED_CONNECTION = -1; const KALTURA_FAIL = 0; const KALTURA_SUCCESS = 1; + const PROVIDER_NAME = 'Kaltura'; private $client; private $language; @@ -127,4 +128,8 @@ function getVideoData($link_url) return !isset($result->objects) ? $result->objects : self::KALTURA_FAILED_CONNECTION; } + public function getName() { + return self::PROVIDER_NAME; + } + } \ No newline at end of file diff --git a/src/Video/Vimeo.php b/src/Video/Vimeo.php index 6e4a6b0..cf90786 100644 --- a/src/Video/Vimeo.php +++ b/src/Video/Vimeo.php @@ -9,6 +9,7 @@ class Vimeo const VIMEO_FAILED_CONNECTION = -1; const VIMEO_FAIL = 0; const VIMEO_SUCCESS = 1; + const PROVIDER_NAME = "Vimeo"; private $regex = '@vimeo\.com/[^0-9]*([0-9]{7,9})@i'; private $search_url = 'https://api.vimeo.com/videos/'; @@ -94,4 +95,8 @@ function getVideoData($link_url) return isset($result->data) ? $result->data : self::VIMEO_FAILED_CONNECTION; } + + public function getName() { + return self::PROVIDER_NAME; + } } diff --git a/src/Video/Youtube.php b/src/Video/Youtube.php index abb17e4..dfa5ec7 100644 --- a/src/Video/Youtube.php +++ b/src/Video/Youtube.php @@ -10,6 +10,7 @@ class Youtube const YOUTUBE_FAIL = 0; const YOUTUBE_SUCCESS = 1; const YOUTUBE_NO_CREDITS = -2; + const PROVIDER_NAME = 'Youtube'; private $regex = array( '@youtube\.com/embed/([^"\&\? ]+)@i', @@ -146,4 +147,8 @@ function getVideoData($link_url) return isset($result->items) ? $result->items : self::YOUTUBE_FAILED_REQUEST; } + + public function getName() { + return self::PROVIDER_NAME; + } }