Skip to content

Instantly share code, notes, and snippets.

@thedava
Created August 19, 2017 20:48
Show Gist options
  • Save thedava/0b3e73f9b61d6387d4f71cc6d1a065e7 to your computer and use it in GitHub Desktop.
Save thedava/0b3e73f9b61d6387d4f71cc6d1a065e7 to your computer and use it in GitHub Desktop.
Download all icon s from a iconset of iconarchive.com
<?php
namespace Application\Console\Command\Remote;
use DavaHome\Console\Command\AbstractCommand;
use GuzzleHttp\Client;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DomCrawler\Crawler;
use Zend\Uri\Http;
use Zend\Uri\Uri;
class IconArchiveDownloaderCommand extends AbstractCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('remote:icon-archive-downloader')
->setDescription('Download a icon set from iconarchive.com')
->addArgument('url', InputArgument::REQUIRED, 'The full url of the icon set')
->addArgument('path', InputArgument::REQUIRED, 'The downloader folder');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$url = $input->getArgument('url');
$downloadPath = $input->getArgument('path');
$client = new Client([
'cookies' => true,
]);
// Main page
$uri = new Http($url);
$dom = new Crawler($client->get($url)->getBody()->getContents());
// Display the icon set name
$name = $dom->filter('h1')->text();
$output->writeln('<cyan>' . trim($name) . '</cyan>');
for ($currentPage = 1, $totalPageCount = 1; $currentPage <= $totalPageCount; $currentPage++) {
// Navigate to page
if (!$this->switchPage($output, $currentPage, $dom, $client, clone $uri)) {
return 1;
}
// Determine total page count
$totalPageCount = $this->determineTotalPageCount($dom);
$dom->filter('div.icondetail')->each(function (Crawler $element) use ($output, &$client, $downloadPath) {
$element->filter('.detail')->filter('a')->each(function (Crawler $element) use ($output, &$client, $downloadPath) {
if ($element->text() === 'PNG') {
$url = $element->attr('href');
$fileName = basename($url);
$output->writeln('Downloading ' . $fileName . '...');
$client->request('GET', $url, ['sink' => $downloadPath . '/' . $fileName]);
}
});
});
}
$output->writeln('<green>done</green>');
return 0;
}
/**
* @param OutputInterface $output
* @param string|int $pageNum
* @param Crawler $page
* @param Client $client
* @param Uri $uri
*
* @return bool
*/
protected function switchPage(OutputInterface $output, $pageNum, Crawler &$page, Client &$client, Uri $uri)
{
$handled = false;
$pageNum = (string)$pageNum;
$page->filter('.pagination')
->filter('a')
->each(function (Crawler $element) use ($output, &$page, &$handled, $uri, $client, $pageNum) {
if ($handled) {
return;
}
if ($element->text() == $pageNum) {
// Navigate to page
if ($element->attr('class') !== 'num-active') {
$uri->setPath($element->attr('href'));
$output->writeln('<yellow>Going to page ' . $pageNum . ':</yellow> ' . $uri->toString());
$page = new Crawler($client->get($uri->toString())->getBody()->getContents());
} else {
// We are already on the page
$output->writeln('<green>Already on the correct page</green>');
}
$handled = true;
}
});
// Abort if page switch failed
if (!$handled) {
$output->writeln('<red>Page navigation couldn\'t be handled!</red>');
return false;
}
return true;
}
/**
* @param Crawler $page
*
* @return int
*/
protected function determineTotalPageCount(Crawler $page)
{
$pageCount = 1;
$page->filter('.pagination')
->filter('a')
->each(function (Crawler $element) use (&$pageCount) {
$text = $element->text();
if (is_numeric($text)) {
$text = (int)$text;
if ($text > $pageCount) {
$pageCount = $text;
}
}
});
return $pageCount;
}
}
@thedava
Copy link
Author

thedava commented Aug 19, 2017

AbstractCommand is a class from the davahome/console repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment