m_translator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 85c85368d6c29b50b648dce0ded543a4694b71e5
4
+ data.tar.gz: 63f82cc9ee783ec5d6486f5150e8ae34321c3d37
5
+ SHA512:
6
+ metadata.gz: b57817bb6e44e2d96f730f4537d6f1309dd91a487c87cf8b66a5982329c0841f757d3dbdeed513515aea0bc4642129c3a14e32c7a9f7af1c26812cba34ed6306
7
+ data.tar.gz: 4ca582b6680863f1505aae58b7dab2eca65cf0dd61c978c81addb44cc9e641c1a1908fff2ec047c9d7b9aea50c8d599604ef468ba62b9696c52d9ca11d7826c5
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .DS_Store
13
+ .idea
14
+ lib/.DS_Store
15
+ m_translator-0.1.0.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.4
7
+ before_install: gem install bundler -v 1.17.1
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ if ENV['TRAVIS']
2
+ source 'https://rubygems.org'
3
+ else
4
+ source 'https://gems.ruby-china.com'
5
+ end
6
+
7
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
8
+
9
+ # Specify your gem's dependencies in m_translator.gemspec
10
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ m_translator (0.1.0)
5
+ activesupport
6
+ execjs (>= 2.7.0)
7
+ json (>= 1.8.6)
8
+ rest-client (>= 2.0)
9
+
10
+ GEM
11
+ remote: https://gems.ruby-china.com/
12
+ specs:
13
+ activesupport (5.0.1)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (~> 0.7)
16
+ minitest (~> 5.1)
17
+ tzinfo (~> 1.1)
18
+ concurrent-ruby (1.0.5)
19
+ diff-lcs (1.3)
20
+ domain_name (0.5.20170404)
21
+ unf (>= 0.0.5, < 1.0.0)
22
+ execjs (2.7.0)
23
+ http-cookie (1.0.3)
24
+ domain_name (~> 0.5)
25
+ i18n (0.9.5)
26
+ concurrent-ruby (~> 1.0)
27
+ json (1.8.6)
28
+ mime-types (3.1)
29
+ mime-types-data (~> 3.2015)
30
+ mime-types-data (3.2016.0521)
31
+ minitest (5.11.3)
32
+ netrc (0.11.0)
33
+ rake (10.5.0)
34
+ rest-client (2.0.2)
35
+ http-cookie (>= 1.0.2, < 2.0)
36
+ mime-types (>= 1.16, < 4.0)
37
+ netrc (~> 0.8)
38
+ rspec (3.5.0)
39
+ rspec-core (~> 3.5.0)
40
+ rspec-expectations (~> 3.5.0)
41
+ rspec-mocks (~> 3.5.0)
42
+ rspec-core (3.5.4)
43
+ rspec-support (~> 3.5.0)
44
+ rspec-expectations (3.5.0)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.5.0)
47
+ rspec-mocks (3.5.0)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.5.0)
50
+ rspec-support (3.5.0)
51
+ thread_safe (0.3.6)
52
+ tzinfo (1.2.5)
53
+ thread_safe (~> 0.1)
54
+ unf (0.1.4)
55
+ unf_ext
56
+ unf_ext (0.0.7.4)
57
+
58
+ PLATFORMS
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ bundler (~> 1.14)
63
+ m_translator!
64
+ rake (~> 10.0)
65
+ rspec (~> 3.0)
66
+
67
+ BUNDLED WITH
68
+ 1.14.5
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # MTranslator
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/m_translator`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem!
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'm_translator'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install m_translator
22
+
23
+ ## Usage
24
+
25
+ ``` ruby
26
+ g =MTranslator::Google.new("en","zh-CN","Hello, Who are you")
27
+ g.translator
28
+ y =MTranslator::Youdao.new("zh-CHS","en","你是谁")
29
+ y.translator
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/m_translator.
41
+
42
+ ## Special thanks
43
+
44
+ https://github.com/hujingshuang/MTrans
45
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "m_translator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ module MTranslator
2
+ class Google
3
+ def initialize(from,to,text)
4
+ @url = "https://translate.google.cn/translate_a/single"
5
+ @from = from
6
+ @to = to
7
+ @text = text
8
+ end
9
+
10
+ def translator
11
+ rep = RestClient.get "#{@url}?#{req_params}"
12
+ JSON.parse(rep.body)
13
+ end
14
+
15
+ private
16
+ def token(text)
17
+ f_path = File.join(File.dirname(__FILE__), '../tk/Google.js' )
18
+ f = File.read(f_path)
19
+ context = ExecJS.compile(f)
20
+ context.call('token', text)
21
+ end
22
+
23
+ def req_params
24
+ params = {}
25
+ params["client"] = "t"
26
+ params["sl"] = @from
27
+ params["tl"] = @to
28
+ params["hl"] = "zh-CN"
29
+ params["dt"] = "t"
30
+ params["ie"] = "UTF-8"
31
+ params["oe"] = "UTF-8"
32
+ params["source"] = "btn"
33
+ params["ssel"] = 0
34
+ params["tsel"] = 0
35
+ params["kc"] = 0
36
+ params["tk"] = token(@text)
37
+ params["q"] = @text
38
+ params.to_query
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module MTranslator
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,56 @@
1
+ module MTranslator
2
+ class Youdao
3
+ # 初始化
4
+ def initialize(from, to, text)
5
+ @url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
6
+ @from = from
7
+ @to = to
8
+ @text = text
9
+ @u = "fanyideskweb"
10
+ @c = "ebSeFb%=XZ%T[KZ)c(sy!"
11
+ @f = "#{ctime}"+"#{rand(10 + 1)}"
12
+ end
13
+
14
+ def translator
15
+ rep = RestClient.post @url,req_params,req_headers
16
+ JSON.parse(rep.body)
17
+ end
18
+
19
+ private
20
+
21
+ def req_params
22
+ params = {}
23
+ params["i"] = @text
24
+ params["from"] = @from
25
+ params["to"] = @to
26
+ params["smartresult"] = "dict"
27
+ params["client"] = "fanyideskweb"
28
+ params["salt"] = @f
29
+ params["sign"] = sign
30
+ params["doctype"] = "json"
31
+ params["version"] = "2.1"
32
+ params["keyfrom"] = "fanyi.web"
33
+ params["action"] = "FY_BY_CLICKBUTTION"
34
+ params["typoResult"] = "false"
35
+ params
36
+ end
37
+
38
+ def req_headers
39
+ headers = {}
40
+ headers["Cookie"] = "OUTFOX_SEARCH_USER_ID_NCOO=1537643834.9570553; OUTFOX_SEARCH_USER_ID=1799185238@10.169.0.83; fanyi-ad-id=43155; fanyi-ad-closed=1; JSESSIONID=aaaBwRanNsqoobhgvaHmw; _ntes_nnid=07e771bc10603d984c2dc8045a293d30,1525267244050; ___rl__test__cookies=#{ctime}"
41
+ headers["Referer"] = "http://fanyi.youdao.com/"
42
+ 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"
43
+ headers
44
+ end
45
+
46
+ # 加密方法
47
+ def sign
48
+ Digest::MD5.hexdigest("#{@u}#{@text}#{@f}#{@c}".encode('utf-8'))
49
+ end
50
+
51
+ # 获取当前时间毫秒 保留13位
52
+ def ctime
53
+ (Time.now.to_f * 1000).to_i
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,12 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+ require 'execjs'
4
+ require 'active_support/all'
5
+ require "m_translator/version"
6
+ require 'm_translator/youdao'
7
+ require 'm_translator/google'
8
+
9
+ module MTranslator
10
+ class Error < StandardError; end
11
+ # Your code goes here...
12
+ end
data/lib/tk/Google.js ADDED
@@ -0,0 +1,35 @@
1
+
2
+ function token(a) {
3
+ var k = "";
4
+ var b = 406644;
5
+ var b1 = 3293161072;
6
+
7
+ var jd = ".";
8
+ var sb = "+-a^+6";
9
+ var Zb = "+-3^+b+-f";
10
+
11
+ for (var e = [], f = 0, g = 0; g < a.length; g++) {
12
+ var m = a.charCodeAt(g);
13
+ 128 > m ? e[f++] = m: (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023), e[f++] = m >> 18 | 240, e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224, e[f++] = m >> 6 & 63 | 128), e[f++] = m & 63 | 128)
14
+ }
15
+ a = b;
16
+ for (f = 0; f < e.length; f++) a += e[f],
17
+ a = RL(a, sb);
18
+ a = RL(a, Zb);
19
+ a ^= b1 || 0;
20
+ 0 > a && (a = (a & 2147483647) + 2147483648);
21
+ a %= 1E6;
22
+ return a.toString() + jd + (a ^ b)
23
+ };
24
+
25
+ function RL(a, b) {
26
+ var t = "a";
27
+ var Yb = "+";
28
+ for (var c = 0; c < b.length - 2; c += 3) {
29
+ var d = b.charAt(c + 2),
30
+ d = d >= t ? d.charCodeAt(0) - 87 : Number(d),
31
+ d = b.charAt(c + 1) == Yb ? a >>> d: a << d;
32
+ a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d
33
+ }
34
+ return a
35
+ }
@@ -0,0 +1,48 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "m_translator/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "m_translator"
8
+ spec.version = MTranslator::VERSION
9
+ spec.authors = ["jack"]
10
+ spec.email = ["qinshenyong@socialdatamax.com"]
11
+
12
+ spec.summary = 'a gem is translation package'
13
+ spec.description = 'a gem is translation package'
14
+ spec.homepage = "https://github.com/cs-cj/m_translator"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/cs-cj/m_translator"
23
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+
38
+ spec.require_paths = ["lib"]
39
+
40
+ spec.add_dependency 'rest-client', ['>= 2.0']
41
+ spec.add_dependency 'json', ['>=1.8.6']
42
+ spec.add_dependency 'execjs',['>=2.7.0']
43
+ spec.add_dependency 'activesupport'
44
+
45
+ spec.add_development_dependency "bundler", "~> 1.14"
46
+ spec.add_development_dependency "rake", "~> 10.0"
47
+ spec.add_development_dependency "rspec", "~> 3.0"
48
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: m_translator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jack
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.8.6
41
+ - !ruby/object:Gem::Dependency
42
+ name: execjs
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.7.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.7.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.14'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.14'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ description: a gem is translation package
112
+ email:
113
+ - qinshenyong@socialdatamax.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - lib/m_translator.rb
128
+ - lib/m_translator/google.rb
129
+ - lib/m_translator/version.rb
130
+ - lib/m_translator/youdao.rb
131
+ - lib/tk/Google.js
132
+ - m_translator.gemspec
133
+ homepage: https://github.com/cs-cj/m_translator
134
+ licenses: []
135
+ metadata:
136
+ allowed_push_host: https://rubygems.org
137
+ homepage_uri: https://github.com/cs-cj/m_translator
138
+ source_code_uri: https://github.com/cs-cj/m_translator
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.5.1
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: a gem is translation package
159
+ test_files: []