refactor: New PlaylistArchiveVideo class

Cleaner way to handle PlaylistArchiveStream info about videos
This commit is contained in:
Pierre Rudloff 2019-04-21 00:56:12 +02:00
parent f9bf3b8d47
commit ddc27a8a2c
2 changed files with 84 additions and 35 deletions

View file

@ -19,11 +19,11 @@ use stdClass;
class PlaylistArchiveStream extends TarArchive implements StreamInterface class PlaylistArchiveStream extends TarArchive implements StreamInterface
{ {
/** /**
* Files to add in the archive. * videos to add in the archive.
* *
* @var array[] * @var PlaylistArchiveVideo[]
*/ */
private $files = []; private $videos = [];
/** /**
* Stream used to store data before it is sent to the browser. * Stream used to store data before it is sent to the browser.
@ -47,11 +47,11 @@ class PlaylistArchiveStream extends TarArchive implements StreamInterface
private $download; private $download;
/** /**
* Current file position in $files array. * Current video being streamed to the archive.
* *
* @var int * @var int
*/ */
private $curFile = 0; private $curVideo;
/** /**
* Video format to download. * Video format to download.
@ -78,12 +78,7 @@ class PlaylistArchiveStream extends TarArchive implements StreamInterface
$this->buffer = $buffer; $this->buffer = $buffer;
} }
foreach ($video->entries as $entry) { foreach ($video->entries as $entry) {
$this->files[] = [ $this->videos[] = new PlaylistArchiveVideo($entry->url);
'url' => $entry->url,
'headersSent' => false,
'complete' => false,
'stream' => null,
];
} }
} }
@ -211,8 +206,8 @@ class PlaylistArchiveStream extends TarArchive implements StreamInterface
{ {
$string = ''; $string = '';
foreach ($this->files as $file) { foreach ($this->videos as $file) {
$string .= $file['url']; $string .= $file->url;
} }
return $string; return $string;
@ -248,8 +243,8 @@ class PlaylistArchiveStream extends TarArchive implements StreamInterface
*/ */
public function eof() public function eof()
{ {
foreach ($this->files as $file) { foreach ($this->videos as $file) {
if (!$file['complete']) { if (!$file->complete) {
return false; return false;
} }
} }
@ -266,25 +261,30 @@ class PlaylistArchiveStream extends TarArchive implements StreamInterface
*/ */
public function read($count) public function read($count)
{ {
if (!$this->files[$this->curFile]['headersSent']) { if (isset($this->curVideo)) {
$urls = $this->download->getURL($this->files[$this->curFile]['url'], $this->format); if (isset($this->curVideo->stream)) {
if (!$this->curVideo->stream->eof()) {
$this->stream_file_part($this->curVideo->stream->read($count));
} elseif (!$this->curVideo->complete) {
$this->complete_file_stream();
$this->curVideo->complete = true;
} else {
$this->curVideo = next($this->videos);
}
} else {
$urls = $this->download->getURL($this->curVideo->url, $this->format);
$response = $this->client->request('GET', $urls[0], ['stream' => true]); $response = $this->client->request('GET', $urls[0], ['stream' => true]);
$contentLengthHeaders = $response->getHeader('Content-Length'); $contentLengthHeaders = $response->getHeader('Content-Length');
$this->init_file_stream_transfer( $this->init_file_stream_transfer(
$this->download->getFilename($this->files[$this->curFile]['url'], $this->format), $this->download->getFilename($this->curVideo->url, $this->format),
$contentLengthHeaders[0] $contentLengthHeaders[0]
); );
$this->files[$this->curFile]['headersSent'] = true; $this->curVideo->stream = $response->getBody();
$this->files[$this->curFile]['stream'] = $response->getBody(); }
} elseif (!$this->files[$this->curFile]['stream']->eof()) { } else {
$this->stream_file_part($this->files[$this->curFile]['stream']->read($count)); $this->curVideo = current($this->videos);
} elseif (!$this->files[$this->curFile]['complete']) {
$this->complete_file_stream();
$this->files[$this->curFile]['complete'] = true;
} elseif (isset($this->files[$this->curFile])) {
$this->curFile += 1;
} }
return fread($this->buffer, $count); return fread($this->buffer, $count);
@ -300,9 +300,9 @@ class PlaylistArchiveStream extends TarArchive implements StreamInterface
if (is_resource($this->buffer)) { if (is_resource($this->buffer)) {
fclose($this->buffer); fclose($this->buffer);
} }
foreach ($this->files as $file) { foreach ($this->videos as $file) {
if (is_resource($file['stream'])) { if (is_resource($file->stream)) {
fclose($file['stream']); fclose($file->stream);
} }
} }
} }

View file

@ -0,0 +1,49 @@
<?php
/**
* PlaylistArchiveVideo class.
*/
namespace Alltube;
use Barracuda\ArchiveStream\TarArchive;
use GuzzleHttp\Client;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use stdClass;
/**
* Video streamed to a PlaylistArchiveStream.
*/
class PlaylistArchiveVideo
{
/**
* Video page URL.
*
* @var string
*/
public $url;
/**
* Has the video been streaded entirely ?
*
* @var bool
*/
public $complete = false;
/**
* popen stream containing the video.
*
* @var resource
*/
public $stream;
/**
* PlaylistArchiveVideo constructor.
*
* @param string $url Video page URL
*/
public function __construct($url)
{
$this->url = $url;
}
}