1
|
|
|
<?php |
2
|
|
|
namespace Sesame\Command; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Client; |
5
|
|
|
use Sesame\Sesame; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class CrawlArticleCommand |
14
|
|
|
* @package Sesame\Command |
15
|
|
|
*/ |
16
|
|
|
class CrawlArticleCommand extends Command |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Sesame |
21
|
|
|
*/ |
22
|
|
|
protected $sesame; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* CrawlArticleCommand constructor |
26
|
|
|
* |
27
|
|
|
* @param null|string $name |
28
|
|
|
*/ |
29
|
|
|
public function __construct(?string $name = null) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($name); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* setSesame |
36
|
|
|
* The setter for the dependency injection of the Sesame class. |
37
|
|
|
* @param Sesame $sesame |
38
|
|
|
*/ |
39
|
|
|
public function setSesame(Sesame $sesame) |
40
|
|
|
{ |
41
|
|
|
$this->sesame = $sesame; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* configure |
46
|
|
|
*/ |
47
|
|
|
protected function configure() |
48
|
|
|
{ |
49
|
|
|
$this |
50
|
|
|
->setName('crawl:article') |
51
|
|
|
->setDescription('Crawls an article.') |
52
|
|
|
->setHelp('This command allows you to crawl an article on article on aliexpress.com and grab its data.') |
53
|
|
|
->addArgument( |
54
|
|
|
'articleUrl', |
55
|
|
|
InputArgument::REQUIRED, |
56
|
|
|
'The URL of the article, which copied from your browser.' |
57
|
|
|
) |
58
|
|
|
->addOption( |
59
|
|
|
'crawlVariations', |
60
|
|
|
'cv', |
61
|
|
|
InputOption::VALUE_REQUIRED, |
62
|
|
|
'With this option you can control if Sesame will crawl the variations for the article too. Syntax "-cv0" to ignore variations or "--crawlVariations=1" to include them.', |
63
|
|
|
1 |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* execute |
69
|
|
|
* The actual business logic of the command. |
70
|
|
|
* @param InputInterface $input |
71
|
|
|
* @param OutputInterface $output |
72
|
|
|
* @return int|null|void |
73
|
|
|
*/ |
74
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
75
|
|
|
{ |
76
|
|
|
$articleUrl = $input->getArgument('articleUrl'); |
77
|
|
|
$crawlVariations = $input->getOption('crawlVariations'); |
78
|
|
|
if ($crawlVariations === 1) { |
79
|
|
|
$crawlVariations = true; |
80
|
|
|
} elseif ($crawlVariations === 0) { |
81
|
|
|
$crawlVariations = false; |
82
|
|
|
} |
83
|
|
|
$client = new Client(); |
84
|
|
|
$response = $client->request('GET', $articleUrl); |
85
|
|
|
$responseBody = $response->getBody(); |
86
|
|
|
$this->sesame->crawlArticle((string) $responseBody, $crawlVariations); |
87
|
|
|
print_r((string)$responseBody); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|