m_translator 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85c85368d6c29b50b648dce0ded543a4694b71e5
4
- data.tar.gz: 63f82cc9ee783ec5d6486f5150e8ae34321c3d37
3
+ metadata.gz: cf87d592a2ae54397bef50ef590645e8ac04f7a2
4
+ data.tar.gz: 59a086e33e71aab6601767a002c9acd03e2d7130
5
5
  SHA512:
6
- metadata.gz: b57817bb6e44e2d96f730f4537d6f1309dd91a487c87cf8b66a5982329c0841f757d3dbdeed513515aea0bc4642129c3a14e32c7a9f7af1c26812cba34ed6306
7
- data.tar.gz: 4ca582b6680863f1505aae58b7dab2eca65cf0dd61c978c81addb44cc9e641c1a1908fff2ec047c9d7b9aea50c8d599604ef468ba62b9696c52d9ca11d7826c5
6
+ metadata.gz: a2c29c8f9eaa2b8abc9188f582872f487246c4ef0f80f8fba85e0ad0af4a4ada5ae2ead98a917bf1193db22f3a4196c905829636153eb12344fd3b077f793acf
7
+ data.tar.gz: 423c8cb9236aa6130301599f152afbba2abe61455c00046e617c3498feecf6cb7875749498f495cb3b88fdb2daab64d7201ba651808138d7e882cf3c916ad8fe
@@ -2,9 +2,8 @@ require 'rest-client'
2
2
  require 'json'
3
3
  require 'execjs'
4
4
  require 'active_support/all'
5
- require "m_translator/version"
6
- require 'm_translator/youdao'
7
- require 'm_translator/google'
5
+
6
+ Dir[File.dirname(__FILE__) + '/m_translator/*.rb'].each {|file| require file }
8
7
 
9
8
  module MTranslator
10
9
  class Error < StandardError; end
