Permalink
Browse files

Add logger support in SignaturDecipher, Format and VideoInfo

  • Loading branch information...
Art4 committed Aug 22, 2017
1 parent 8df1b2a commit 5ffa5c32ec43b854d9779c763361db1e7f8598d8
View
@@ -2,6 +2,5 @@ cache/*
composer.phar
composer.lock
config/custom.php
Deciphers.log
logs/*
vendor
View
@@ -82,37 +82,48 @@ function($custom = 'custom')
$container->set('cache', $cache);
// Create Logger
$now = new \DateTime('now', new \DateTimeZone($config->get('default_timezone')));
$filepath = sprintf(
'%s' . \DIRECTORY_SEPARATOR . '%s',
__DIR__ . \DIRECTORY_SEPARATOR . 'logs',
$now->format('Y')
$logger = new \YoutubeDownloader\Logger\HandlerAwareLogger(
new \YoutubeDownloader\Logger\Handler\NullHandler()
);
if ( ! file_exists($filepath) )
if ( $config->get('debug') === true )
{
mkdir($filepath);
# code...
$now = new \DateTime('now', new \DateTimeZone($config->get('default_timezone')));
$filepath = sprintf(
'%s' . \DIRECTORY_SEPARATOR . '%s',
__DIR__ . \DIRECTORY_SEPARATOR . 'logs',
$now->format('Y')
);
if ( ! file_exists($filepath) )
{
mkdir($filepath);
}
$stream = fopen(
$filepath . \DIRECTORY_SEPARATOR . $now->format('Y-m-d') . '.log',
'a+'
);
if ( is_resource($stream) )
{
$handler = new \YoutubeDownloader\Logger\Handler\StreamHandler($stream, [
\YoutubeDownloader\Logger\LogLevel::EMERGENCY,
\YoutubeDownloader\Logger\LogLevel::ALERT,
\YoutubeDownloader\Logger\LogLevel::CRITICAL,
\YoutubeDownloader\Logger\LogLevel::ERROR,
\YoutubeDownloader\Logger\LogLevel::WARNING,
\YoutubeDownloader\Logger\LogLevel::NOTICE,
\YoutubeDownloader\Logger\LogLevel::INFO,
\YoutubeDownloader\Logger\LogLevel::DEBUG,
]);
$logger->addHandler($handler);
}
}
$stream = fopen(
$filepath . \DIRECTORY_SEPARATOR . $now->format('Y-m-d') . '.log',
'a+'
);
$handler = new \YoutubeDownloader\Logger\Handler\StreamHandler($stream, [
\YoutubeDownloader\Logger\LogLevel::EMERGENCY,
\YoutubeDownloader\Logger\LogLevel::ALERT,
\YoutubeDownloader\Logger\LogLevel::CRITICAL,
\YoutubeDownloader\Logger\LogLevel::ERROR,
\YoutubeDownloader\Logger\LogLevel::WARNING,
\YoutubeDownloader\Logger\LogLevel::NOTICE,
\YoutubeDownloader\Logger\LogLevel::INFO,
\YoutubeDownloader\Logger\LogLevel::DEBUG,
]);
$logger = new \YoutubeDownloader\Logger\HandlerAwareLogger($handler);
$container->set('logger', $logger);
return $container;
View
@@ -67,5 +67,7 @@ public function runWithRoute($route)
$controller = $controller_factory->make($route, $this);
$controller->execute();
$this->getContainer()->get('logger')->debug('Controller executed. App closed.');
}
}
@@ -69,6 +69,11 @@ public function execute()
$video_info = \YoutubeDownloader\VideoInfo::createFromStringWithConfig($video_info_string, $config);
$video_info->setCache($this->get('cache'));
if ( $video_info instanceOf \YoutubeDownloader\Logger\LoggerAware )
{
$video_info->setLogger($this->get('logger'));
}
try
{
$mp3_info = $toolkit->getDownloadMP3($video_info, $config);
@@ -63,6 +63,11 @@ public function execute()
$video_info = \YoutubeDownloader\VideoInfo::createFromStringWithConfig($video_info_string, $config);
$video_info->setCache($this->get('cache'));
if ( $video_info instanceOf \YoutubeDownloader\Logger\LoggerAware )
{
$video_info->setLogger($this->get('logger'));
}
if ($video_info->getStatus() == 'fail')
{
$message = 'Error in video ID: ' . $video_info->getErrorReason();
View
@@ -2,11 +2,16 @@
namespace YoutubeDownloader;
use YoutubeDownloader\Logger\LoggerAware;
use YoutubeDownloader\Logger\LoggerAwareTrait;
/**
* a video format
*/
class Format
class Format implements LoggerAware
{
use LoggerAwareTrait;
/**
* Creates a Stream from array
*
@@ -37,6 +42,8 @@ public static function createFromArray(
private $data = [];
private $data_parsed = false;
private $raw_data = [];
/**
@@ -74,8 +81,6 @@ private function __construct(VideoInfo $video_info, array $data, array $config)
}
$this->raw_data = $data;
$this->parseUrl();
}
/**
@@ -85,6 +90,11 @@ private function __construct(VideoInfo $video_info, array $data, array $config)
*/
private function parseUrl()
{
if ( $this->data_parsed === true )
{
return;
}
parse_str(urldecode($this->data['url']), $url_info);
if (isset($this->raw_data['bitrate']))
@@ -122,7 +132,8 @@ private function parseUrl()
$sig = SignatureDecipher::decipherSignatureWithRawPlayerScript(
$decipherScript,
$this->raw_data['s']
$this->raw_data['s'],
$this->getLogger()
);
if ( strpos($this->raw_data['url'], 'ratebypass=') === false )
@@ -141,6 +152,8 @@ private function parseUrl()
$this->data['expires'] = isset($url_info['expire']) ? date("G:i:s T", $url_info['expire']) : '';
$this->data['ipbits'] = isset($url_info['ipbits']) ? $url_info['ipbits'] : '';
$this->data['ip'] = isset($url_info['ip']) ? $url_info['ip'] : '';
$this->data_parsed = true;
}
/**
@@ -160,6 +173,8 @@ public function getVideoId()
*/
public function getUrl()
{
$this->parseUrl();
return $this->data['url'];
}
@@ -170,6 +185,8 @@ public function getUrl()
*/
public function getItag()
{
$this->parseUrl();
return $this->data['itag'];
}
@@ -180,6 +197,8 @@ public function getItag()
*/
public function getQuality()
{
$this->parseUrl();
return $this->data['quality'];
}
@@ -190,6 +209,8 @@ public function getQuality()
*/
public function getType()
{
$this->parseUrl();
return $this->data['type'];
}
@@ -200,6 +221,8 @@ public function getType()
*/
public function getExpires()
{
$this->parseUrl();
return $this->data['expires'];
}
@@ -210,6 +233,8 @@ public function getExpires()
*/
public function getIpbits()
{
$this->parseUrl();
return $this->data['ipbits'];
}
@@ -220,6 +245,8 @@ public function getIpbits()
*/
public function getIp()
{
$this->parseUrl();
return $this->data['ip'];
}
}
@@ -0,0 +1,31 @@
<?php
namespace YoutubeDownloader\Logger\Handler;
/**
* a handler instance that handles no entries
*/
class NullHandler implements Handler
{
/**
* Check if this handler handels a log level
*
* @param string $level A valid log level from LogLevel class
* @return boolean
*/
public function handles($level)
{
return false;
}
/**
* Handle an entry
*
* @param Entry $entry
* @return boolean
*/
public function handle(Entry $entry)
{
return false;
}
}
@@ -161,7 +161,7 @@ public function log($level, $message, array $context = array())
* @param YoutubeDownloader\Logger\Handler\Handler $handler
* @return void
*/
private function addHandler(Handler $handler)
public function addHandler(Handler $handler)
{
$this->handlers[] = $handler;
}
@@ -0,0 +1,40 @@
<?php
namespace YoutubeDownloader\Logger;
/**
* Trait for logger-aware instances
*/
trait LoggerAwareTrait
{
/**
* @var YoutubeDownloader\Logger\Logger
*/
protected $logger;
/**
* Sets a logger instance on the object
*
* @param Logger $logger
* @return null
*/
public function setLogger(Logger $logger)
{
$this->logger = $logger;
}
/**
* Gets a logger instance
*
* @return Logger
*/
public function getLogger()
{
if ( $this->logger === null )
{
$this->logger = new NullLogger;
}
return $this->logger;
}
}
Oops, something went wrong.

0 comments on commit 5ffa5c3

Please sign in to comment.