1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use App\Src\UseCases\Domain\Context\Model\Characteristic; |
6
|
|
|
use App\Src\UseCases\Infra\Sql\Model\CharacteristicsModel; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\Exception\ClientException; |
9
|
|
|
use Illuminate\Console\Command; |
10
|
|
|
use Illuminate\Support\Facades\Storage; |
11
|
|
|
use Ramsey\Uuid\Uuid; |
12
|
|
|
|
13
|
|
|
class ImportDepartmentFromWiki extends Command |
14
|
|
|
{ |
15
|
|
|
protected $signature = 'characteristics:department'; |
16
|
|
|
protected $description = 'Import the department'; |
17
|
|
|
private $httpClient; |
18
|
|
|
|
19
|
|
|
private $query = "?action=ask&api_version=3&query=[[A un numéro de département::%2B]] |?A un numéro de département |?A un nom |?A un climat |?A une icone&format=json"; |
20
|
|
|
private $queryPage2 = "?action=ask&api_version=3&query=[[A un numéro de département::%2B]] |?A un numéro de département |?A un nom |?A un climat |?A une icone|offset=50&format=json"; |
21
|
|
|
private $queryPage3 = "?action=ask&api_version=3&query=[[A un numéro de département::%2B]] |?A un numéro de département |?A un nom |?A un climat |?A une icone|offset=100&format=json"; |
22
|
|
|
|
23
|
|
|
private $queryPictures = '?action=query&titles=:file:&prop=imageinfo&iiprop=url&format=json'; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
parent::__construct(); |
29
|
|
|
$this->httpClient = new Client(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
public function handle() |
34
|
|
|
{ |
35
|
|
|
$response = $this->httpClient->get(config('wiki.api_uri').$this->query); |
36
|
|
|
$content = json_decode($response->getBody()->getContents(), true); |
37
|
|
|
$departments = $content['query']['results']; |
38
|
|
|
$this->handleDepartments($departments); |
39
|
|
|
|
40
|
|
|
$response = $this->httpClient->get(config('wiki.api_uri').$this->queryPage2); |
41
|
|
|
$content = json_decode($response->getBody()->getContents(), true); |
42
|
|
|
$departments = $content['query']['results']; |
43
|
|
|
$this->handleDepartments($departments); |
44
|
|
|
|
45
|
|
|
$response = $this->httpClient->get(config('wiki.api_uri').$this->queryPage3); |
46
|
|
|
$content = json_decode($response->getBody()->getContents(), true); |
47
|
|
|
$departments = $content['query']['results']; |
48
|
|
|
$this->handleDepartments($departments); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function handleDepartments($departments) |
52
|
|
|
{ |
53
|
|
|
foreach($departments as $department){ |
54
|
|
|
$key = key($department); |
55
|
|
|
$department = last($department)['printouts']; |
56
|
|
|
|
57
|
|
|
$climat = last($department['A un climat'])['fulltext']; |
58
|
|
|
$number = last($department['A un numéro de département']); |
59
|
|
|
$name = last($department['A un nom']); |
60
|
|
|
$iconUrlApi = str_replace(':file:', last($department['A une icone'])['fulltext'], $this->queryPictures); |
61
|
|
|
|
62
|
|
|
$characteristicModel = CharacteristicsModel::query()->where('code', $number)->first(); |
63
|
|
|
if(!isset($characteristicModel)){ |
64
|
|
|
$characteristicModel = new CharacteristicsModel(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$characteristicModel->uuid = $uuid = Uuid::uuid4(); |
68
|
|
|
$characteristicModel->priority = 10000; |
69
|
|
|
$characteristicModel->page_label = $key; |
70
|
|
|
$characteristicModel->pretty_page_label = $name; |
71
|
|
|
$characteristicModel->type = Characteristic::DEPARTMENT; |
72
|
|
|
$characteristicModel->code = $number; |
73
|
|
|
$characteristicModel->opt = [ |
74
|
|
|
'number' => $number, |
75
|
|
|
'climat' => $climat, |
76
|
|
|
'url' => 'wiki/'.str_replace(' ', '_', $climat) |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
$response = $this->httpClient->get(config('wiki.api_uri').$iconUrlApi); |
81
|
|
|
$content = json_decode($response->getBody()->getContents(), true); |
82
|
|
|
$picturesInfo = $content['query']['pages']; |
83
|
|
|
foreach($picturesInfo as $picture) { |
84
|
|
|
if (isset($picture['imageinfo']) && isset(last($picture['imageinfo'])['url'])) { |
85
|
|
|
$characteristicModel->page_id = $picture['pageid']; |
86
|
|
|
try { |
87
|
|
|
$response = $this->httpClient->get(last($picture['imageinfo'])['url']); |
88
|
|
|
$content = $response->getBody()->getContents(); |
89
|
|
|
$path = 'public/characteristics/' . $uuid . '.png'; |
90
|
|
|
Storage::put('public/characteristics/' . $uuid . '.png', $content); |
91
|
|
|
}catch (ClientException $e){ |
92
|
|
|
$this->info('No icon for department : '.$number); |
93
|
|
|
$path = ''; |
94
|
|
|
} |
95
|
|
|
}else{ |
96
|
|
|
$this->info('No icon for department : '.$number); |
97
|
|
|
$path = ''; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$characteristicModel->icon = $path; |
|
|
|
|
102
|
|
|
$characteristicModel->visible = false; |
103
|
|
|
|
104
|
|
|
$characteristicModel->save(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|