-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0766ec
commit 17242cd
Showing
11 changed files
with
310 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.idea | ||
|
||
config.ini | ||
vendor | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.