1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KI\UserBundle\Command; |
4
|
|
|
|
5
|
|
|
use KI\UserBundle\Entity\User; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
12
|
|
|
|
13
|
|
|
class PhotoUpdateCommand extends ContainerAwareCommand |
14
|
|
|
{ |
15
|
|
|
protected function configure() |
16
|
|
|
{ |
17
|
|
|
$this |
18
|
|
|
->setName('upont:update:photo') |
19
|
|
|
->setDescription('Import missing photos from Facebook for the given promo') |
20
|
|
|
->addArgument('promo', InputArgument::REQUIRED, 'The promo whose photos are to be updated.') |
21
|
|
|
->addArgument('usernames', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'The usernames from the specified promo whose photos are to be updated.') |
|
|
|
|
22
|
|
|
->addOption('preview', 'p', InputOption::VALUE_NONE, 'Make a preview of the photos to be imported without importing them') |
|
|
|
|
23
|
|
|
->addOption('all', 'a', InputOption::VALUE_NONE, 'Treat the users regardless whether they already have a photo on uPont') |
|
|
|
|
24
|
|
|
->addOption('interactive', 'i', InputOption::VALUE_NONE, 'For each match, ask interactively whether the photo should be updated') |
|
|
|
|
25
|
|
|
->addOption('similarity-threshold', 's', InputOption::VALUE_REQUIRED, 'Similarity threshold with Fb profiles above which photos are imported in non-preview and non-interactive mode (in % between 0 and 100)', 85) |
|
|
|
|
26
|
|
|
; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
30
|
|
|
{ |
31
|
|
|
$em = $this->getContainer()->get('doctrine')->getManager(); |
32
|
|
|
$repo = $this->getContainer()->get('doctrine')->getRepository(User::class); |
33
|
|
|
$curlService = $this->getContainer()->get('ki_core.service.curl'); |
34
|
|
|
$imageService = $this->getContainer()->get('ki_core.service.image'); |
35
|
|
|
$fbToken = $this->getContainer()->getParameter('facebook_token'); |
36
|
|
|
$questionHelper = $this->getHelper('question'); |
37
|
|
|
|
38
|
|
|
$users = $repo->findByPromo($input->getArgument('promo')); |
39
|
|
|
$usernames = $input->getArgument('usernames'); |
40
|
|
|
if ($usernames) { |
41
|
|
|
foreach ($users as $user) { |
42
|
|
|
if (!in_array($user, $usernames)) { |
43
|
|
|
unset($users[array_search($user, $users)]); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
$question = new ConfirmationQuestion('Update? ', false, '/^y/i'); |
48
|
|
|
$token = '?access_token=' . $fbToken; |
49
|
|
|
|
50
|
|
|
// Ids des différents groupes facebook |
51
|
|
View Code Duplication |
switch ($input->getArgument('promo')) { |
52
|
|
|
// Attention, toujours préciser l'id facebook de la promo d'après |
53
|
|
|
// pour avoir les étrangers |
54
|
|
|
case '015': |
55
|
|
|
$id = '359646667495742'; |
56
|
|
|
break; // Wei't spirit |
57
|
|
|
case '016': |
58
|
|
|
$id = '1451446761806184'; |
59
|
|
|
break; // Wei't the phoque |
60
|
|
|
case '017': |
61
|
|
|
$id = '737969042997359'; |
62
|
|
|
break; // F'wei'ght Club |
63
|
|
|
case '018': |
64
|
|
|
$id = '1739424532975028'; |
65
|
|
|
break; // WEI'STED |
66
|
|
|
case '019': |
67
|
|
|
$id = '313192685791329'; |
68
|
|
|
break; // WEI'T FOR IT |
69
|
|
|
case '020': |
70
|
|
|
$id = '313192685791329'; |
71
|
|
|
break; // WEI'T FOR IT |
72
|
|
|
default: |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// On récupère la liste des membres |
77
|
|
|
$baseUrl = 'https://graph.facebook.com/v2.10'; |
78
|
|
|
$data = json_decode($curlService->curl($baseUrl . '/' . $id . '/members' . $token . '&limit=10000'), true); |
79
|
|
|
|
80
|
|
|
$updateCount = 0; |
81
|
|
|
$unfoundCount = 0; |
82
|
|
|
$notUpdatedInteractivelyCount = 0; |
83
|
|
|
$updateExistingPhoto = 0; |
84
|
|
|
|
85
|
|
|
$output->writeln('Fb photos '.($input->getOption('preview') ? 'would be ' : '').'imported for the following people (> '.$input->getOption('similarity-threshold').'% similar) :'); |
|
|
|
|
86
|
|
|
|
87
|
|
|
foreach ($users as $user) { |
88
|
|
|
$bestMatch = null; |
89
|
|
|
$bestPercent = -1; |
90
|
|
|
$noPhoto = $user->imageUrl() === 'uploads/others/default-user.png'; |
91
|
|
|
|
92
|
|
|
if ($noPhoto || $input->getOption('all')) { |
93
|
|
View Code Duplication |
foreach ($data['data'] as $member) { |
94
|
|
|
$percent = $this->isSimilar($user, $member); |
95
|
|
|
if ($percent > $bestPercent) { |
96
|
|
|
$bestPercent = $percent; |
97
|
|
|
$bestMatch = $member; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($bestPercent > $input->getOption('similarity-threshold')) { |
102
|
|
|
$output->writeln($user->getFirstName().' '.$user->getLastName().' <- '.$bestMatch['name'].' ('.$bestPercent.'% similar)'.($input->getOption('all') ? ' ['.($noPhoto ? 'to update' : 'already updated').']' : '')); |
|
|
|
|
103
|
|
|
$updateConfirmation = $input->getOption('interactive') ? $questionHelper->ask($input, $output, $question) : true; |
|
|
|
|
104
|
|
|
if (!$input->getOption('preview') && $updateConfirmation) { |
105
|
|
|
$url = '/' . $bestMatch['id'] . '/picture' . $token . '&width=9999&redirect=false'; |
106
|
|
|
$dataImage = json_decode($curlService->curl($baseUrl . $url), true); |
107
|
|
|
$image = $imageService->upload($dataImage['data']['url'], true); |
108
|
|
|
$user->setImage($image); |
109
|
|
|
} |
110
|
|
|
if (!$input->getOption('interactive') || $updateConfirmation) { |
111
|
|
|
$updateCount++; |
112
|
|
|
if (!$noPhoto) { |
113
|
|
|
$updateExistingPhoto++; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
else { |
117
|
|
|
$notUpdatedInteractivelyCount++; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
else { |
121
|
|
|
$unfoundCount++; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$em->flush(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$userSpecification = $usernames ? 'amongst the '.count($usernames).' specified user'.(count($usernames) > 1 ? 's ' : ' ') : ' '; |
|
|
|
|
129
|
|
|
$output->writeln( |
130
|
|
|
['End of list', |
131
|
|
|
'', |
132
|
|
|
'Students in promo '.$input->getArgument('promo').' '.$userSpecification.': '.count($users) |
133
|
|
|
]); |
134
|
|
|
if ($input->getOption('all')) { |
135
|
|
|
$output->writeln( |
136
|
|
|
[($input->getOption('preview') && !$input->getOption('interactive') ? 'To be i' : 'I').'mported missing photos: '.($updateCount-$updateExistingPhoto), |
|
|
|
|
137
|
|
|
'Non-updated photos: '.($unfoundCount+$notUpdatedInteractivelyCount), |
138
|
|
|
'Replaced photos: '.$updateExistingPhoto |
139
|
|
|
]); |
140
|
|
|
} |
141
|
|
|
else { |
142
|
|
|
$output->writeln( |
143
|
|
|
['Missing photos in promo '.$userSpecification.': '.($updateCount+$unfoundCount+$notUpdatedInteractivelyCount), |
|
|
|
|
144
|
|
|
($input->getOption('preview') && !$input->getOption('interactive') ? 'To be i' : 'I').'mported missing photos: '.$updateCount, |
|
|
|
|
145
|
|
|
'Remaining missing photos (unfound or refused Facebook profiles): '.($unfoundCount+$notUpdatedInteractivelyCount) |
|
|
|
|
146
|
|
|
]); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Compare un User uPont et un utilisateur Facebook et essaye de deviner si |
151
|
|
|
// ce sont les mêmes personnes |
152
|
|
|
private function isSimilar(User $user, array $member) |
153
|
|
|
{ |
154
|
|
|
$percent = 0; |
155
|
|
|
similar_text($user->getFirstName() . ' ' . $user->getLastName(), $member['name'], $percent); |
156
|
|
|
return $percent; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.