]> git.basschouten.com Git - openhab-addons.git/blob
06e86505accf87718d1672d2a9f3839a189e9dc8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.voice.googlestt.internal;
14
15 import java.io.IOException;
16 import java.net.HttpURLConnection;
17 import java.net.URL;
18 import java.util.Arrays;
19 import java.util.HashSet;
20 import java.util.Locale;
21 import java.util.Set;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The {@link GoogleSTTLocale} is responsible for loading supported locales for the Google Cloud Speech-to-Text service.
31  *
32  * @author Miguel Álvarez - Initial contribution
33  */
34 @NonNullByDefault
35 public class GoogleSTTLocale {
36     private static final Set<Locale> SUPPORTED_LOCALES = new HashSet<>();
37     private static final String GC_STT_DOC_LANGUAGES = "https://cloud.google.com/speech-to-text/docs/languages";
38     private static final String LOCAL_COPY = "af-ZA,sq-AL,am-ET,ar-DZ,ar-BH,ar-EG,ar-IQ,ar-IL,ar-JO,ar-KW,ar-LB,ar-MA,ar-OM,ar-QA,ar-SA,ar-PS,ar-TN,ar-AE,ar-YE,hy-AM,az-AZ,eu-ES,bn-BD,bn-IN,bs-BA,bg-BG,my-MM,ca-ES,hr-HR,cs-CZ,da-DK,nl-BE,nl-NL,en-AU,en-CA,en-GH,en-HK,en-IN,en-IE,en-KE,en-NZ,en-NG,en-PK,en-PH,en-SG,en-ZA,en-TZ,en-GB,en-US,et-EE,fi-FI,fr-BE,fr-CA,fr-FR,fr-CH,gl-ES,ka-GE,de-AT,de-DE,de-CH,el-GR,gu-IN,he-IL,hi-IN,hu-HU,is-IS,id-ID,it-IT,it-CH,ja-JP,jv-ID,kn-IN,kk-KZ,km-KH,ko-KR,lo-LA,lv-LV,lt-LT,mk-MK,ms-MY,ml-IN,mr-IN,mn-MN,ne-NP,no-NO,fa-IR,pl-PL,pt-BR,pt-PT,ro-RO,ru-RU,sr-RS,si-LK,sk-SK,sl-SI,es-AR,es-BO,es-CL,es-CO,es-CR,es-DO,es-EC,es-SV,es-GT,es-HN,es-MX,es-NI,es-PA,es-PY,es-PE,es-PR,es-ES,es-US,es-UY,es-VE,su-ID,sw-KE,sw-TZ,sv-SE,ta-IN,ta-MY,ta-SG,ta-LK,te-IN,th-TH,tr-TR,uk-UA,ur-IN,ur-PK,uz-UZ,vi-VN,zu-ZA";
39
40     public static Set<Locale> getSupportedLocales() {
41         return SUPPORTED_LOCALES;
42     }
43
44     public static void loadLocales(boolean fromDoc) {
45         Logger logger = LoggerFactory.getLogger(GoogleSTTLocale.class);
46         if (!SUPPORTED_LOCALES.isEmpty()) {
47             logger.debug("Languages already loaded");
48             return;
49         }
50         if (!fromDoc) {
51             logger.debug("Loading languages from local");
52             loadLocalesFromLocal();
53             return;
54         }
55         logger.debug("Loading languages from doc");
56         try {
57             URL url = new URL(GC_STT_DOC_LANGUAGES);
58             HttpURLConnection con = (HttpURLConnection) url.openConnection();
59             con.setRequestMethod("GET");
60             con.setRequestProperty("Content-Type", "text/html");
61             int status = con.getResponseCode();
62             if (status != 200) {
63                 logger.warn("Http error loading supported locales, code: {}", status);
64                 loadLocalesFromLocal();
65                 return;
66             }
67             String html = new String(con.getInputStream().readAllBytes());
68             Pattern pattern = Pattern.compile("\\<td\\>(?<lang>[a-z]{2})\\-(?<country>[A-Z]{2})\\<\\/td\\>",
69                     Pattern.MULTILINE);
70             Matcher matcher = pattern.matcher(html);
71             Locale lastLocale = null;
72             while (matcher.find()) {
73                 Locale locale = new Locale(matcher.group("lang"), matcher.group("country"));
74                 if (lastLocale == null || !lastLocale.equals(locale)) {
75                     lastLocale = locale;
76                     SUPPORTED_LOCALES.add(locale);
77                     logger.debug("Locale added {}", locale.toLanguageTag());
78                 }
79             }
80         } catch (IOException e) {
81             logger.warn("Error loading supported locales: {}", e.getMessage());
82             loadLocalesFromLocal();
83         }
84     }
85
86     private static void loadLocalesFromLocal() {
87         Arrays.stream(LOCAL_COPY.split(",")).map((localeTag) -> {
88             String[] localeTagParts = localeTag.split("-");
89             return new Locale(localeTagParts[0], localeTagParts[1]);
90         }).forEach(SUPPORTED_LOCALES::add);
91     }
92 }