Skip to content
Snippets Groups Projects
Commit d837d5cc authored by Enzo Simonnet's avatar Enzo Simonnet
Browse files

requête serveur wiktextract ok

parent 44a0f6ac
No related branches found
No related tags found
No related merge requests found
...@@ -36,6 +36,7 @@ class WiktionaryManager ...@@ -36,6 +36,7 @@ class WiktionaryManager
* @param $word * @param $word
* @param $language * @param $language
* @return mixed * @return mixed
* @throws \Exception|TransportExceptionInterface
*/ */
public function search($word, $language) public function search($word, $language)
{ {
...@@ -45,34 +46,13 @@ class WiktionaryManager ...@@ -45,34 +46,13 @@ class WiktionaryManager
return $this->lastSearch['data']; return $this->lastSearch['data'];
} }
// usage: wikstraktor.py [-h] [-l LANGUAGE] [-w WIKI_LANGUAGE] [-m MOT]
// [-f DESTINATION_FILE] [-A] [-C]
//
// Interroger un wiktionnaire
// ex :
// ‣./wikstraktor.py -m blue
// ‣./wikstraktor.py -m blue -f blue.json -A -C
// ‣./wikstraktor.py -l en -w fr -m blue -f blue.json -A -C
// options:
// -h, --help show this help message and exit
// -l LANGUAGE, --language LANGUAGE
// la langue du mot
// -w WIKI_LANGUAGE, --wiki_language WIKI_LANGUAGE
// la langue du wiki
// -m MOT, --mot MOT le mot à chercher
// -f DESTINATION_FILE, --destination_file DESTINATION_FILE
// le fichier dans lequel stocker le résultat
// -A, --force_ascii json avec que des caractères ascii
// -C, --compact json sans indentation
//$time_start = microtime(true); /*Debug*/ //$time_start = microtime(true); /*Debug*/
$url = 'http://localhost:5000/search/' . $language . '/' . $language . '/' . $word . '/a_wikstraktor'; $url = 'http://localhost:5000/search/' . $language . '/' . $language . '/' . $word . '/a_wikstraktor';
$client = HttpClient::create(); $client = HttpClient::create();
$result = null;
// Function to restart the server // Function to restart the server
function restartServer() { function restartServer() {
$startServerCommand = 'nohup /var/www/live-query-wiktextract/venv/bin/python3 /var/www/live-query-wiktextract/src/app.py'; $startServerCommand = 'nohup /var/www/live-query-wiktextract/venv/bin/python3 /var/www/live-query-wiktextract/src/app.py';
exec($startServerCommand); exec($startServerCommand);
...@@ -80,55 +60,30 @@ class WiktionaryManager ...@@ -80,55 +60,30 @@ class WiktionaryManager
// Make the initial request with a timeout // Make the initial request with a timeout
try { try {
$response = $client->request('GET', $url, ['timeout' => 5]); $response = $client->request('GET', $url);
// dump($response->getStatusCode());die();
// Check the HTTP status code // Check the HTTP status code
if ($response->getStatusCode() === 200) { if ($response->getStatusCode() === 200) {
// Server is up and returned a successful response // Server is up and returned a successful response
$result = $response->getContent(); $result = $response->getContent();
} else {
// Handle other cases, if needed
$result = null;
} }
} catch (TransportExceptionInterface $exception) { } catch (TransportExceptionInterface $exception) {
// Catch the exception and check if it's a 404 response if (empty(exec('ps aux | grep "[a]pp.py"'))) {
if ($exception->getResponse() && $exception->getResponse()->getStatusCode() === 404) {
// Handle cases where the word is not found
$result = null;
} elseif (empty(exec('ps aux | grep "[a]pp.py"'))) {
restartServer(); restartServer();
$response = $client->request('GET', $url, ['timeout' => 5]); if (!empty(exec('ps aux | grep "[a]pp.py"'))) {
$response = $client->request('GET', $url);
// Check the HTTP status code if ($response->getStatusCode() === 200) {
if ($response->getStatusCode() === 200) { // Server is up and returned a successful response
// Server is up and returned a successful response $result = $response->getContent();
$result = $response->getContent(); }
} else { } else {
// Handle cases where the word is not found throw new \Exception('Server is down');
$result = null;
// Log or handle the error as needed
} }
} else {
// Handle cases where the server is still not responding
$result = null;
// Log or handle the error as needed
} }
} catch (Exception $e) {
// Handle other exceptions
dump($e->getResponse()->getContent()); die();
} }
// Stream responses with a timeout
foreach ($client->stream([$client->request('GET', $url)], 5) as $response => $chunk) {
if ($chunk->isTimeout()) {
// Handle timeout for streaming responses
restartServer();
}
}
//dump(gettype($result));dump($url);dump(microtime(true)-$time_start);dump($result);/*Debug*/ //dump(gettype($result));dump($url);dump(microtime(true)-$time_start);dump($result);/*Debug*/
// Output and error handling // Output and error handling
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment