Skip to content

Commit

Permalink
Mess detection
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymoulin committed Aug 1, 2019
1 parent dab55c6 commit 088482e
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 38 deletions.
17 changes: 10 additions & 7 deletions src/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use \Exception;
use \DomainException;
use \ZipArchive;

class DownloadCommand extends \Symfony\Component\Console\Command\Command
{
Expand Down Expand Up @@ -55,7 +58,7 @@ protected function execute(InputInterface $input, OutputInterface $output): void
} elseif ($input->getOption(self::URL)) {
$aUrl[] = $input->getOption(self::URL);
} else {
throw new \Exception('Required parameter : ' . implode(' or ', [self::URL, self::LIST_FILE]));
throw new Exception('Required parameter : ' . implode(' or ', [self::URL, self::LIST_FILE]));
}
$oParser = new Parser();
if ($input->getOption(self::DRIVERS)) {
Expand All @@ -76,7 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output): void
$oResult = $oParser->parse($sUrl);
$iNbUrl > 1 && $output->isVerbose() && $progress->advance();
if (!$oResult) {
throw new \DomainException(
throw new DomainException(
"Unable to parse $sUrl." . PHP_EOL . "Consider creating an issue on " .
"https://github.com/jaymoulin/yamete/issues/"
);
Expand All @@ -85,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output): void
$output->writeln("<info>Download $sUrl success!</info>");
!($input->getOption(self::ZIP) && !$input->getOption(self::PDF)) ?: $this->zip($oResult, $output);
!$input->getOption(self::PDF) ?: $this->pdf($oResult, $output);
} catch (\Exception $eException) {
} catch (Exception $eException) {
is_resource($fArtifact) && fputs($fArtifact, $sUrl . PHP_EOL);
if ($iNbUrl == 1) {
throw $eException;
Expand All @@ -104,14 +107,14 @@ protected function execute(InputInterface $input, OutputInterface $output): void
private function zip(ResultIterator $oResult, OutputInterface $output): void
{
$output->writeln('<comment>Creating zip archive</comment>');
$zip = new \ZipArchive();
$zip = new ZipArchive();
$isOpened = false;
$baseName = null;
foreach ($oResult as $sFileName => $sResource) {
$baseName = dirname($sFileName);
if (!$isOpened) {
$isOpened = true;
$zip->open($baseName . '.zip', \ZipArchive::CREATE);
$zip->open($baseName . '.zip', ZipArchive::CREATE);
}
$zip->addFile($sFileName);
}
Expand Down Expand Up @@ -144,7 +147,7 @@ private function pdf(ResultIterator $oResult, OutputInterface $output): void
rmdir($baseName);
$pdf->Output('F', $baseName . '.pdf');
$output->writeln("<comment>PDF created $baseName.pdf</comment>");
} catch (\Exception $eException) {
} catch (Exception $eException) {
$sMessage = $eException->getMessage();
$output->writeln("<error>PDF errored! : $sMessage</error>");
ini_set('memory_limit', $iMemoryLimit);
Expand Down Expand Up @@ -182,7 +185,7 @@ private function download(ResultIterator $oResult, OutputInterface $output): voi
$output->isVerbose() && $progress->advance();
}
if (!$bSuccess) {
throw new \Exception(
throw new Exception(
"No result on download url" . PHP_EOL .
"consider creating an issue https://github.com/jaymoulin/yamete/issues/"
);
Expand Down
6 changes: 3 additions & 3 deletions src/Driver/HentaiFromHell.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Yamete\Driver;

use GuzzleCloudflare\Middleware;
use \GuzzleCloudflare\Middleware;
use \GuzzleHttp\Cookie\FileCookieJar;

class HentaiFromHell extends \Yamete\DriverAbstract
{
Expand All @@ -26,7 +27,7 @@ public function getClient(array $aOptions = []): \GuzzleHttp\Client
{
$oClient = parent::getClient(
[
'cookies' => new \GuzzleHttp\Cookie\FileCookieJar(tempnam('/tmp', __CLASS__)),
'cookies' => new FileCookieJar(tempnam('/tmp', __CLASS__)),
'headers' => ['User-Agent' => self::USER_AGENT],
]
);
Expand All @@ -52,7 +53,6 @@ public function getDownloadables(): array
* @var \PHPHtmlParser\Dom\AbstractNode $oLink
*/
$sLink = $oLink->getAttribute('href');
preg_match('~^https?://(?<domain>[^/]+)~', $sLink, $aDomains);
$oRes = $this->getClient()->request('GET', $sLink);
$sRegexp = '~<meta property="og:image" content="(?<file>[^"]+)"/>~';
if (preg_match($sRegexp, (string)$oRes->getBody(), $aMatches)) {
Expand Down
9 changes: 6 additions & 3 deletions src/Driver/HentaiWorldFr.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Yamete\Driver;

use \AppendIterator;
use \ArrayIterator;

class HentaiWorldFr extends \Yamete\DriverAbstract
{
private $aMatches = [];
Expand Down Expand Up @@ -32,7 +35,7 @@ public function getDownloadables(): array
'/',
['www.' . $this->getDomain(), 'Doujins', explode('/', $this->aMatches['album'])[0]]
) . '/';
return iterator_to_array($this->getForUrl($sUrl, new \AppendIterator()));
return iterator_to_array($this->getForUrl($sUrl, new AppendIterator()));
}

/**
Expand All @@ -42,7 +45,7 @@ public function getDownloadables(): array
* @return \Iterator
* @throws \GuzzleHttp\Exception\GuzzleException
*/
private function getForUrl($sUrl, \AppendIterator $oAppend, $iIndex = 0): \Iterator
private function getForUrl($sUrl, AppendIterator $oAppend, $iIndex = 0): \Iterator
{
$oRes = $this->getClient()->request('GET', $sUrl);
$aReturn = [];
Expand All @@ -63,7 +66,7 @@ private function getForUrl($sUrl, \AppendIterator $oAppend, $iIndex = 0): \Itera
$this->getForUrl($sFilename, $oAppend, $iIndex);
}
}
$oAppend->append(new \ArrayIterator($aReturn));
$oAppend->append(new ArrayIterator($aReturn));
return $oAppend;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Driver/Myreadingmanga.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Yamete\Driver;

use \GuzzleCloudflare\Middleware;
use \GuzzleHttp\Cookie\FileCookieJar;

class Myreadingmanga extends \Yamete\DriverAbstract
{
Expand Down Expand Up @@ -41,7 +42,7 @@ private function getImgListForBody(string $sBody): array
*/
public function getDownloadables(): array
{
$oClient = $this->getClient(['cookies' => new \GuzzleHttp\Cookie\FileCookieJar(tempnam('/tmp', __CLASS__))]);
$oClient = $this->getClient(['cookies' => new FileCookieJar(tempnam('/tmp', __CLASS__))]);
/**
* @var \GuzzleHttp\HandlerStack $oHandler
*/
Expand Down
5 changes: 3 additions & 2 deletions src/Driver/NHentai.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Yamete\Driver;

use GuzzleRetry\GuzzleRetryMiddleware;
use \GuzzleRetry\GuzzleRetryMiddleware;
use \GuzzleHttp\Cookie\FileCookieJar;

class NHentai extends \Yamete\DriverAbstract
{
Expand All @@ -24,7 +25,7 @@ public function canHandle(): bool
*/
public function getDownloadables(): array
{
$oClient = $this->getClient(['cookies' => new \GuzzleHttp\Cookie\FileCookieJar(tempnam('/tmp', __CLASS__))]);
$oClient = $this->getClient(['cookies' => new FileCookieJar(tempnam('/tmp', __CLASS__))]);
/**
* @var \GuzzleHttp\HandlerStack $oHandler
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Driver/PorncomixOnline.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Yamete\Driver;

use \GuzzleCloudflare\Middleware;
use \GuzzleHttp\Cookie\FileCookieJar;

class PorncomixOnline extends \Yamete\DriverAbstract
{
Expand All @@ -26,7 +27,7 @@ public function getClient(array $aOptions = []): \GuzzleHttp\Client
{
$oClient = parent::getClient(
[
'cookies' => new \GuzzleHttp\Cookie\FileCookieJar(tempnam('/tmp', __CLASS__)),
'cookies' => new FileCookieJar(tempnam('/tmp', __CLASS__)),
'headers' => ['User-Agent' => self::USER_AGENT],
]
);
Expand Down
3 changes: 2 additions & 1 deletion src/Driver/TMOHentai.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Yamete\Driver;

use \GuzzleCloudflare\Middleware;
use \GuzzleHttp\Cookie\FileCookieJar;

class TMOHentai extends \Yamete\DriverAbstract
{
Expand Down Expand Up @@ -31,7 +32,7 @@ public function getClient(array $aOptions = []): \GuzzleHttp\Client
{
$oClient = parent::getClient(
[
'cookies' => new \GuzzleHttp\Cookie\FileCookieJar(tempnam('/tmp', __CLASS__)),
'cookies' => new FileCookieJar(tempnam('/tmp', __CLASS__)),
'headers' => ['User-Agent' => self::USER_AGENT],
]
);
Expand Down
6 changes: 4 additions & 2 deletions src/Driver/XXXComicPornCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Yamete\Driver;

use \AppendIterator;

if (!class_exists(XXXComicPornCom::class)) {
class XXXComicPornCom extends \Yamete\DriverAbstract
{
Expand Down Expand Up @@ -37,7 +39,7 @@ public function getDownloadables(): array
$this->sUrl = strpos($this->sUrl, '?') ? substr($this->sUrl, 0, strpos($this->sUrl, '?')) : $this->sUrl;
$oRes = $this->getClient()->request('GET', $this->sUrl);
$this->sUrl .= ($this->sUrl{strlen($this->sUrl) - 1} != '/') ? '/' : '';
$aReturn = new \AppendIterator();
$aReturn = new AppendIterator();
$sBody = (string)$oRes->getBody();
$index = 0;
$oOptions = $this->getDomParser()->load($sBody)->find('.part-select option');
Expand Down Expand Up @@ -80,7 +82,7 @@ private function getForBody(string $sBody, int $iIndex): array
. '-' . basename($sFilename);
$aReturn[$sBasename] = $sFilename;
}
return [new \ArrayIterator($aReturn), $iIndex];
return [new ArrayIterator($aReturn), $iIndex];
}

private function getFolder(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Driver/YaoiMangaOnline.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Yamete\Driver;

use \GuzzleCloudflare\Middleware;
use \GuzzleHttp\Cookie\FileCookieJar;

class YaoiMangaOnline extends \Yamete\DriverAbstract
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public function getClient(array $aOptions = []): \GuzzleHttp\Client
{
$oClient = parent::getClient(
[
'cookies' => new \GuzzleHttp\Cookie\FileCookieJar(tempnam('/tmp', __CLASS__)),
'cookies' => new FileCookieJar(tempnam('/tmp', __CLASS__)),
'headers' => ['User-Agent' => self::USER_AGENT],
]
);
Expand Down
11 changes: 7 additions & 4 deletions src/DriverAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Yamete;

use \GuzzleHttp\Client;
use \PHPHtmlParser\Dom;

abstract class DriverAbstract implements DriverInterface
{
const USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36'
Expand All @@ -10,14 +13,14 @@ abstract class DriverAbstract implements DriverInterface
private $oDomParser;
protected $sUrl;

public function getClient(array $aOptions = []) : \GuzzleHttp\Client
public function getClient(array $aOptions = []) : Client
{
return $this->oClient = $this->oClient ?: new \GuzzleHttp\Client($aOptions);
return $this->oClient = $this->oClient ?: new Client($aOptions);
}

protected function getDomParser() : \PHPHtmlParser\Dom
protected function getDomParser() : Dom
{
return $this->oDomParser = $this->oDomParser ?: new \PHPHtmlParser\Dom;
return $this->oDomParser = $this->oDomParser ?: new Dom;
}

public function setUrl(string $sUrl) : DriverInterface
Expand Down
20 changes: 11 additions & 9 deletions src/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Yamete;

use \Exception;
use \Iterator;

class PDF extends \FPDF
{

Expand All @@ -26,7 +29,7 @@ private function resizeToFit(string $imgFilename): array
{
list($width, $height) = getimagesize($imgFilename);
if (!$width || !$height) {
throw new \Exception('Not an image file');
throw new Exception('Not an image file');
}
$width = $this->pixelsToMM($width);
$height = $this->pixelsToMM($height);
Expand All @@ -37,10 +40,10 @@ private function resizeToFit(string $imgFilename): array
}

/**
* @param \Iterator $oList
* @throws \Exception
* @param Iterator $oList
* @throws Exception
*/
public function createFromList(\Iterator $oList): void
public function createFromList(Iterator $oList): void
{
foreach ($oList as $sFilename => $sResource) {
list($width, $height) = getimagesize($sFilename);
Expand All @@ -59,13 +62,12 @@ private function fullSizeImage(string $sFileName): void
try {
$this->Image($sFileName, 0, 0, $height, $width);
} catch (\Exception $e) {
if (strpos($e->getMessage(), 'Not a PNG file') !== false) {
$sNewFilename = str_replace('.png', '.jpg', $sFileName);
rename($sFileName, $sNewFilename);
$this->Image($sNewFilename, 0, 0, $height, $width);
} else {
if (strpos($e->getMessage(), 'Not a PNG file') === false) {
throw $e;
}
$sNewFilename = str_replace('.png', '.jpg', $sFileName);
rename($sFileName, $sNewFilename);
$this->Image($sNewFilename, 0, 0, $height, $width);
}
}
}
10 changes: 7 additions & 3 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Yamete;

use \UnexpectedValueException;
use \crodas\ClassInfo\ClassInfo;
use \RuntimeException;

class Parser
{
/**
Expand All @@ -23,16 +27,16 @@ public function addDriverDirectory(string $sDirectory): Parser
{
substr($sDirectory, -1) != '/' && $sDirectory .= '/';
if (!is_dir($sDirectory)) {
throw new \UnexpectedValueException($sDirectory . ' is not a valid directory');
throw new UnexpectedValueException($sDirectory . ' is not a valid directory');
}
foreach (glob($sDirectory . '*.php') as $sDriver) {
$aClasses = (new \crodas\ClassInfo\ClassInfo($sDriver))->getClasses();
$aClasses = (new ClassInfo($sDriver))->getClasses();
if (count($aClasses)) {
$sClass = (string)current($aClasses);
include $sDriver;
$oDriver = new $sClass;
if (!$oDriver instanceof DriverInterface) {
throw new \RuntimeException("Driver $sClass ($sDriver) must implements " . DriverInterface::class);
throw new RuntimeException("Driver $sClass ($sDriver) must implements " . DriverInterface::class);
}
$this->aDrivers[] = $oDriver;
}
Expand Down
4 changes: 3 additions & 1 deletion src/ResultIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Yamete;

use \ArrayIterator;

/**
* Class ResultIterator
* @package Yamete
Expand All @@ -15,7 +17,7 @@ class ResultIterator implements \Iterator, \Countable
public function __construct(DriverInterface $oDriver)
{
$this->oDriver = $oDriver;
$this->oIterator = new \ArrayIterator($oDriver->getDownloadables());
$this->oIterator = new ArrayIterator($oDriver->getDownloadables());
}

public function current(): Downloadable
Expand Down

0 comments on commit 088482e

Please sign in to comment.