1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\IoBundle\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use TreeHouse\IoBundle\Entity\Scraper; |
11
|
|
|
use TreeHouse\IoBundle\Entity\Scraper as ScraperEntity; |
12
|
|
|
use TreeHouse\IoBundle\Model\SourceInterface; |
13
|
|
|
use TreeHouse\IoBundle\Scrape\Exception\CrawlException; |
14
|
|
|
use TreeHouse\IoBundle\Scrape\SourceRevisitor; |
15
|
|
|
use TreeHouse\IoBundle\Source\SourceManagerInterface; |
16
|
|
|
|
17
|
|
|
class ScrapeRevisitUrlsCommand extends Command |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ManagerRegistry |
21
|
|
|
*/ |
22
|
|
|
protected $doctrine; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var SourceManagerInterface |
26
|
|
|
*/ |
27
|
|
|
protected $sourceManager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var SourceRevisitor |
31
|
|
|
*/ |
32
|
|
|
protected $revisitor; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ManagerRegistry $doctrine |
36
|
|
|
* @param SourceManagerInterface $sourceManager |
37
|
|
|
* @param SourceRevisitor $revisitor |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function __construct(ManagerRegistry $doctrine, SourceManagerInterface $sourceManager, SourceRevisitor $revisitor) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$this->doctrine = $doctrine; |
42
|
|
|
$this->sourceManager = $sourceManager; |
43
|
|
|
$this->revisitor = $revisitor; |
44
|
|
|
|
45
|
|
|
parent::__construct(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this->setName('io:scrape:revisit-urls'); |
54
|
|
|
$this->setDescription('Revisits earlier scraped sources to see if they still exist'); |
55
|
|
|
$this->addOption('scraper', InputOption::VALUE_OPTIONAL, 'Limit the sources to a specific scraper id'); |
56
|
|
|
$this->addOption( |
57
|
|
|
'async', |
58
|
|
|
'a', |
59
|
|
|
InputOption::VALUE_NONE, |
60
|
|
|
'Whether to revisit asynchronous. Doing so will queue sources, rather them revisiting them right away' |
61
|
|
|
); |
62
|
|
|
$this->addOption('no-limit', null, InputOption::VALUE_NONE, 'Disables the rate limit'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
69
|
|
|
{ |
70
|
|
|
$async = $input->getOption('async'); |
71
|
|
|
$noLimit = $input->getOption('no-limit'); |
72
|
|
|
$scrapers = $this->findScrapers($input->getArgument('scraper')); |
|
|
|
|
73
|
|
|
|
74
|
|
|
foreach ($scrapers as $scraperEntity) { |
75
|
|
|
$date = new \DateTime(sprintf('-%d hours', $scraperEntity->getRevisitFrequency())); |
76
|
|
|
|
77
|
|
|
$builder = $this->sourceManager->getRepository()->queryByScraperAndUnvisitedSince($scraperEntity, $date); |
78
|
|
|
foreach ($builder->getQuery()->iterate() as list($source)) { |
79
|
|
|
/* @var SourceInterface $source */ |
80
|
|
|
try { |
81
|
|
|
$output->writeln(sprintf('Revisiting <info>%s</info>', $source->getOriginalUrl())); |
82
|
|
|
|
83
|
|
|
$this->revisitor->revisit($source, $async, $noLimit); |
84
|
|
|
} catch (CrawlException $e) { |
85
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param integer[] $ids |
93
|
|
|
* |
94
|
|
|
* @return ScraperEntity[] |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
protected function findScrapers(array $ids) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$repo = $this->doctrine->getRepository('TreeHouseIoBundle:Scraper'); |
99
|
|
|
|
100
|
|
|
if (!empty($ids)) { |
101
|
|
|
return $repo->findBy(['id' => $ids]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $repo->findAll(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.