Skip to content

Commit

Permalink
Merge branch 'v2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
xfslove committed Aug 25, 2020
2 parents 63af11f + 279df3f commit dfbf2a3
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -42,6 +42,7 @@ The workflow will attempt to correct spelling mistakes which can be accepted wit
| domain | https://translate.google.com | if you cannot access the default domain, you can config this. <br />大陆访问不了默认域名,所以如果使用2.x版本需要将这个变量设置为https://translate.google.cn. 或者还是使用[1.x版本](https://github.com/xfslove/alfred-google-translate/tree/v1.x) |
| voice | remote | avaliable values: <br />remote: fetch voice from google, <br />local: use macOS local voice (notice: maybe only works on English),<br />none: dont use voice |
| save_count | 20 | limit the translation history, see [alfred-translate-history](https://github.com/xfslove/alfred-translate-history). <br />a value of 0 will keep no history |
| socks_proxy| - | not turned by default. you can specify local or remote socks proxy. format: `socks://{host}:{port}` example: local shadowsocks proxy 'socks://127.0.0.1:1086' |

##### environment variables config snapshot:

Expand Down
16 changes: 11 additions & 5 deletions index.js
Expand Up @@ -8,11 +8,13 @@ var uuidv4 = require('uuid/v4');
var languagePair = new configstore('language-config-pair');
var history = new configstore("translate-history");
var languages = require("./languages");
var SocksProxyAgent = require('socks-proxy-agent');

var g_config = {
voice: process.env.voice || 'remote',
save: process.env.save_count || 20,
domain: process.env.domain || 'https://translate.google.com'
domain: process.env.domain || 'https://translate.google.com',
agent: process.env.socks_proxy ? new SocksProxyAgent(process.env.socks_proxy) : undefined
};

var pair = languagePair.get('pair');
Expand Down Expand Up @@ -40,7 +42,8 @@ if (pair) {
from: 'auto',
to: 'en',
domain: g_config.domain,
client: 'gtx'
client: 'gtx',
agent: g_config.agent
})
.then(function (res) {
var detect = res.from.language.iso;
Expand Down Expand Up @@ -99,7 +102,8 @@ function doTranslate(opts) {
from: opts.from.language,
to: opts.to.language,
domain: g_config.domain,
client: 'gtx'
client: 'gtx',
agent: g_config.agent
})
.then(function (res) {
var items = [];
Expand Down Expand Up @@ -218,15 +222,17 @@ function doTranslate(opts) {
to: res.from.language.iso,
domain: g_config.domain,
file: res.from.language.ttsfile,
client: 'gtx'
client: 'gtx',
agent: g_config.agent
});
var toArray = [];
res.to.text.array.forEach(o => tts.split(o).forEach(t => toArray.push(t)));
tts.multi(toArray, {
to: res.to.language.iso,
domain: g_config.domain,
file: res.to.language.ttsfile,
client: 'gtx'
client: 'gtx',
agent: g_config.agent
});
}
})
Expand Down
5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "alfred-google-translate",
"version": "2.0.8",
"version": "2.0.9",
"description": "Alfred 3 workflow to translate with google translate api",
"license": "MIT",
"repository": "xfslove/alfred-google-translate",
Expand Down Expand Up @@ -39,7 +39,8 @@
"alfy": "^0.9.0",
"configstore": "^2.0.0",
"got": "^6.3.0",
"uuid": "^3.3.2"
"uuid": "^3.3.2",
"socks-proxy-agent": "^4.0.2"
},
"devDependencies": {
"alfy-test": "^0.3.0",
Expand Down
6 changes: 3 additions & 3 deletions token.js
Expand Up @@ -73,14 +73,14 @@ var window = {
TKK: config.get('TKK') || '0'
};

function updateTKK(url) {
function updateTKK(opts) {
return new Promise(function (resolve, reject) {
var now = Math.floor(Date.now() / 3600000);

if (Number(window.TKK.split('.')[0]) === now) {
resolve();
} else {
got(url).then(function (res) {
got(opts.domain, {agent: opts.agent}).then(function (res) {
var matches = res.body.match(/tkk:\s?'(.+?)'/i);

if (matches) {
Expand All @@ -107,7 +107,7 @@ function updateTKK(url) {
function get(text, opts) {
opts = opts || {};

return updateTKK(opts.domain).then(function () {
return updateTKK(opts).then(function () {
var tk = sM(text);
tk = tk.replace('&tk=', '');
return {name: 'tk', value: tk};
Expand Down
2 changes: 1 addition & 1 deletion translate.js
Expand Up @@ -25,7 +25,7 @@ function translate(text, opts) {
data[token.name] = token.value;
return url + '?' + querystring.stringify(data);
}).then(function (url) {
return got(url).then(function (res) {
return got(url, {agent: opts.agent}).then(function (res) {
var body = JSON.parse(res.body);

var result = {
Expand Down
2 changes: 1 addition & 1 deletion tts.js
Expand Up @@ -77,7 +77,7 @@ function single(text, opts) {
data[token.name] = token.value;
return url + '?' + querystring.stringify(data);
}).then(function (url) {
return got(url, {encoding: null}).then(function (res) {
return got(url, {encoding: null, agent: opts.agent}).then(function (res) {
fs.appendFile(opts.file, res.body, function (err) {
if (err) throw err;
});
Expand Down

0 comments on commit dfbf2a3

Please sign in to comment.