Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosflorencio committed Feb 22, 2015
0 parents commit 3391ef4
Show file tree
Hide file tree
Showing 10 changed files with 1,214 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea

config.ini
vendor
108 changes: 108 additions & 0 deletions App/Downloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php namespace App;

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
{
/**
* @var Resolver
*/
private $client;

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

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

/**
* @var int
*/
public static $currentLessonNumber;

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

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) {
Utils::box('Downloading Lessons');
foreach ($diff['lessons'] as $lesson) {
$this->client->downloadLesson($lesson);
}
}

if($new_episodes > 0) {
Utils::box('Downloading Series');
foreach ($diff['series'] as $serie => $episodes) {
$this->system->createSerieFolderIfNotExists($serie);
foreach ($episodes as $episode) {
$this->client->downloadSerieEpisode($serie, $episode);
}
}
}

Utils::writeln(sprintf("Finished! %d new lessons and %d new episodes.",
$new_lessons,
$new_episodes
));
}

/**
* Tries to login
*
* @param $options
*/
public function doAuth($options)
{
if (!$this->client->doAuth($options['email'], $options['password'])) {
Utils::write("Your login details are wrong!");
die();
}
Utils::write("Successfull!");
}


}
88 changes: 88 additions & 0 deletions App/Html/Parser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php namespace App\Html;

use Symfony\Component\DomCrawler\Crawler;

class Parser
{

/**
* Parses the html and adds the lessons the the array
*
* @param $html
* @param $array
*/
public static function getAllLessons($html, &$array)
{
$parser = new Crawler($html);

$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
$array['lessons'][] = $matches[1];
}

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
*
* @param $html
*
* @return bool|string
*/
public static function hasNextPage($html)
{
$parser = new Crawler($html);

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

return FALSE;
}

/**
* Gets the token input
*
* @param $html
*
* @return string
*/
public static function getToken($html)
{
$parser = new Crawler($html);

return $parser->filter("input[name=_token]")->attr('value');
}

/**
* Gets the download link
*
* @param $html
*/
public static function getDownloadLink($html)
{
preg_match('/(\/downloads\/\d+\?type=\w+)/', $html, $matches);

return $matches[0];
}

/**
* Extracts the name of the episode
*
* @param $html
*
* @return string
*/
public static function getNameOfEpisode($html)
{
$parser = new Crawler($html);

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

0 comments on commit 3391ef4

Please sign in to comment.