1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
12 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14 | function verb(n) { return function (v) { return step([n, v]); }; }
|
15 | function step(op) {
|
16 | if (f) throw new TypeError("Generator is already executing.");
|
17 | while (_) try {
|
18 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19 | if (y = 0, t) op = [op[0] & 2, t.value];
|
20 | switch (op[0]) {
|
21 | case 0: case 1: t = op; break;
|
22 | case 4: _.label++; return { value: op[1], done: false };
|
23 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
24 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25 | default:
|
26 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30 | if (t[2]) _.ops.pop();
|
31 | _.trys.pop(); continue;
|
32 | }
|
33 | op = body.call(thisArg, _);
|
34 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36 | }
|
37 | };
|
38 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
39 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
40 | };
|
41 | Object.defineProperty(exports, "__esModule", { value: true });
|
42 | exports.getAllAudioBase64 = exports.getAudioBase64 = void 0;
|
43 | var assertInputTypes_1 = __importDefault(require("./assertInputTypes"));
|
44 | var axios_1 = __importDefault(require("axios"));
|
45 | var splitLongText_1 = __importDefault(require("./splitLongText"));
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | var getAudioBase64 = function (text, _a) {
|
58 | var _b = _a === void 0 ? {} : _a, _c = _b.lang, lang = _c === void 0 ? 'en' : _c, _d = _b.slow, slow = _d === void 0 ? false : _d, _e = _b.host, host = _e === void 0 ? 'https://translate.google.com' : _e, _f = _b.timeout, timeout = _f === void 0 ? 10000 : _f;
|
59 | return __awaiter(void 0, void 0, void 0, function () {
|
60 | var res, result;
|
61 | return __generator(this, function (_g) {
|
62 | switch (_g.label) {
|
63 | case 0:
|
64 | assertInputTypes_1.default(text, lang, slow, host);
|
65 | if (typeof timeout !== 'number' || timeout <= 0) {
|
66 | throw new TypeError('timeout should be a positive number');
|
67 | }
|
68 | if (text.length > 200) {
|
69 | throw new RangeError("text length (" + text.length + ") should be less than 200 characters. Try \"getAllAudioBase64(text, [option])\" for long text.");
|
70 | }
|
71 | return [4 , axios_1.default({
|
72 | method: 'post',
|
73 | baseURL: host,
|
74 | url: '/_/TranslateWebserverUi/data/batchexecute',
|
75 | timeout: timeout,
|
76 | data: 'f.req=' +
|
77 | encodeURIComponent(JSON.stringify([
|
78 | [['jQ1olc', JSON.stringify([text, lang, slow ? true : null, 'null']), null, 'generic']],
|
79 | ])),
|
80 | })];
|
81 | case 1:
|
82 | res = _g.sent();
|
83 | try {
|
84 | result = eval(res.data.slice(5))[0][2];
|
85 | }
|
86 | catch (e) {
|
87 | throw new Error("parse response failed:\n" + res.data);
|
88 | }
|
89 |
|
90 | if (!result) {
|
91 | throw new Error("lang \"" + lang + "\" might not exist");
|
92 | }
|
93 |
|
94 | try {
|
95 | result = eval(result)[0];
|
96 | }
|
97 | catch (e) {
|
98 | throw new Error("parse response failed:\n" + res.data);
|
99 | }
|
100 | return [2 , result];
|
101 | }
|
102 | });
|
103 | });
|
104 | };
|
105 | exports.getAudioBase64 = getAudioBase64;
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | var getAllAudioBase64 = function (text, _a) {
|
124 | var _b = _a === void 0 ? {} : _a, _c = _b.lang, lang = _c === void 0 ? 'en' : _c, _d = _b.slow, slow = _d === void 0 ? false : _d, _e = _b.host, host = _e === void 0 ? 'https://translate.google.com' : _e, _f = _b.splitPunct, splitPunct = _f === void 0 ? '' : _f, _g = _b.timeout, timeout = _g === void 0 ? 10000 : _g;
|
125 | return __awaiter(void 0, void 0, void 0, function () {
|
126 | var shortTextList, base64List, result, i, shortText, base64;
|
127 | return __generator(this, function (_h) {
|
128 | switch (_h.label) {
|
129 | case 0:
|
130 | assertInputTypes_1.default(text, lang, slow, host);
|
131 | if (typeof splitPunct !== 'string') {
|
132 | throw new TypeError('splitPunct should be a string');
|
133 | }
|
134 | if (typeof timeout !== 'number' || timeout <= 0) {
|
135 | throw new TypeError('timeout should be a positive number');
|
136 | }
|
137 | shortTextList = splitLongText_1.default(text, { splitPunct: splitPunct });
|
138 | return [4 , Promise.all(shortTextList.map(function (shortText) { return exports.getAudioBase64(shortText, { lang: lang, slow: slow, host: host, timeout: timeout }); }))];
|
139 | case 1:
|
140 | base64List = _h.sent();
|
141 | result = [];
|
142 | for (i = 0; i < shortTextList.length; i++) {
|
143 | shortText = shortTextList[i];
|
144 | base64 = base64List[i];
|
145 | result.push({ shortText: shortText, base64: base64 });
|
146 | }
|
147 | return [2 , result];
|
148 | }
|
149 | });
|
150 | });
|
151 | };
|
152 | exports.getAllAudioBase64 = getAllAudioBase64;
|
153 |
|
\ | No newline at end of file |