From 3e8dd2adfc8a159c59f43dc51dcb21baeb938a7d Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 5 Jun 2024 13:21:09 +0200 Subject: [PATCH] wip #6945 @0.5 --- .idea/deployment.xml | 15 +++- src/Api.php | 179 +++++++++++++++---------------------------- 2 files changed, 75 insertions(+), 119 deletions(-) diff --git a/.idea/deployment.xml b/.idea/deployment.xml index b4a40f5..0c9b02b 100644 --- a/.idea/deployment.xml +++ b/.idea/deployment.xml @@ -1,6 +1,6 @@ - + @@ -198,6 +198,13 @@ + + + + + + + @@ -243,8 +250,11 @@ - + + + + @@ -388,5 +398,6 @@ + \ No newline at end of file diff --git a/src/Api.php b/src/Api.php index 9505fec..e760986 100644 --- a/src/Api.php +++ b/src/Api.php @@ -6,121 +6,66 @@ use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use Illuminate\Support\Facades\Cache; -class Api { - /** - * @var string - */ - protected $key; - - /** - * @var string - */ - protected $region; - - /** - * @var null|string - */ - protected $token = null; - - /** - * @var int - */ - protected $tokenTime = 0; - - - /** - * @var Client - */ - protected $client; - - public function __construct($key, $region = 'francecentral') { - $this->key = $key; - $this->region = $region; - $this->client = new Client(); - } - - public function textToSpeech($text, $locale, $gender, $voiceName, $saveToFile) { - $ssml = ""; - $ssml .= htmlspecialchars($text, ENT_XML1 | ENT_SUBSTITUTE | ENT_QUOTES); - $ssml .= ""; - - $this->_call('/v1', 'post', [ - 'X-Microsoft-OutputFormat' => 'audio-48khz-192kbitrate-mono-mp3', - 'Content-type' => 'application/ssml+xml' - ], $ssml, ['sink' => $saveToFile]); - } - - /** - * @param $raw - * @return array|false - */ - public function listVoices($raw = false) { - $cacheKey = 'azure_tts_voices_' . $raw; - Cache::forget($cacheKey); - return Cache::remember($cacheKey, 86400, function () use ($raw) { - return $this->_listVoices($raw); - }); - } - - /** - * @param $raw bool - * @return array|false - */ - protected function _listVoices($raw = false) { - $resp = $this->_call('/voices/list'); - if (!$resp) { - return false; - } - $j = json_decode($resp->getBody()->getContents(), true); - if ($raw) { - return $j; - } - $res = []; - foreach ($j as $voice) { - $res[$voice['Locale'] . '/' . $voice['Gender'] . '/' . $voice['ShortName']] = $voice['DisplayName'] . ' / ' . $voice['Gender'] . ' / ' . $voice['LocaleName']; - } - return $res; - } - - /** - * @param string $url - * @param string $method - * @param array $headers - * @param string|null $body - * @param array $options - * @return Response|false - */ - protected function _call($url, $method = 'get', $headers = [], $body = null, $options = []) { - - if (!$this->checkToken()) { - return false; - } - $baseURL = 'https://' . $this->region . '.tts.speech.microsoft.com/cognitiveservices/'; - $headers = array_merge([ - 'Authorization' => 'Bearer ' . $this->token, - 'User-Agent' => 'Cubist TTS Client'], $headers); - - return $this->client->send(new Request($method, $baseURL . ltrim($url, '/'), $headers, $body), $options); - } - - protected function checkToken() { - if (null !== $this->token && $this->tokenTime > (time() - 540)) { - return true; - } - - try { - - $this->token = $this->client->post('https://' . $this->region . '.api.cognitive.microsoft.com/sts/v1.0/issuetoken', - ['headers' => - [ - 'Ocp-Apim-Subscription-Key' => $this->key - ] - ] - )->getBody()->getContents(); - return true; - } catch (\Exception $e) { - - } - return false; - } +class Api +{ + /** + * @var string + */ + protected $key; + + /** + * @var string + */ + protected $region; + + /** + * @var null|string + */ + protected $token = null; + + /** + * @var int + */ + protected $tokenTime = 0; + + + /** + * @var Client + */ + protected $client; + + public function __construct($key, $region = 'francecentral') + { + $this->key = $key; + $this->region = $region; + $this->client = new Client(); + } + + public function translate($text, $toLanguage, $fromLanguage) + { + return $this->_call('/translate?api-version=3.0&from=' . $fromLanguage . '&to=' . $toLanguage, 'post', [ + 'Content-type' => 'application/json' + ], json_encode(['Text' => $text])); + } + + + /** + * @param string $url + * @param string $method + * @param array $headers + * @param string|null $body + * @param array $options + * @return Response|false + */ + protected function _call($url, $method = 'get', $headers = [], $body = null, $options = []) + { + + $baseURL = 'https://api.cognitive.microsofttranslator.com/'; + $headers = array_merge([ + 'Ocp-Apim-Subscription-Key' => $this->key, + 'Ocp-Apim-Subscription-Region' => $this->region, + 'User-Agent' => 'Cubist Translate Client'], $headers); + + return $this->client->send(new Request($method, $baseURL . ltrim($url, '/'), $headers, $body), $options); + } } \ No newline at end of file -- 2.39.5