Skip to content

Commit

Permalink
added types
Browse files Browse the repository at this point in the history
  • Loading branch information
Athlon1600 committed Nov 4, 2023
1 parent 9c2e33d commit aa72002
Show file tree
Hide file tree
Showing 21 changed files with 216 additions and 172 deletions.
14 changes: 10 additions & 4 deletions src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace YouTube;

use Curl\BrowserClient;
use Curl\Response;
use YouTube\Utils\Utils;

class Browser extends BrowserClient
{
public function setUserAgent($agent): void
public function setUserAgent(string $agent): void
{
$this->headers['User-Agent'] = $agent;
}
Expand All @@ -17,13 +18,18 @@ public function getUserAgent(): ?string
return Utils::arrayGet($this->headers, 'User-Agent');
}

public function followRedirects($enabled): self
public function followRedirects(bool $enabled): self
{
$this->options[CURLOPT_FOLLOWLOCATION] = $enabled ? 1 : 0;
return $this;
}

public function cachedGet($url)
/**
* Return some special response that lets caller know if cache miss or hit
* @param string $url
* @return Response
*/
public function cachedGet(string $url): Response
{
$cache_path = sprintf('%s/%s', static::getStorageDirectory(), $this->getCacheKey($url));

Expand All @@ -44,7 +50,7 @@ public function cachedGet($url)
return $response;
}

protected function getCacheKey($url): string
protected function getCacheKey(string $url): string
{
return md5($url) . '_v4';
}
Expand Down
2 changes: 1 addition & 1 deletion src/DownloadOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected function getLowToHighAudioFormats(): array
/** @var StreamFormat $a */
/** @var StreamFormat $b */

return $a->contentLength - $b->contentLength;
return intval($a->contentLength) - intval($b->contentLength);
});

return $copy;
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/TooManyRequestsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TooManyRequestsException extends YouTubeException
{
protected $page;
protected WatchVideoPage $page;

public function __construct(WatchVideoPage $page)
{
Expand All @@ -18,7 +18,7 @@ public function __construct(WatchVideoPage $page)
/**
* @return WatchVideoPage
*/
public function getPage()
public function getPage(): WatchVideoPage
{
return $this->page;
}
Expand Down
27 changes: 9 additions & 18 deletions src/Models/InitialPlayerResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,23 @@
* JSON data that appears inside /watch?v= page [ytInitialPlayerResponse=]
* @package YouTube\Models
*/
class InitialPlayerResponse
class InitialPlayerResponse extends JsonObject
{
private $ytInitialPlayerResponse;
//public array $videoDetails;
//public array $microformat;

public function __construct($ytInitialPlayerResponse)
public function isPlayabilityStatusOkay(): bool
{
$this->ytInitialPlayerResponse = $ytInitialPlayerResponse;
return $this->deepGet('playabilityStatus.status') == 'OK';
}

public function all()
public function getVideoDetails(): ?array
{
return $this->ytInitialPlayerResponse;
return $this->deepGet('videoDetails');
}

protected function query($key)
public function getCaptionTracks(): array
{
return Utils::arrayGet($this->ytInitialPlayerResponse, $key);
}

public function isPlayabilityStatusOkay()
{
return $this->query('playabilityStatus.status') == 'OK';
}

public function getVideoDetails()
{
return $this->query('videoDetails');
return (array)$this->deepGet("captions.playerCaptionsTracklistRenderer.captionTracks");
}
}
2 changes: 1 addition & 1 deletion src/Models/JsonObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public function deepGet(string $key, $default = null)

public function toArray(): array
{
return get_object_vars($this);
return $this->_data;
}
}
17 changes: 6 additions & 11 deletions src/Models/VideoInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace YouTube\Models;

class VideoInfo extends AbstractModel
/**
* General class for holding video info. Not all fields required
*/
class VideoInfo
{
// uniquely identifies this video
public ?string $id;
Expand All @@ -12,23 +15,15 @@ class VideoInfo extends AbstractModel

public ?string $title = null;
public ?string $description = null;
public ?string $uploadDate;
public ?\DateTime $uploadDate;

// accessible by public
public ?string $pageUrl;
public ?string $category;

public ?int $viewCount;
public ?int $commentCount;
public ?int $likeCount;
public ?int $dislikeCount;

/**
* @var VideoThumbnail[]
*/
public array $thumbnails;

public ?string $thumbnail;

// in seconds
public ?int $durationSeconds;

Expand Down
15 changes: 15 additions & 0 deletions src/Models/YouTubeCaption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace YouTube\Models;

// TODO: CaptionResource extends YouTubeResource
class YouTubeCaption
{
public ?string $name = null;
public ?string $languageCode = null;

// See: https://developers.google.com/youtube/v3/docs/captions#snippet.trackKind
public ?bool $isAutomatic = null;

public ?string $baseUrl = null;
}
32 changes: 9 additions & 23 deletions src/Models/YouTubeConfigData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,25 @@

namespace YouTube\Models;

use YouTube\Utils\Utils;

class YouTubeConfigData
class YouTubeConfigData extends JsonObject
{
private $data;

public function __construct($data)
{
$this->data = $data;
}

protected function query($key)
{
return Utils::arrayGet($this->data, $key);
}

public function getGoogleVisitorId()
public function getGoogleVisitorId(): ?string
{
return $this->query('VISITOR_DATA');
return $this->deepGet('VISITOR_DATA');
}

public function getClientName()
public function getClientName(): ?string
{
return $this->query('INNERTUBE_CONTEXT_CLIENT_NAME');
return $this->deepGet('INNERTUBE_CONTEXT_CLIENT_NAME');
}

public function getClientVersion()
public function getClientVersion(): ?string
{
return $this->query('INNERTUBE_CONTEXT_CLIENT_VERSION');
return $this->deepGet('INNERTUBE_CONTEXT_CLIENT_VERSION');
}

public function getApiKey()
public function getApiKey(): ?string
{
return $this->query('INNERTUBE_API_KEY');
return $this->deepGet('INNERTUBE_API_KEY');
}
}
32 changes: 0 additions & 32 deletions src/PlayerVideoInfoMapper.php

This file was deleted.

12 changes: 6 additions & 6 deletions src/Responses/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,39 @@ abstract class HttpResponse
/**
* @var Response
*/
private $response;
private Response $response;

// Will become null if response contents cannot be decoded from JSON
private $json;
private ?array $json;

public function __construct(Response $response)
{
$this->response = $response;
$this->json = json_decode($response->body, true);
}

public function getResponse()
public function getResponse(): Response
{
return $this->response;
}

/**
* @return string|null
*/
public function getResponseBody()
public function getResponseBody(): ?string
{
return $this->response->body;
}

/**
* @return array|null
*/
public function getJson()
public function getJson(): ?array
{
return $this->json;
}

public function isStatusOkay()
public function isStatusOkay(): bool
{
return $this->getResponse()->status == 200;
}
Expand Down
11 changes: 9 additions & 2 deletions src/Responses/PlayerApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@

use YouTube\Utils\Utils;

/**
* Response from: /youtubei/v1/player
*/
class PlayerApiResponse extends HttpResponse
{
protected function query($key)
/**
* @param string $key
* @return array|mixed|null
*/
protected function query(string $key)
{
return Utils::arrayGet($this->getJson(), $key);
}

public function getAllFormats()
public function getAllFormats(): array
{
$formats = $this->query('streamingData.formats');

Expand Down
Loading

0 comments on commit aa72002

Please sign in to comment.