Skip to content

Commit

Permalink
Merge pull request #45 from cidilabs/checkYoutubeCredits
Browse files Browse the repository at this point in the history
Check youtube credits/Added provider name concept
  • Loading branch information
cidilabs authored Dec 19, 2022
2 parents f66c584 + 9229986 commit 6657ae6
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/Rule/VideoScan.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,8 +41,13 @@ public function check()

$captions = $this->getCaptionData($url, $provider);

if (self::NO_API_CREDITS === $captions) {
$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;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Video/Kaltura.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}
5 changes: 5 additions & 0 deletions src/Video/Vimeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Expand Down Expand Up @@ -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;
}
}
15 changes: 13 additions & 2 deletions src/Video/Youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Youtube
const YOUTUBE_FAILED_REQUEST = -1;
const YOUTUBE_FAIL = 0;
const YOUTUBE_SUCCESS = 1;
const YOUTUBE_NO_CREDITS = -2;
const PROVIDER_NAME = 'Youtube';

private $regex = array(
'@youtube\.com/embed/([^"\&\? ]+)@i',
Expand Down Expand Up @@ -127,17 +129,26 @@ 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;
}

return isset($result->items) ? $result->items : self::YOUTUBE_FAILED_REQUEST;
}

public function getName() {
return self::PROVIDER_NAME;
}
}
26 changes: 26 additions & 0 deletions tests/VideoScanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<embed type="video/webm" src="https://www.youtube.com/watch?v=1xZxxVlu7BM" width="400" height="300">';
$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.');
}
}
8 changes: 8 additions & 0 deletions tests/YoutubeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// }

}

0 comments on commit 6657ae6

Please sign in to comment.