1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ngtfkx\Laradeck\AddressGenerator\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
|
|
|
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Facades\Storage; |
8
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
|
|
|
9
|
|
|
|
10
|
|
|
class ParseCityAddressRu extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'address:city-address-ru {city} {url} {--limit=0}'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Parse streets and building numbers from http://www.city-address.ru'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Collection |
28
|
|
|
*/ |
29
|
|
|
protected $streets; |
30
|
|
|
|
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
$this->streets = new Collection(); |
34
|
|
|
|
35
|
|
|
parent::__construct(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Execute the console command. |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public function handle() |
44
|
|
|
{ |
45
|
|
|
$cityId = $this->argument('city'); |
46
|
|
|
|
47
|
|
|
$url = $this->argument('url'); |
48
|
|
|
|
49
|
|
|
$limit = (int)$this->option('limit'); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Получаем список всех страниц с улицами |
53
|
|
|
*/ |
54
|
|
|
$firstPage = $this->download($url); |
55
|
|
|
|
56
|
|
|
$allPages = (int)(new Crawler($firstPage))->filter('.uk-pagination > li')->last()->text(); |
57
|
|
|
|
58
|
|
|
$k = 0; |
59
|
|
|
for ($i = 1; $i <= $allPages; $i++) { |
60
|
|
|
/** |
61
|
|
|
* Получам список улиц на текущей странице |
62
|
|
|
*/ |
63
|
|
|
$page = $this->download($url . '/page-' . $i . '/'); |
64
|
|
|
|
65
|
|
|
$this->line('Downloading streets and building numbers from page #' . $i . ' from ' . $allPages); |
66
|
|
|
|
67
|
|
|
$streets = (new Crawler($page))->filter('.c4 > a'); |
68
|
|
|
|
69
|
|
|
$streets->each(function ($node) { |
70
|
|
|
/** |
71
|
|
|
* Получаем список домов |
72
|
|
|
*/ |
73
|
|
|
$url = 'http://www.city-address.ru' . $node->attr('href'); |
74
|
|
|
|
75
|
|
|
$page = $this->download($url); |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Обрабатываем список домов |
79
|
|
|
*/ |
80
|
|
|
$numbers = collect((new Crawler($page))->filter('.c6 > div > a')->extract('_text')) |
81
|
|
|
->reject(function ($item, $key) { |
|
|
|
|
82
|
|
|
return strpos($item, 'дом ') !== 0; |
83
|
|
|
})->map(function ($item, $key) { |
|
|
|
|
84
|
|
|
return str_replace('дом № ', '', $item); |
85
|
|
|
})->toArray(); |
86
|
|
|
|
87
|
|
|
$this->streets->put($node->text(), $numbers); |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
|
if (++$k == $limit) { |
91
|
|
|
$this->warn('Interrupted by the limit'); |
92
|
|
|
break; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Генерируем контент для файла с данными |
98
|
|
|
*/ |
99
|
|
|
$output = '<?php' . PHP_EOL . PHP_EOL; |
100
|
|
|
$output .= 'return [' . PHP_EOL; |
101
|
|
|
foreach ($this->streets as $street => $numbers) { |
102
|
|
|
$glueNumbers = implode('", "', $numbers); |
103
|
|
|
if (!empty($glueNumbers)) { |
104
|
|
|
$output .= ' "' . trim($street, ";") . '" => ["' . $glueNumbers . '"],' . PHP_EOL; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
$output .= '];' . PHP_EOL; |
108
|
|
|
|
109
|
|
|
Storage::put($cityId . '.php', $output); |
110
|
|
|
|
111
|
|
|
$this->info('Data was saved'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function download(string $url) |
115
|
|
|
{ |
116
|
|
|
$content = file_get_contents($url); |
117
|
|
|
|
118
|
|
|
return $content; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths