|
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 $key => $department){ |
|
54
|
|
|
$department = last($department)['printouts']; |
|
55
|
|
|
|
|
56
|
|
|
$climat = last($department['A un climat'])['fulltext']; |
|
57
|
|
|
$number = last($department['A un numéro de département']); |
|
58
|
|
|
$name = last($department['A un nom']); |
|
59
|
|
|
$iconUrlApi = str_replace(':file:', last($department['A une icone'])['fulltext'], $this->queryPictures); |
|
60
|
|
|
|
|
61
|
|
|
$characteristicModel = CharacteristicsModel::query()->where('code', $number)->first(); |
|
62
|
|
|
if(!isset($characteristicModel)){ |
|
63
|
|
|
$characteristicModel = new CharacteristicsModel(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$characteristicModel->uuid = $uuid = Uuid::uuid4(); |
|
67
|
|
|
$characteristicModel->priority = 10000; |
|
68
|
|
|
$characteristicModel->page_label = $name; |
|
69
|
|
|
$characteristicModel->pretty_page_label = $name; |
|
70
|
|
|
$characteristicModel->type = Characteristic::DEPARTMENT; |
|
71
|
|
|
$characteristicModel->code = $number; |
|
72
|
|
|
$characteristicModel->opt = [ |
|
73
|
|
|
'number' => $number, |
|
74
|
|
|
'climat' => $climat, |
|
75
|
|
|
'url' => 'wiki/'.str_replace(' ', '_', $climat) |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
$response = $this->httpClient->get(config('wiki.api_uri').$iconUrlApi); |
|
80
|
|
|
$content = json_decode($response->getBody()->getContents(), true); |
|
81
|
|
|
$picturesInfo = $content['query']['pages']; |
|
82
|
|
|
foreach($picturesInfo as $picture) { |
|
83
|
|
|
if (isset($picture['imageinfo']) && isset(last($picture['imageinfo'])['url'])) { |
|
84
|
|
|
$characteristicModel->page_id = $picture['pageid']; |
|
85
|
|
|
try { |
|
86
|
|
|
$response = $this->httpClient->get(last($picture['imageinfo'])['url']); |
|
87
|
|
|
$content = $response->getBody()->getContents(); |
|
88
|
|
|
$path = 'public/characteristics/' . $uuid . '.png'; |
|
89
|
|
|
Storage::put('public/characteristics/' . $uuid . '.png', $content); |
|
90
|
|
|
}catch (ClientException $e){ |
|
91
|
|
|
$this->info('No icon for department : '.$number); |
|
92
|
|
|
$path = ''; |
|
93
|
|
|
} |
|
94
|
|
|
}else{ |
|
95
|
|
|
$this->info('No icon for department : '.$number); |
|
96
|
|
|
$path = ''; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$characteristicModel->icon = $path; |
|
|
|
|
|
|
101
|
|
|
$characteristicModel->visible = false; |
|
102
|
|
|
|
|
103
|
|
|
$characteristicModel->save(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|