Completed
Push — master ( d676b1...ecc147 )
by Dispositif
02:12
created

CompleteProcess::onlineQuerySearch()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 21
rs 9.7666
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 : Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Application\Examples;
11
12
use App\Application\Bot;
13
use App\Application\Memory;
14
use App\Application\QueueInterface;
15
use App\Domain\Models\Wiki\OuvrageTemplate;
16
use App\Domain\OuvrageComplete;
17
use App\Domain\OuvrageFactory;
18
use App\Domain\OuvrageOptimize;
19
use App\Domain\Utils\TemplateParser;
20
use App\Infrastructure\DbAdapter;
21
use App\Infrastructure\GoogleBooksAdapter;
22
use Normalizer;
23
use Throwable;
24
25
include __DIR__.'/../myBootstrap.php';
26
27
$dirty = new CompleteProcess(new DbAdapter());
28
$dirty->run();
29
30
/**
31
 * Class CompleteProcess
32
 */
33
class CompleteProcess
34
{
35
    /**
36
     * @var QueueInterface
37
     */
38
    private $queueAdapter;
39
    /**
40
     * @var string
41
     */
42
    private $raw = '';
43
    private $log = [];
44
    private $notCosmetic = false;
45
    private $major = false;
46
    /**
47
     * @var OuvrageTemplate
48
     */
49
    private $ouvrage;
50
51
    public function __construct(QueueInterface $queueAdapter)
52
    {
53
        $this->queueAdapter = $queueAdapter;
54
    }
55
56
    public function run(?int $limit = 10000)
57
    {
58
        $memory = new Memory();
59
        while ($limit > 0) {
60
            $limit--;
61
            sleep(1);
62
            $this->raw = $this->getNewRaw();
63
64
            echo sprintf(
65
                "-------------------------------\n%s [%s]\n\n%s\n",
66
                date("Y-m-d H:i:s"),
67
                Bot::getGitVersion() ?? '',
68
                $this->raw
69
            );
70
            $memory->echoMemory(true);
71
72
            // initialise variables
73
            $this->log = [];
74
            $this->ouvrage = null;
75
            $this->notCosmetic = false;
76
            $this->major = false;
77
78
79
            try {
80
                $parse = TemplateParser::parseAllTemplateByName('ouvrage', $this->raw);
81
                $origin = $parse['ouvrage'][0]['model'] ?? null;
82
            } catch (Throwable $e) {
83
                echo sprintf("*** ERREUR impossible de transformer en modèle %s \n", $this->raw);
84
                continue;
85
            }
86
87
            if (!$origin instanceof OuvrageTemplate) {
88
                echo sprintf("*** ERREUR impossible de transformer en modèle %s \n", $this->raw);
89
                continue;
90
            }
91
92
            // Final optimizing (with online predictions)
93
            $optimizer = new OuvrageOptimize($origin, null);
94
            $optimizer->doTasks();
95
            $this->ouvrage = $optimizer->getOuvrage();
96
            $this->log = array_merge($this->log, $optimizer->getLog());
97
            $this->notCosmetic = ($optimizer->notCosmetic || $this->notCosmetic);
98
99
            /**
100
             * RECHERCHE ONLINE
101
             */
102
            $isbn = $origin->getParam('isbn') ?? null; // avant mise en forme EAN>ISBN
103
            $isbn10 = $origin->getParam('isbn2') ?? $origin->getParam('isbn10') ?? null;
104
            if (!empty($isbn)
105
                && empty($origin->getParam('isbn invalide'))
106
                && empty($origin->getParam('isbn erroné'))
107
            ) {
108
                $this->onlineIsbnSearch($isbn, $isbn10);
109
            }
110
111
            $this->sendCompleted();
112
            unset($optimizer);
113
            unset($parse);
114
            unset($origin);
115
        } // END WHILE
116
117
        return true;
118
    }
119
120
    /**
121
     * Get raw string to complete from AMQP queue, SQL Select or file reading.
122
     *
123
     * @return string|null
124
     * @throws \Exception
125
     */
126
    private function getNewRaw(): ?string
127
    {
128
        $raw = $this->queueAdapter->getNewRaw();
129
        if (!$raw) {
130
            echo "STOP: no more queue to process \n";
131
            throw new \Exception('no more queue to process');
132
        }
133
134
        return $raw;
135
    }
136
137
    private function onlineIsbnSearch(string $isbn, ?string $isbn10 = null)
138
    {
139
        online:
140
        echo "sleep 10...\n";
141
        sleep(10);
142
143
        try {
144
            dump('BIBLIO NAT FRANCE...');
145
            // BnF sait pas trouver un vieux livre (10) d'après ISBN-13... FACEPALM !
146
            if ($isbn10) {
147
                $bnfOuvrage = OuvrageFactory::BnfFromIsbn($isbn10);
148
                sleep(2);
149
            }
150
            if (!$isbn10 || empty($bnfOuvrage) || empty($bnfOuvrage->getParam('titre'))) {
151
                $bnfOuvrage = OuvrageFactory::BnfFromIsbn($isbn);
152
            }
153
            if (isset($bnfOuvrage) and $bnfOuvrage instanceof OuvrageTemplate) {
154
                $this->completeOuvrage($bnfOuvrage);
155
            }
156
        } catch (Throwable $e) {
157
            echo "*** ERREUR BnF Isbn Search".$e->getMessage()."\n";
158
        }
159
160
        if (!isset($bnfOuvrage) || !$this->skipGoogle($bnfOuvrage)) {
161
            try {
162
                dump('GOOGLE...');
163
                $googleOuvrage = OuvrageFactory::GoogleFromIsbn($isbn);
164
                $this->completeOuvrage($googleOuvrage);
165
            } catch (Throwable $e) {
166
                echo "*** ERREUR GOOGLE Isbn Search ***".$e->getMessage()."\n";
167
                if (strpos($e->getMessage(), 'Daily Limit Exceeded') !== false) {
168
                    echo "sleep 3h\n";
169
                    sleep(60 * 60 * 3);
170
                    echo "Wake up\n";
171
                    goto online;
172
                }
173
            }
174
        }
175
176
        if (!isset($bnfOuvrage) && !isset($googleOuvrage)) {
177
            try {
178
                dump('OpenLibrary...');
179
                $openLibraryOuvrage = OuvrageFactory::OpenLibraryFromIsbn($isbn);
180
                if (!empty($openLibraryOuvrage)) {
181
                    $this->completeOuvrage($openLibraryOuvrage);
182
                }
183
            } catch (Throwable $e) {
184
                echo '**** ERREUR OpenLibrary Isbn Search';
185
            }
186
        }
187
    }
188
189
//    private function onlineQuerySearch(string $query)
190
//    {
191
//        echo "sleep 40...";
192
//        sleep(20);
193
//        onlineQuerySearch:
194
//
195
//        try {
196
//            dump('GOOGLE SEARCH...');
197
//            //            $googleOuvrage = OuvrageFactory::GoogleFromIsbn($isbn);
198
//            $adapter = new GoogleBooksAdapter();
199
//            $data = $adapter->search('blabla');
200
//            dump($data);
201
//            //die;
202
//            //            return $import->getOuvrage();
203
//            //            $this->completeOuvrage($googleOuvrage);
204
//        } catch (Throwable $e) {
205
//            echo "*** ERREUR GOOGLE QuerySearch *** ".$e->getMessage()."\n";
206
//            echo "sleep 30min";
207
//            sleep(60 * 30);
208
//            echo "Wake up\n";
209
//            goto onlineQuerySearch;
210
//        }
211
//    }
212
213
    private function completeOuvrage(OuvrageTemplate $onlineOuvrage)
214
    {
215
        dump($onlineOuvrage->serialize(true));
216
        $optimizer = new OuvrageOptimize($onlineOuvrage);
217
        $onlineOptimized = ($optimizer)->doTasks()->getOuvrage();
218
219
        $completer = new OuvrageComplete($this->ouvrage, $onlineOptimized);
220
        $this->ouvrage = $completer->getResult();
221
        dump($completer->getLog());
222
        if ($completer->major) {
223
            $this->major = true;
224
        }
225
        $this->notCosmetic = ($completer->notCosmetic || $this->notCosmetic);
226
        $this->log = array_merge($this->log, $completer->getLog());
227
        unset($optimizer);
228
        unset($completer);
229
    }
230
231
    private function sendCompleted()
232
    {
233
        $isbn13 = $this->ouvrage->getParam('isbn') ?? null;
234
235
        $finalData = [
236
            //    'page' =>
237
            'raw' => $this->raw,
238
            'opti' => $this->serializeFinalOpti(),
239
            'optidate' => date("Y-m-d H:i:s"),
240
            'modifs' => mb_substr(implode(',', $this->log), 0, 250),
241
            'notcosmetic' => ($this->notCosmetic) ? 1 : 0,
242
            'major' => ($this->major) ? 1 : 0,
243
            'isbn' => substr($isbn13, 0, 20),
244
            'version' => Bot::getGitVersion() ?? null,
245
        ];
246
        dump($finalData);
247
        // Json ?
248
        $result = $this->queueAdapter->sendCompletedData($finalData);
249
        dump($result); // bool
250
    }
251
252
    /**
253
     * Final serialization of the completed OuvrageTemplate.
254
     *
255
     * @return string
256
     */
257
    private function serializeFinalOpti(): string
258
    {
259
        $finalOpti = $this->ouvrage->serialize(true);
260
        $finalOpti = Normalizer::normalize($finalOpti);
261
262
        return $finalOpti;
263
    }
264
265
    private function skipGoogle($bnfOuvrage): bool
266
    {
267
        if ($bnfOuvrage instanceOf OuvrageTemplate
268
            && !empty($bnfOuvrage->getParam('titre'))
269
            && (!empty($this->ouvrage->getParam('lire en ligne'))
270
                || !empty($this->ouvrage->getParam('présentation en ligne')))
271
        ) {
272
            return true;
273
        }
274
275
        return false;
276
    }
277
}
278