@@ -0,0 +1,44 @@
1
+ module MTranslator
2
+ class Baidu
3
+ def initialize(from,to,text)
4
+ @url = "https://fanyi.baidu.com/v2transapi"
5
+ @from = from
6
+ @to = to
7
+ @text = text
8
+ end
9
+
10
+ def translator
11
+ rep = RestClient.post @url,req_params,req_headers
12
+ JSON.parse(rep.body)
13
+ end
14
+
15
+ private
16
+
17
+ def token(text,gtk)
18
+ f_path = File.join(File.dirname(__FILE__), '../tk/Baidu.js' )
19
+ f = File.read(f_path)
20
+ context = ExecJS.compile(f)
21
+ context.call('token', text,gtk)
22
+ end
23
+
24
+ def req_params
25
+ params = {}
26
+ params["from"] = @from
27
+ params["to"] = @to
28
+ params["query"] = @text
29
+ params["transtype"] = "translang"
30
+ params["simple_means_flag"] = "3"
31
+ params["sign"] = token(@text,"320305.131321201")
32
+ params["token"] = "7b35624ba7fe34e692ea909140d9582d"
33
+ params
34
+ end
35
+
36
+ def req_headers
37
+ headers = {}
38
+ headers["Cookie"] = "BAIDUID=16FFA1EAAF1A387C647A22DB9FC81DAE:FG=1; BIDUPSID=16FFA1EAAF1A387C647A22DB9FC81DAE; PSTM=1514024118; __cfduid=d2f7fd3a024d1ee90b8a817ddd866d9bc1514812370; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDUSS=E83bFplYnBRen5hV3FKNDZCM3ZSZldWMVBjV1ZGbW9uTTVwcjFYVDQzTGVFTlJhQVFBQUFBJCQAAAAAAAAAAAEAAADnIjgkcG9obG9ndTQxNTA3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN6DrFreg6xaS; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=1429_21103_18559_22075; PSINO=3; locale=zh; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1525654866,1525657996,1525658015,1525671031; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1525671031; to_lang_often=%5B%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%2C%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%5D; from_lang_often=%5B%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%2C%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%5D"
39
+ headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
40
+ headers
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,41 @@
1
+ module MTranslator
2
+ class Sogou
3
+ def initialize(from,to,text)
4
+ @url = "http://fanyi.sogou.com/reventondc/translate"
5
+ @from = from
6
+ @to = to
7
+ @text = text
8
+ end
9
+
10
+ def translator
11
+ rep = RestClient.post @url,req_params
12
+ JSON.parse(rep.body)
13
+ end
14
+
15
+ private
16
+
17
+ def token
18
+ f_path = File.join(File.dirname(__FILE__), '../tk/Sogou.js' )
19
+ f = File.read(f_path)
20
+ context = ExecJS.compile(f)
21
+ context.call('token')
22
+ end
23
+
24
+ def req_params
25
+ params = {}
26
+ params["from"] = @from
27
+ params["to"] = @to
28
+ params["client"] = "pc"
29
+ params["fr"] = "browser_pc"
30
+ params["text"] = @text
31
+ params["useDetect"] = "on"
32
+ params["useDetectResult"] = "on"
33
+ params["needQc"] = "1"
34
+ params["uuid"] = token
35
+ params["oxford"] = "on"
36
+ params["isReturnSugg"] = "off"
37
+ params
38
+ end
39
+
40
+ end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module MTranslator
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,31 @@
1
+ function a(r, o) {
2
+ for (var t = 0; t < o.length - 2; t += 3) {
3
+ var a = o.charAt(t + 2);
4
+ a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a),
5
+ a = "+" === o.charAt(t + 1) ? r >>> a: r << a,
6
+ r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a
7
+ }
8
+ return r
9
+ }
10
+ var C = null;
11
+ var token = function(r, _gtk) {
12
+ var o = r.length;
13
+ o > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(o / 2) - 5, 10) + r.substring(r.length, r.length - 10));
14
+ var t = void 0,
15
+ t = null !== C ? C: (C = _gtk || "") || "";
16
+ for (var e = t.split("."), h = Number(e[0]) || 0, i = Number(e[1]) || 0, d = [], f = 0, g = 0; g < r.length; g++) {
17
+ var m = r.charCodeAt(g);
18
+ 128 > m ? d[f++] = m: (2048 > m ? d[f++] = m >> 6 | 192 : (55296 === (64512 & m) && g + 1 < r.length && 56320 === (64512 & r.charCodeAt(g + 1)) ? (m = 65536 + ((1023 & m) << 10) + (1023 & r.charCodeAt(++g)), d[f++] = m >> 18 | 240, d[f++] = m >> 12 & 63 | 128) : d[f++] = m >> 12 | 224, d[f++] = m >> 6 & 63 | 128), d[f++] = 63 & m | 128)
19
+ }
20
+ for (var S = h,
21
+ u = "+-a^+6",
22
+ l = "+-3^+b+-f",
23
+ s = 0; s < d.length; s++) S += d[s],
24
+ S = a(S, u);
25
+
26
+ return S = a(S, l),
27
+ S ^= i,
28
+ 0 > S && (S = (2147483647 & S) + 2147483648),
29
+ S %= 1e6,
30
+ S.toString() + "." + (S ^ h)
31
+ }
@@ -0,0 +1,6 @@
1
+ function token() {
2
+ return tk = t() + t() + t() + t() + t() + t() + t() + t()
3
+ };
4
+ function t() {
5
+ return Math.floor(65536 * (1 + Math.random())).toString(16).substring(1)
6
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: m_translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - jack
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-14 00:00:00.000000000 Z
11
+ date: 2018-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -125,10 +125,14 @@ files:
125
125
  - bin/console
126
126
  - bin/setup
127
127
  - lib/m_translator.rb
128
+ - lib/m_translator/baidu.rb
128
129
  - lib/m_translator/google.rb
130
+ - lib/m_translator/sogou.rb
129
131
  - lib/m_translator/version.rb
130
132
  - lib/m_translator/youdao.rb
133
+ - lib/tk/Baidu.js
131
134
  - lib/tk/Google.js
135
+ - lib/tk/Sogou.js
132
136
  - m_translator.gemspec
133
137
  homepage: https://github.com/cs-cj/m_translator
134
138
  licenses: []