Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Commit

Permalink
Add logger support in SignaturDecipher, Format and VideoInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Aug 22, 2017
1 parent 8df1b2a commit 5ffa5c3
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 65 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ cache/*
composer.phar
composer.lock
config/custom.php
Deciphers.log
logs/*
vendor
63 changes: 37 additions & 26 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/Application/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}
5 changes: 5 additions & 0 deletions src/Application/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/Application/ResultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
35 changes: 31 additions & 4 deletions src/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -37,6 +42,8 @@ public static function createFromArray(

private $data = [];

private $data_parsed = false;

private $raw_data = [];

/**
Expand Down Expand Up @@ -74,8 +81,6 @@ private function __construct(VideoInfo $video_info, array $data, array $config)
}

$this->raw_data = $data;

$this->parseUrl();
}

/**
Expand All @@ -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']))
Expand Down Expand Up @@ -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 )
Expand All @@ -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;
}

/**
Expand All @@ -160,6 +173,8 @@ public function getVideoId()
*/
public function getUrl()
{
$this->parseUrl();

return $this->data['url'];
}

Expand All @@ -170,6 +185,8 @@ public function getUrl()
*/
public function getItag()
{
$this->parseUrl();

return $this->data['itag'];
}

Expand All @@ -180,6 +197,8 @@ public function getItag()
*/
public function getQuality()
{
$this->parseUrl();

return $this->data['quality'];
}

Expand All @@ -190,6 +209,8 @@ public function getQuality()
*/
public function getType()
{
$this->parseUrl();

return $this->data['type'];
}

Expand All @@ -200,6 +221,8 @@ public function getType()
*/
public function getExpires()
{
$this->parseUrl();

return $this->data['expires'];
}

Expand All @@ -210,6 +233,8 @@ public function getExpires()
*/
public function getIpbits()
{
$this->parseUrl();

return $this->data['ipbits'];
}

Expand All @@ -220,6 +245,8 @@ public function getIpbits()
*/
public function getIp()
{
$this->parseUrl();

return $this->data['ip'];
}
}
31 changes: 31 additions & 0 deletions src/Logger/Handler/NullHandler.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Logger/HandlerAwareLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
40 changes: 40 additions & 0 deletions src/Logger/LoggerAwareTrait.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 5ffa5c3

Please sign in to comment.