Last active
November 29, 2020 15:47
-
-
Save iDevelopThings/79c934d939dc6034bb7c1ec8ffb5fd69 to your computer and use it in GitHub Desktop.
PHP/Laravel, get youtube video id + start time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services\YouTube; | |
use Str; | |
class YoutubeUrl | |
{ | |
private string $url; | |
public function __construct($url) | |
{ | |
$this->url = $url; | |
return $this->parse(); | |
} | |
/** | |
* Parse and return video start time + id | |
* | |
* @return array | |
*/ | |
public function parse() | |
{ | |
$parsed = parse_url($this->url); | |
$query = null; | |
if (isset($parsed['query'])) { | |
parse_str($parsed['query'], $query); | |
} | |
if (!isset($query['v'])) { | |
$query['v'] = Str::afterLast($parsed['path'], '/'); | |
} | |
return [ | |
'start_at' => $query['t'] ?? $query['start'] ?? null, | |
'id' => $query['v'], | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services\YouTube; | |
use Carbon\Carbon; | |
use Illuminate\Support\Facades\Http; | |
use Str; | |
class YoutubeUrlAdvanced | |
{ | |
private string $url; | |
/** | |
* @var string|null | |
*/ | |
private ?string $id; | |
/** | |
* @var int|null | |
*/ | |
private ?int $startAt; | |
public ?object $video; | |
public function __construct($url) | |
{ | |
$this->url = $url; | |
$this->id = null; | |
$this->startAt = null; | |
$this->parse(); | |
} | |
/** | |
* Parse and return video start time + id | |
*/ | |
private function parse() | |
{ | |
$parsed = parse_url($this->url); | |
$query = null; | |
if (isset($parsed['query'])) { | |
parse_str($parsed['query'], $query); | |
} | |
if (!isset($query['v'])) { | |
$query['v'] = Str::afterLast($parsed['path'], '/'); | |
} | |
$this->startAt = $query['t'] ?? $query['start'] ?? null; | |
$this->id = $query['v']; | |
} | |
/** | |
* Get the information about the video url | |
* | |
* @return object|null | |
*/ | |
public function getVideoInformation() | |
{ | |
if ($this->id === null) { | |
return null; | |
} | |
$video = Http::get("https://www.googleapis.com/youtube/v3/videos", [ | |
'part' => 'contentDetails,snippet,status', | |
'id' => $this->id, | |
'key' => config('services.youtube.api_key'), | |
]); | |
if ($video->failed()) { | |
throw new \Exception('Failed to get video information: ' . json_encode($video->json())); | |
} | |
$this->video = $video->object()->items[0]; | |
return $this->video; | |
} | |
/** | |
* @return string|null | |
*/ | |
public function getId(): ?string | |
{ | |
return $this->id; | |
} | |
/** | |
* @return int|null | |
*/ | |
public function getStartAt(): ?int | |
{ | |
return $this->startAt; | |
} | |
/** | |
* @return string | |
*/ | |
public function getUrl(): string | |
{ | |
return $this->url; | |
} | |
/** | |
* Is this video potentially going to cause copyright issues? | |
* | |
* @return bool | |
*/ | |
public function isLicensedContent(): bool | |
{ | |
return $this->video->contentDetails->licensedContent === true; | |
} | |
/** | |
* Is it SFW/SFK? | |
* | |
* @return bool | |
*/ | |
public function isAgeRestricted(): bool | |
{ | |
return isset($this->video->contentDetails->contentRating->ytRating) && | |
$this->video->contentDetails->contentRating->ytRating === 'ytAgeRestricted'; | |
} | |
/** | |
* Can we view it in our country? | |
* | |
* @param string $region | |
* | |
* @return bool | |
*/ | |
public function isRegionBlocked(string $region): bool | |
{ | |
if (!isset($this->video->contentDetails->regionRestriction)) { | |
return false; | |
} | |
$regionRestriction = $this->video->contentDetails->regionRestriction; | |
if (isset($regionRestriction->allowed) && !in_array($region, $regionRestriction->allowed)) { | |
return true; | |
} | |
if (isset($regionRestriction->blocked) && in_array($region, $regionRestriction->blocked)) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Check the video is older than 1 hour | |
* | |
* @return bool | |
*/ | |
public function isFreshlyUploaded(): bool | |
{ | |
return Carbon::parse($this->video->snippet->publishedAt)->gt(now()->subHour()); | |
} | |
/** | |
* Get some basic data about the video to pass around, display etc | |
* | |
* @return array | |
*/ | |
public function data() | |
{ | |
return [ | |
'videoId' => $this->video->id, | |
'channelId' => $this->video->snippet->channelId, | |
'channelTitle' => $this->video->snippet->channelTitle, | |
'title' => $this->video->snippet->title, | |
'description' => $this->video->snippet->description, | |
'thumbnail' => $this->video->snippet->thumbnails->default->url | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment