google_web_translate 0.2.1 → 0.2.2

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: a801716622f94a6f87ac50ea1e348d0c9d91e155
4
- data.tar.gz: 4913fdab2c0ee17de89447b7292d13d1730ddf87
3
+ metadata.gz: 9de16ca14cfa21f2f4e2feba9fb67f1e662d37bc
4
+ data.tar.gz: a80a769dbb0f387f9c5c12c3baa7dc31befd0cf6
5
5
  SHA512:
6
- metadata.gz: 8741be3763bc8f09111bf2f9fd7fb7dc00c75b6304ce9fe4e447a29fabba1d3c3553b7019e9012269d80ea88ab7d49ef51c1744c65d1b8b7f1a76da1063cc2cf
7
- data.tar.gz: 7c440c5f7fb0a4f21efb82b4cd0bcc6322ad3ed003713b0650f7b2864a0fd712830a97eac637aa9517a84be1e8e10960d4d2cc028f5c6bb0e8a862a578214598
6
+ metadata.gz: 576ad67066a1a4ba0fd953743a06a9fc713a229e6db732b5805a77b132b4984a6d7c8f4966d2f35b64f6cca9915888ee10efb3eb542c3896bfc6d3fc7d8af23c
7
+ data.tar.gz: 55a74de3fddd74d6e179cea599ad82ee76e19f0799de07c66f22c25c93593a06515172456a3d6dc0705af963d1c2c54921bb996aab70128b9165cc10593e95cd
data/README.md CHANGED
@@ -34,6 +34,8 @@ Or install it yourself as:
34
34
  result = api.translate("Hello", "en", "de")
35
35
  puts result.translation
36
36
 
37
+ supported_languages = api.languages
38
+
37
39
  ## Development
38
40
 
39
41
  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.
@@ -1,30 +1,30 @@
1
-
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'google_web_translate/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = 'google_web_translate'
8
- spec.version = GoogleWebTranslate::VERSION
9
- spec.authors = ['Andrew']
10
- spec.email = ['sobakasu@gmail.com']
11
-
12
- spec.summary = 'Text translation using the google web interface'
13
- spec.homepage = 'https://github.com/sobakasu/google_web_translate'
14
- spec.license = 'MIT'
15
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
- f.match(%r{^(test|spec|features)/})
17
- end
18
- spec.bindir = 'bin'
19
- spec.executables = %w[google_web_translate]
20
- spec.require_paths = ['lib']
21
-
22
- spec.add_development_dependency 'bundler', '~> 1.16'
23
- spec.add_development_dependency 'rake', '~> 10.0'
24
- spec.add_development_dependency 'rspec', '~> 3.0'
25
- spec.add_development_dependency 'simplecov'
26
- spec.add_development_dependency 'webmock'
27
-
28
- spec.add_dependency 'execjs'
29
- spec.add_dependency 'thor'
30
- end
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'google_web_translate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'google_web_translate'
8
+ spec.version = GoogleWebTranslate::VERSION
9
+ spec.authors = ['Andrew']
10
+ spec.email = ['sobakasu@gmail.com']
11
+
12
+ spec.summary = 'Text translation using the google web interface'
13
+ spec.homepage = 'https://github.com/sobakasu/google_web_translate'
14
+ spec.license = 'MIT'
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = 'bin'
19
+ spec.executables = %w[google_web_translate]
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.16'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
+ spec.add_development_dependency 'simplecov'
26
+ spec.add_development_dependency 'webmock'
27
+
28
+ spec.add_dependency 'execjs'
29
+ spec.add_dependency 'thor'
30
+ end
@@ -1,130 +1,138 @@
1
- require 'execjs'
2
- require 'json'
3
-
4
- module GoogleWebTranslate
5
- # interface to the google web translation api
6
- class API
7
- def initialize(options = {})
8
- @dt = options[:dt] || DEFAULT_DT
9
- @token_ttl = options[:token_ttl] || DEFAULT_TOKEN_TTL
10
- @debug = options[:debug]
11
- @http_client = options[:http_client] || HTTPClient.new(options)
12
- end
13
-
14
- def translate(string, from, to)
15
- data = fetch_translation(string, from, to)
16
- Result.new(data)
17
- end
18
-
19
- private
20
-
21
- URL_MAIN = 'https://translate.google.com'.freeze
22
- URL_TRANSLATE_1 = URL_MAIN + '/translate_a/single'.freeze
23
- DEFAULT_DT = %w[at bd ex ld md qca rw rm ss t].freeze
24
- DEFAULT_TOKEN_TTL = 3600
25
-
26
- def fetch_translation(string, from, to)
27
- json = fetch_url_body(translate_url(string, from, to))
28
- # File.open("response.json", "w") { |f| f.puts json }
29
- JSON.parse(json)
30
- end
31
-
32
- def fetch_url_response(url)
33
- @http_client.get(url.to_s)
34
- end
35
-
36
- def fetch_url_body(url)
37
- uri = URI.parse(url)
38
- uri = URI.join(URL_MAIN, url) if uri.relative?
39
- debug("fetch #{uri}")
40
- response = fetch_url_response(uri)
41
- # debug("response: #{response.body}")
42
- response.body
43
- end
44
-
45
- def valid_token?
46
- @token_updated_at && Time.now - @token_updated_at < @token_ttl
47
- end
48
-
49
- def fetch_main
50
- fetch_url_body(URL_MAIN)
51
- end
52
-
53
- def fetch_desktop_module(html)
54
- html =~ /([^="]*desktop_module_main.js)/
55
- url = Regexp.last_match(1)
56
- raise 'unable to find desktop module' unless url
57
- fetch_url_body(url)
58
- end
59
-
60
- def munge_module(js)
61
- js.gsub(/((?:var\s+)?\w+\s*=\s*\w+\.createElement.*?;)/) do |_i|
62
- 'return "";'
63
- end
64
- end
65
-
66
- def compile_js(html)
67
- desktop_module_js = munge_module(fetch_desktop_module(html))
68
- window_js = File.read(File.join(__dir__, '..', 'js', 'window.js'))
69
- js = window_js + desktop_module_js
70
- File.open('generated.js', 'w') { |f| f.puts(js) } if debug?
71
- ExecJS.compile(js)
72
- end
73
-
74
- def update_token
75
- # download main page
76
- html = fetch_main
77
- # extract tkk from html
78
- @tkk = extract_tkk(html)
79
- # compile desktop module javascript
80
- @js_context = compile_js(html)
81
- @token_updated_at = Time.now
82
- end
83
-
84
- def tk(string)
85
- update_token unless valid_token?
86
- @js_context.call('setWindowProperty', 'TKK', @tkk)
87
- # tk = @js_context.call("wq", string)
88
- tk = @js_context.call('generateToken', string, @tkk)
89
- (tk.split('=') || [])[1]
90
- end
91
-
92
- def tk_js
93
- File.read(File.join(__dir__, 'google_web.js'))
94
- end
95
-
96
- def extract_tkk(html)
97
- raise 'TKK not found' unless html =~ /TKK=eval\('(.*?)'\);/
98
- tkk_code = Regexp.last_match(1)
99
- # tkk_code = Translatomatic::StringEscaping.unescape(tkk_code)
100
- tkk_code = StringEscaping.unescape(tkk_code)
101
- debug("tkk code unescaped: #{tkk_code}")
102
- tkk = ExecJS.eval(tkk_code)
103
- # tkk = context.call(nil)
104
- debug("evaluated tkk: #{tkk}")
105
- tkk
106
- end
107
-
108
- def translate_url(string, from, to)
109
- tk = tk(string)
110
- debug("tk: #{tk}")
111
- query = {
112
- sl: from, tl: to, ie: 'UTF-8', oe: 'UTF-8',
113
- q: string, dt: @dt, tk: tk,
114
- # not sure what these are for
115
- client: 't', hl: 'en', otf: 1, ssel: 4, tsel: 6, kc: 5
116
- }
117
- url = URI.parse(URL_TRANSLATE_1)
118
- url.query = URI.encode_www_form(query)
119
- url.to_s
120
- end
121
-
122
- def debug(msg)
123
- puts msg if debug?
124
- end
125
-
126
- def debug?
127
- @debug
128
- end
129
- end
130
- end
1
+ require 'execjs'
2
+ require 'json'
3
+
4
+ module GoogleWebTranslate
5
+ # interface to the google web translation api
6
+ class API
7
+ def initialize(options = {})
8
+ @dt = options[:dt] || DEFAULT_DT
9
+ @token_ttl = options[:token_ttl] || DEFAULT_TOKEN_TTL
10
+ @debug = options[:debug]
11
+ @http_client = options[:http_client] || HTTPClient.new(options)
12
+ end
13
+
14
+ def translate(string, from, to)
15
+ data = fetch_translation(string, from, to)
16
+ Result.new(data)
17
+ end
18
+
19
+ def languages
20
+ @languages ||= begin
21
+ html = fetch_main
22
+ html.scan(/\['(\w{2})','(\w{2})'\]/).flatten.uniq.sort
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ URL_MAIN = 'https://translate.google.com'.freeze
29
+ URL_TRANSLATE_1 = URL_MAIN + '/translate_a/single'.freeze
30
+ DEFAULT_DT = %w[at bd ex ld md qca rw rm ss t].freeze
31
+ DEFAULT_TOKEN_TTL = 3600
32
+
33
+ def fetch_translation(string, from, to)
34
+ json = fetch_url_body(translate_url(string, from, to))
35
+ # File.open("response.json", "w") { |f| f.puts json }
36
+ JSON.parse(json)
37
+ end
38
+
39
+ def fetch_url_response(url)
40
+ @http_client.get(url.to_s)
41
+ end
42
+
43
+ def fetch_url_body(url)
44
+ uri = URI.parse(url)
45
+ uri = URI.join(URL_MAIN, url) if uri.relative?
46
+ debug("fetch #{uri}")
47
+ response = fetch_url_response(uri)
48
+ # debug("response: #{response.body}")
49
+ response.body
50
+ end
51
+
52
+ def valid_token?
53
+ @token_updated_at && Time.now - @token_updated_at < @token_ttl
54
+ end
55
+
56
+ def fetch_main(options = {})
57
+ @html = nil if options[:no_cache]
58
+ @html ||= fetch_url_body(URL_MAIN)
59
+ end
60
+
61
+ def fetch_desktop_module(html)
62
+ html =~ /([^="]*desktop_module_main.js)/
63
+ url = Regexp.last_match(1)
64
+ raise 'unable to find desktop module' unless url
65
+ fetch_url_body(url)
66
+ end
67
+
68
+ def munge_module(js)
69
+ js.gsub(/((?:var\s+)?\w+\s*=\s*\w+\.createElement.*?;)/) do |_i|
70
+ 'return "";'
71
+ end
72
+ end
73
+
74
+ def compile_js(html)
75
+ desktop_module_js = munge_module(fetch_desktop_module(html))
76
+ window_js = File.read(File.join(__dir__, '..', 'js', 'window.js'))
77
+ js = window_js + desktop_module_js
78
+ File.open('generated.js', 'w') { |f| f.puts(js) } if debug?
79
+ ExecJS.compile(js)
80
+ end
81
+
82
+ def update_token
83
+ # download main page
84
+ html = fetch_main(no_cache: true)
85
+ # extract tkk from html
86
+ @tkk = extract_tkk(html)
87
+ # compile desktop module javascript
88
+ @js_context = compile_js(html)
89
+ @token_updated_at = Time.now
90
+ end
91
+
92
+ def tk(string)
93
+ update_token unless valid_token?
94
+ @js_context.call('setWindowProperty', 'TKK', @tkk)
95
+ # tk = @js_context.call("wq", string)
96
+ tk = @js_context.call('generateToken', string, @tkk)
97
+ (tk.split('=') || [])[1]
98
+ end
99
+
100
+ def tk_js
101
+ File.read(File.join(__dir__, 'google_web.js'))
102
+ end
103
+
104
+ def extract_tkk(html)
105
+ raise 'TKK not found' unless html =~ /TKK=eval\('(.*?)'\);/
106
+ tkk_code = Regexp.last_match(1)
107
+ # tkk_code = Translatomatic::StringEscaping.unescape(tkk_code)
108
+ tkk_code = StringEscaping.unescape(tkk_code)
109
+ debug("tkk code unescaped: #{tkk_code}")
110
+ tkk = ExecJS.eval(tkk_code)
111
+ # tkk = context.call(nil)
112
+ debug("evaluated tkk: #{tkk}")
113
+ tkk
114
+ end
115
+
116
+ def translate_url(string, from, to)
117
+ tk = tk(string)
118
+ debug("tk: #{tk}")
119
+ query = {
120
+ sl: from, tl: to, ie: 'UTF-8', oe: 'UTF-8',
121
+ q: string, dt: @dt, tk: tk,
122
+ # not sure what these are for
123
+ client: 't', hl: 'en', otf: 1, ssel: 4, tsel: 6, kc: 5
124
+ }
125
+ url = URI.parse(URL_TRANSLATE_1)
126
+ url.query = URI.encode_www_form(query)
127
+ url.to_s
128
+ end
129
+
130
+ def debug(msg)
131
+ puts msg if debug?
132
+ end
133
+
134
+ def debug?
135
+ @debug
136
+ end
137
+ end
138
+ end
@@ -1,18 +1,18 @@
1
- require 'thor'
2
- require 'pp'
3
-
4
- module GoogleWebTranslate
5
- # Command line interface
6
- class CLI < Thor
7
- desc 'string from to', 'translate a string from one language to another'
8
- method_option :dt, type: :array, desc: 'data types'
9
- def translate(string, from, to)
10
- api_options = { debug: ENV['DEBUG'] }
11
- api_options[:dt] = options[:dt] if options[:dt]
12
-
13
- api = API.new(api_options)
14
- result = api.translate(string, from, to)
15
- pp result.to_h
16
- end
17
- end
18
- end
1
+ require 'thor'
2
+ require 'pp'
3
+
4
+ module GoogleWebTranslate
5
+ # Command line interface
6
+ class CLI < Thor
7
+ desc 'string from to', 'translate a string from one language to another'
8
+ method_option :dt, type: :array, desc: 'data types'
9
+ def translate(string, from, to)
10
+ api_options = { debug: ENV['DEBUG'] }
11
+ api_options[:dt] = options[:dt] if options[:dt]
12
+
13
+ api = API.new(api_options)
14
+ result = api.translate(string, from, to)
15
+ pp result.to_h
16
+ end
17
+ end
18
+ end
@@ -1,26 +1,26 @@
1
- require 'net/http'
2
-
3
- module GoogleWebTranslate
4
- # HTTP client functionality
5
- class HTTPClient
6
- def self.user_agent
7
- gem_version = "GoogleWebTranslate/#{VERSION}"
8
- platform_version = "(#{RUBY_PLATFORM}) #{RUBY_ENGINE}/#{RUBY_VERSION}"
9
- gem_version + ' ' + platform_version
10
- end
11
-
12
- def initialize(options = {})
13
- @user_agent = options[:user_agent] || self.class.user_agent
14
- end
15
-
16
- def get(url)
17
- uri = URI.parse(url)
18
- request = Net::HTTP::Get.new(uri)
19
- request['User-Agent'] = @user_agent
20
- options = { use_ssl: uri.scheme == 'https' }
21
- Net::HTTP.start(uri.host, uri.port, options) do |http|
22
- http.request(request)
23
- end
24
- end
25
- end
26
- end
1
+ require 'net/http'
2
+
3
+ module GoogleWebTranslate
4
+ # HTTP client functionality
5
+ class HTTPClient
6
+ def self.user_agent
7
+ gem_version = "GoogleWebTranslate/#{VERSION}"
8
+ platform_version = "(#{RUBY_PLATFORM}) #{RUBY_ENGINE}/#{RUBY_VERSION}"
9
+ gem_version + ' ' + platform_version
10
+ end
11
+
12
+ def initialize(options = {})
13
+ @user_agent = options[:user_agent] || self.class.user_agent
14
+ end
15
+
16
+ def get(url)
17
+ uri = URI.parse(url)
18
+ request = Net::HTTP::Get.new(uri)
19
+ request['User-Agent'] = @user_agent
20
+ options = { use_ssl: uri.scheme == 'https' }
21
+ Net::HTTP.start(uri.host, uri.port, options) do |http|
22
+ http.request(request)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,54 +1,54 @@
1
- module GoogleWebTranslate
2
- # Translation results
3
- class Result
4
- attr_reader :raw
5
-
6
- # @private
7
- DATA_INDICES = {
8
- translation: [0, 0, 0], # dt:t
9
- alternatives: [5, 0, 2], # dt:at
10
- dictionary: [1], # dt: bd
11
- synonyms: [11], # dt:ss
12
- definitions: [12, 0], # dt:md
13
- examples: [13, 0], # dt:ex
14
- see_also: [14, 0], # dt:rw
15
- }.freeze
16
-
17
- DATA_INDICES.each_key { |key| attr_reader key }
18
-
19
- def initialize(data)
20
- @raw = data
21
- @keys = []
22
- @properties = {}
23
-
24
- DATA_INDICES.each do |key, indices|
25
- indices = indices.dup
26
- extract_data(key, *indices)
27
- end
28
-
29
- @alternatives = @alternatives.collect { |i| i[0] } if @alternatives
30
- @keys.each { |key| @properties[key] = instance_variable_get("@#{key}") }
31
- end
32
-
33
- def to_h
34
- @properties
35
- end
36
-
37
- private
38
-
39
- def extract_data(name, *indices)
40
- value = array_value(@raw, *indices)
41
- return if value.nil?
42
- instance_variable_set("@#{name}", value)
43
- @keys.push(name)
44
- end
45
-
46
- def array_value(array, *indices)
47
- return nil if array.nil?
48
- index = indices.shift
49
- value = array[index]
50
- return value if indices.empty?
51
- array_value(value, *indices)
52
- end
53
- end
54
- end
1
+ module GoogleWebTranslate
2
+ # Translation results
3
+ class Result
4
+ attr_reader :raw
5
+
6
+ # @private
7
+ DATA_INDICES = {
8
+ translation: [0, 0, 0], # dt:t
9
+ alternatives: [5, 0, 2], # dt:at
10
+ dictionary: [1], # dt: bd
11
+ synonyms: [11], # dt:ss
12
+ definitions: [12, 0], # dt:md
13
+ examples: [13, 0], # dt:ex
14
+ see_also: [14, 0], # dt:rw
15
+ }.freeze
16
+
17
+ DATA_INDICES.each_key { |key| attr_reader key }
18
+
19
+ def initialize(data)
20
+ @raw = data
21
+ @keys = []
22
+ @properties = {}
23
+
24
+ DATA_INDICES.each do |key, indices|
25
+ indices = indices.dup
26
+ extract_data(key, *indices)
27
+ end
28
+
29
+ @alternatives = @alternatives.collect { |i| i[0] } if @alternatives
30
+ @keys.each { |key| @properties[key] = instance_variable_get("@#{key}") }
31
+ end
32
+
33
+ def to_h
34
+ @properties
35
+ end
36
+
37
+ private
38
+
39
+ def extract_data(name, *indices)
40
+ value = array_value(@raw, *indices)
41
+ return if value.nil?
42
+ instance_variable_set("@#{name}", value)
43
+ @keys.push(name)
44
+ end
45
+
46
+ def array_value(array, *indices)
47
+ return nil if array.nil?
48
+ index = indices.shift
49
+ value = array[index]
50
+ return value if indices.empty?
51
+ array_value(value, *indices)
52
+ end
53
+ end
54
+ end
@@ -1,3 +1,3 @@
1
- module GoogleWebTranslate
2
- VERSION = '0.2.1'.freeze
3
- end
1
+ module GoogleWebTranslate
2
+ VERSION = '0.2.2'.freeze
3
+ end
@@ -1,5 +1,5 @@
1
- require 'google_web_translate/version.rb'
2
- require 'google_web_translate/string_escaping.rb'
3
- require 'google_web_translate/http_client.rb'
4
- require 'google_web_translate/result.rb'
5
- require 'google_web_translate/api.rb'
1
+ require 'google_web_translate/version.rb'
2
+ require 'google_web_translate/string_escaping.rb'
3
+ require 'google_web_translate/http_client.rb'
4
+ require 'google_web_translate/result.rb'
5
+ require 'google_web_translate/api.rb'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_web_translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-22 00:00:00.000000000 Z
11
+ date: 2018-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler