|
| 1 | +/* |
| 2 | + * Speech Services API v3.0-beta1 |
| 3 | + */ |
| 4 | + |
| 5 | +package customvoice; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.text.DateFormat; |
| 9 | +import java.text.SimpleDateFormat; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Date; |
| 12 | +import java.util.List; |
| 13 | +import java.util.UUID; |
| 14 | + |
| 15 | +import org.apache.commons.cli.CommandLine; |
| 16 | +import org.json.JSONArray; |
| 17 | +import org.json.JSONObject; |
| 18 | + |
| 19 | +import customvoice.client.ApiClient; |
| 20 | +import customvoice.client.ApiException; |
| 21 | +import customvoice.client.Configuration; |
| 22 | +import customvoice.client.DateFormatHelper; |
| 23 | +import customvoice.model.Voice; |
| 24 | +import customvoice.model.VoiceSynthesis; |
| 25 | + |
| 26 | +public class ApiHandler { |
| 27 | + public static void excuteApi(CommandLine cli) { |
| 28 | + String region = cli.getOptionValue("r"); |
| 29 | + String hostPath = String.format(Configuration.HostUri, region); |
| 30 | + String subscriptionKey = cli.getOptionValue("subscriptionkey"); |
| 31 | + |
| 32 | + ApiClient apiClient = new ApiClient(hostPath); |
| 33 | + apiClient.setApiKey(subscriptionKey); |
| 34 | + VoiceSynthesisLib api = new VoiceSynthesisLib(apiClient); |
| 35 | + |
| 36 | + try { |
| 37 | + if (cli.hasOption("create")) { |
| 38 | + String name = cli.getOptionValue("name"); |
| 39 | + if (name == null) { |
| 40 | + System.out.println("Please enter the name of voice synthesis task"); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + String description = cli.getOptionValue("description"); |
| 45 | + if (description == null) { |
| 46 | + description = ""; |
| 47 | + } |
| 48 | + |
| 49 | + String locale = cli.getOptionValue("locale"); |
| 50 | + if (locale == null) { |
| 51 | + System.out.println("Please enter the locale of the model voice synthesis task used"); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + String modelList = cli.getOptionValue("modelidlist"); |
| 56 | + if (modelList == null) { |
| 57 | + System.out |
| 58 | + .println("Please enter the model list of the voice synthesis task used, separated by ';'"); |
| 59 | + return; |
| 60 | + } |
| 61 | + List<UUID> model = new ArrayList<UUID>(); |
| 62 | + for (String id : modelList.split(";")) { |
| 63 | + model.add(UUID.fromString(id)); |
| 64 | + } |
| 65 | + |
| 66 | + String outputFormat = cli.getOptionValue("outputformat"); |
| 67 | + if (outputFormat == null) { |
| 68 | + outputFormat = "riff-16khz-16bit-mono-pcm"; |
| 69 | + } |
| 70 | + |
| 71 | + String properties = ""; |
| 72 | + if (cli.hasOption("concatenateresult")) { |
| 73 | + properties = "{\"ConcatenateResult\": \"true\"}"; |
| 74 | + } |
| 75 | + |
| 76 | + String scriptFile = cli.getOptionValue("scriptfile"); |
| 77 | + if (scriptFile == null) { |
| 78 | + System.out.println("Please enter the script file path"); |
| 79 | + return; |
| 80 | + } |
| 81 | + File script = new File(scriptFile); |
| 82 | + |
| 83 | + String synthesisId = api.SubmitSynthesis(name, description, locale, model, outputFormat, properties, |
| 84 | + script); |
| 85 | + System.out.printf("Submit synthesis request successful , id: %s", synthesisId); |
| 86 | + return; |
| 87 | + } else if (cli.hasOption("getvoice")) { |
| 88 | + List<Voice> reslut = api.GetVoice(); |
| 89 | + System.out.println(new JSONArray(reslut)); |
| 90 | + } else if (cli.hasOption("getvoicesynthesis")) { |
| 91 | + String timeStarts = cli.getOptionValue("timestart"); |
| 92 | + String timeEnds = cli.getOptionValue("timeend"); |
| 93 | + String status = cli.getOptionValue("status"); |
| 94 | + String skips = cli.getOptionValue("skip"); |
| 95 | + int skip = -1; |
| 96 | + if (skips != null) { |
| 97 | + try { |
| 98 | + skip = Integer.parseInt(skips); |
| 99 | + if (skip < 0) { |
| 100 | + System.out.println( |
| 101 | + "Please enter a valid skip parameter, should be a ingeter greater or equals 0"); |
| 102 | + return; |
| 103 | + } |
| 104 | + } catch (NumberFormatException e) { |
| 105 | + System.out.println( |
| 106 | + "Please enter a valid skip parameter, should be a ingeter greater or equals 0"); |
| 107 | + return; |
| 108 | + } |
| 109 | + } |
| 110 | + int top = -1; |
| 111 | + String tops = cli.getOptionValue("top"); |
| 112 | + if (tops != null) { |
| 113 | + try { |
| 114 | + top = Integer.parseInt(tops); |
| 115 | + if (top < 0) { |
| 116 | + System.out |
| 117 | + .println("Please enter a valid top parameter, should be a ingeter greater than 0"); |
| 118 | + return; |
| 119 | + } |
| 120 | + } catch (NumberFormatException e) { |
| 121 | + System.out.println("Please enter a valid top parameter, should be a ingeter greater than 0"); |
| 122 | + return; |
| 123 | + } |
| 124 | + } |
| 125 | + Date timeStart = null; |
| 126 | + if (timeStarts != null) { |
| 127 | + try { |
| 128 | + timeStart = DateFormatHelper.parseToDate(timeStarts); |
| 129 | + } catch (NumberFormatException e) { |
| 130 | + System.out.println("Please enter a valid timestart parameter, like 2020-05-01 12:00"); |
| 131 | + return; |
| 132 | + } |
| 133 | + } |
| 134 | + Date timeEnd = null; |
| 135 | + if (timeEnds != null) { |
| 136 | + try { |
| 137 | + timeEnd = DateFormatHelper.parseToDate(timeEnds); |
| 138 | + } catch (NumberFormatException e) { |
| 139 | + System.out.println("Please enter a valid timeend parameter, like 2020-05-15"); |
| 140 | + return; |
| 141 | + } |
| 142 | + } |
| 143 | + List<VoiceSynthesis> result = api.GetVoiceSynthesis(timeStart, timeEnd, status, skip, top); |
| 144 | + System.out.println(new JSONArray(result)); |
| 145 | + } else if (cli.hasOption("getvoicesynthesisbyid")) { |
| 146 | + String voiceSynthesisId = cli.getOptionValue("voicesynthesisid"); |
| 147 | + if (voiceSynthesisId == null) { |
| 148 | + System.out.println("Please enter Voice Synthesis Id"); |
| 149 | + return; |
| 150 | + } |
| 151 | + VoiceSynthesis reslut = api.GetVoiceSynthesis(UUID.fromString(voiceSynthesisId)); |
| 152 | + System.out.println(new JSONObject(reslut)); |
| 153 | + } else if (cli.hasOption("delete")) { |
| 154 | + String voiceSynthesisId = cli.getOptionValue("voicesynthesisid"); |
| 155 | + if (voiceSynthesisId == null) { |
| 156 | + System.out.println("Please enter Voice Synthesis Id"); |
| 157 | + return; |
| 158 | + } |
| 159 | + api.DeleteSynthesis(UUID.fromString(voiceSynthesisId)); |
| 160 | + System.out.printf("Delete successful, id : %s", voiceSynthesisId); |
| 161 | + } else { |
| 162 | + System.out.println("Please enter the action you need to perform"); |
| 163 | + } |
| 164 | + } catch (Exception e) { |
| 165 | + System.out.println( |
| 166 | + "Request failed, wrong parameter or request timed out, please check the parameters and try again."); |
| 167 | + System.out.println("We got unexpected: " + e.getMessage()); |
| 168 | + if (e instanceof ApiException) { |
| 169 | + System.out.println("Response body: " + ((ApiException) e).getResponseBody()); |
| 170 | + } |
| 171 | + e.printStackTrace(); |
| 172 | + return; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments