Skip to content

Commit

Permalink
PSR2 support with some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosflorencio committed Feb 24, 2015
1 parent e0766ec commit 17242cd
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 151 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea

config.ini
vendor
vendor
139 changes: 90 additions & 49 deletions App/Downloader.php
Original file line number Diff line number Diff line change
@@ -1,109 +1,150 @@
<?php namespace App;
<?php
/**
* Main cycle of the app
*/
namespace App;

use App\Exceptions\LoginException;
use App\Http\Resolver;
use App\System\Controller;
use App\Utils\Utils;
use GuzzleHttp\Client;
use League\Flysystem\Filesystem;
use League\Flysystem\Util;
use Ubench;

/**
* Class Downloader
* @package App
*/
class Downloader
{
/**
* Http resolver object
* @var Resolver
*/
private $client;

/**
* System object
* @var Controller
*/
private $system;

/**
* Ubench lib
* @var Ubench
*/
private $bench;

/**
* Current lesson number
* @var int
*/
public static $currentLessonNumber;

/**
* Receives dependencies
*
* @param Client $client
* @param Filesystem $system
* @param Ubench $bench
*/
function __construct(Client $client, Filesystem $system, Ubench $bench)
public function __construct(Client $client, Filesystem $system, Ubench $bench)
{
$this->client = new Resolver($client, $bench);
$this->system = new Controller($system);
$this->bench = $bench;
}

/**
* All the logic
*
* @param $options
*/
public function start($options)
{
Utils::box('Starting Collecting the data');

$this->bench->start();
$localLessons = $this->system->getAllLessons();
$allLessonsOnline = $this->client->getAllLessons();
$this->bench->end();

//Magic to get what to download
$diff = Utils::resolveFaultyLessons($allLessonsOnline, $localLessons);

$new_lessons = Utils::countLessons($diff);
$new_episodes = Utils::countEpisodes($diff);

Utils::write(sprintf("%d new lessons and %d episodes. %s elapsed with %s of memory usage.",
$new_lessons,
$new_episodes,
$this->bench->getTime(),
$this->bench->getMemoryUsage())
);

Utils::box('Authenticating');
$this->doAuth($options);

if($new_lessons > 0) {
$this->system->createFolderIfNotExists(LESSONS_FOLDER);
Utils::box('Downloading Lessons');
foreach ($diff['lessons'] as $lesson) {
$this->client->downloadLesson($lesson);
try {
$counter = [
'series' => 1,
'lessons' => 1
];

Utils::box('Starting Collecting the data');

$this->bench->start();
$localLessons = $this->system->getAllLessons();
$allLessonsOnline = $this->client->getAllLessons();
$this->bench->end();

//Magic to get what to download
$diff = Utils::resolveFaultyLessons($allLessonsOnline, $localLessons);

$new_lessons = Utils::countLessons($diff);
$new_episodes = Utils::countEpisodes($diff);

Utils::write(sprintf("%d new lessons and %d episodes. %s elapsed with %s of memory usage.",
$new_lessons,
$new_episodes,
$this->bench->getTime(),
$this->bench->getMemoryUsage())
);

Utils::box('Authenticating');

$this->doAuth($options);

if ($new_lessons > 0) {
$this->system->createFolderIfNotExists(LESSONS_FOLDER);
Utils::box('Downloading Lessons');
foreach ($diff['lessons'] as $lesson) {
$this->client->downloadLesson($lesson);
Utils::write(sprintf("Current: %d of %d total. Left: %d",
$counter['lessons']++,
$new_lessons,
$new_lessons - $counter['lessons'] + 1
));
}
}
}

if($new_episodes > 0) {
$this->system->createFolderIfNotExists(SERIES_FOLDER);
Utils::box('Downloading Series');
foreach ($diff['series'] as $serie => $episodes) {
$this->system->createSerieFolderIfNotExists($serie);
foreach ($episodes as $episode) {
$this->client->downloadSerieEpisode($serie, $episode);
if ($new_episodes > 0) {
$this->system->createFolderIfNotExists(SERIES_FOLDER);
Utils::box('Downloading Series');
foreach ($diff['series'] as $serie => $episodes) {
$this->system->createSerieFolderIfNotExists($serie);
foreach ($episodes as $episode) {
$this->client->downloadSerieEpisode($serie, $episode);
Utils::write(sprintf("Current: %d of %d total. Left: %d",
$counter['series']++,
$new_episodes,
$new_episodes - $counter['series'] + 1
));
}
}
}
}

Utils::writeln(sprintf("Finished! %d new lessons and %d new episodes.",
$new_lessons,
$new_episodes
));
Utils::writeln(sprintf("Finished! %d new lessons and %d new episodes.",
$new_lessons,
$new_episodes
));
} catch (LoginException $e) {
Utils::write("Your login details are wrong!");
} catch (SubscriptionNotActiveException $e) {
Utils::write('Your subscription is not active!');
}
}

/**
* Tries to login
* Tries to login.
*
* @param $options
*
* @throws \Exception
*/
public function doAuth($options)
{
if (!$this->client->doAuth($options['email'], $options['password'])) {
Utils::write("Your login details are wrong!");
die();
throw new LoginException("Can't do the login..");
}
Utils::write("Successfull!");
}

}
}
15 changes: 15 additions & 0 deletions App/Exceptions/LoginException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Login Exception
*/
namespace App\Exceptions;

use Exception;

/**
* Class LoginException
* @package App\Exceptions
*/
class LoginException extends Exception
{
}
15 changes: 15 additions & 0 deletions App/Exceptions/SubscriptionNotActiveException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Subscription Not Active Exception
*/
namespace App\Exceptions;

use Exception;

/**
* Class SubscriptionNotActiveException
* @package App\Exceptions
*/
class SubscriptionNotActiveException extends Exception
{
}
34 changes: 21 additions & 13 deletions App/Html/Parser.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<?php namespace App\Html;
<?php
/**
* Dom Parser
*/
namespace App\Html;

use Symfony\Component\DomCrawler\Crawler;

/**
* Class Parser
* @package App\Html
*/
class Parser
{

/**
* Parses the html and adds the lessons the the array
* Parses the html and adds the lessons the the array.
*
* @param $html
* @param $array
Expand All @@ -18,18 +25,18 @@ public static function getAllLessons($html, &$array)
$parser->filter('a.js-lesson-title')->each(function (Crawler $node, $i) use (&$array) {
$link = $node->attr('href');

if (preg_match('/' . LARACASTS_LESSONS_PATH . '\/(.+)/', $link, $matches)) { // lesson
if (preg_match('/'.LARACASTS_LESSONS_PATH.'\/(.+)/', $link, $matches)) { // lesson
$array['lessons'][] = $matches[1];
}

if (preg_match('/' . LARACASTS_SERIES_PATH . '\/(.+)\/episodes\/(\d+)/', $link, $matches)) { // lesson
$array['series'][$matches[1]][] = (int)$matches[2];
if (preg_match('/'.LARACASTS_SERIES_PATH.'\/(.+)\/episodes\/(\d+)/', $link, $matches)) { // lesson
$array['series'][$matches[1]][] = (int) $matches[2];
}
});
}

/**
* Determines if there is next page, false if not or the link
* Determines if there is next page, false if not or the link.
*
* @param $html
*
Expand All @@ -40,14 +47,15 @@ public static function hasNextPage($html)
$parser = new Crawler($html);

$node = $parser->filter('[rel=next]');
if ($node->count() > 0)
if ($node->count() > 0) {
return $node->attr('href');
}

return FALSE;
return false;
}

/**
* Gets the token input
* Gets the token input.
*
* @param $html
*
Expand All @@ -61,7 +69,7 @@ public static function getToken($html)
}

/**
* Gets the download link
* Gets the download link.
*
* @param $html
*/
Expand All @@ -73,7 +81,7 @@ public static function getDownloadLink($html)
}

/**
* Extracts the name of the episode
* Extracts the name of the episode.
*
* @param $html
*
Expand All @@ -85,4 +93,4 @@ public static function getNameOfEpisode($html)

return trim($parser->filter('.lesson-title')->text());
}
}
}
Loading

0 comments on commit 17242cd

Please sign in to comment.