]> _ Git - cubist_azuretranslation.git/commitdiff
wip #6945 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 5 Jun 2024 11:21:09 +0000 (13:21 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 5 Jun 2024 11:21:09 +0000 (13:21 +0200)
.idea/deployment.xml
src/Api.php

index b4a40f59d34e524e18cba932faaa373e0b948677..0c9b02b975753d388c5d8da4379962d3b7deb033 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
-  <component name="PublishConfigData" remoteFilesAllowedToDisappearOnAutoupload="false">
+  <component name="PublishConfigData" autoUpload="Always" serverName="toolbox.fluidbook.com" remoteFilesAllowedToDisappearOnAutoupload="false" autoUploadExternalChanges="true">
     <serverData>
       <paths name="alpha.toolbox.fluidbook.com">
         <serverdata>
           </mappings>
         </serverdata>
       </paths>
+      <paths name="odl.cube">
+        <serverdata>
+          <mappings>
+            <mapping local="$PROJECT_DIR$" web="/" />
+          </mappings>
+        </serverdata>
+      </paths>
       <paths name="odl.paris.cubedesigners.com">
         <serverdata>
           <mappings>
       <paths name="toolbox.fluidbook.com">
         <serverdata>
           <mappings>
-            <mapping local="$PROJECT_DIR$" web="/" />
+            <mapping deploy="/vendor/cubist/azuretranslate" local="$PROJECT_DIR$" web="/" />
           </mappings>
+          <excludedPaths>
+            <excludedPath local="true" path="$PROJECT_DIR$/vendor" />
+          </excludedPaths>
         </serverdata>
       </paths>
       <paths name="toolbox.fluidbook.com (Host)">
         </serverdata>
       </paths>
     </serverData>
+    <option name="myAutoUpload" value="ALWAYS" />
   </component>
 </project>
\ No newline at end of file
index 9505fecaacfec5a4c88afb49709768dd3baf9aec..e7609862d24c629fbbe488d443ab6625f23b5a6b 100644 (file)
@@ -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 = "<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis'  xml:lang='$locale'><voice xml:lang='$locale' xml:gender='$gender' name='$voiceName'>";
-               $ssml .= htmlspecialchars($text, ENT_XML1 | ENT_SUBSTITUTE | ENT_QUOTES);
-               $ssml .= "</voice></speak>";
-
-               $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