google_web_translate 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/google_web_translate +7 -0
- data/bin/setup +8 -0
- data/google-web-translate.gemspec +31 -0
- data/lib/google_web_translate.rb +4 -0
- data/lib/google_web_translate/api.rb +141 -0
- data/lib/google_web_translate/cli.rb +16 -0
- data/lib/google_web_translate/result.rb +48 -0
- data/lib/google_web_translate/string_escaping.rb +53 -0
- data/lib/google_web_translate/version.rb +3 -0
- data/lib/js/window.js +17 -0
- metadata +176 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d7223fdbd591202fc83a78b390c03e29cc48e6de
|
4
|
+
data.tar.gz: 8c00a999400be1a01f40e69d9fbc0786232c2288
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aeb626c15ad4c175a9f83cdedd78ee60918fd5641555fee7f99d7370d0d8d7ce3ef8667f918f552ab3939f23f6e746a91328446884bd5dd8f31bb118bb6694d4
|
7
|
+
data.tar.gz: 61899a7e320e66ab08b1170699856e7ccd1255f734b63e6166972b04d41fbd2d604796e7807b9a082985f1770843d28730fed577c9126df8d7126c7febe0f309
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at sobakasu@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
|
+
|
5
|
+
def gem_installed?(args)
|
6
|
+
name, *version = *args
|
7
|
+
dependency = Gem::Dependency.new(name, *version)
|
8
|
+
specs = dependency.matching_specs
|
9
|
+
specs && !specs.empty?
|
10
|
+
end
|
11
|
+
|
12
|
+
def optional_gem(*args)
|
13
|
+
gem(*args) if gem_installed?(args)
|
14
|
+
end
|
15
|
+
|
16
|
+
# database adapters
|
17
|
+
optional_gem 'therubyracer', platform: :ruby
|
18
|
+
optional_gem 'therubyrhino', platform: :jruby
|
19
|
+
|
20
|
+
# Specify your gem's dependencies in google-web-translate.gemspec
|
21
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Andrew
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# GoogleWebTranslate
|
2
|
+
|
3
|
+
Translates text using the Google translate web interface.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'google_web_translate'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install google_web_translate
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Command line
|
24
|
+
|
25
|
+
google_web_translate "Hello" en de
|
26
|
+
|
27
|
+
### API
|
28
|
+
|
29
|
+
api = GoogleWebTranslate::API.new
|
30
|
+
result = api.translate("Hello", "en", "de")
|
31
|
+
puts result.translation
|
32
|
+
|
33
|
+
## Development
|
34
|
+
|
35
|
+
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.
|
36
|
+
|
37
|
+
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).
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sobakasu/google_web_translate. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
42
|
+
|
43
|
+
## License
|
44
|
+
|
45
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
46
|
+
|
47
|
+
## Code of Conduct
|
48
|
+
|
49
|
+
Everyone interacting in the GoogleWebTranslate project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sobakasu/google_web_translate/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'google_web_translate'
|
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,31 @@
|
|
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 'rest-client'
|
30
|
+
spec.add_dependency 'thor'
|
31
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'execjs'
|
2
|
+
require 'json'
|
3
|
+
require 'rest-client'
|
4
|
+
|
5
|
+
module GoogleWebTranslate
|
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
|
+
@user_agent = options[:user_agent] || DEFAULT_USER_AGENT
|
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
|
+
DEFAULT_USER_AGENT = "GoogleWebTranslate #{VERSION}".freeze
|
26
|
+
|
27
|
+
def fetch_translation(string, from, to)
|
28
|
+
json = fetch_url_body(translate_url(string, from, to))
|
29
|
+
# File.open("response.json", "w") { |f| f.puts json }
|
30
|
+
JSON.parse(json)
|
31
|
+
end
|
32
|
+
|
33
|
+
def fetch_url_response(url)
|
34
|
+
RestClient.get(url.to_s)
|
35
|
+
end
|
36
|
+
|
37
|
+
def fetch_url_body(url)
|
38
|
+
uri = URI.parse(url)
|
39
|
+
uri = URI.join(URL_MAIN, url) if uri.relative?
|
40
|
+
debug("fetch #{uri}")
|
41
|
+
response = fetch_url_response(uri)
|
42
|
+
# debug("response: #{response.body}")
|
43
|
+
response.body
|
44
|
+
end
|
45
|
+
|
46
|
+
def valid_token?
|
47
|
+
@token_updated_at && Time.now - @token_updated_at < @token_ttl
|
48
|
+
end
|
49
|
+
|
50
|
+
def fetch_main
|
51
|
+
fetch_url_body(URL_MAIN)
|
52
|
+
end
|
53
|
+
|
54
|
+
def fetch_desktop_module(html)
|
55
|
+
html =~ /([^="]*desktop_module_main.js)/
|
56
|
+
url = Regexp.last_match(1)
|
57
|
+
raise 'unable to find desktop module' unless url
|
58
|
+
fetch_url_body(url)
|
59
|
+
end
|
60
|
+
|
61
|
+
def munge_module(js)
|
62
|
+
js.gsub(/((?:var\s+)?\w+\s*=\s*\w+\.createElement.*?;)/) do |_i|
|
63
|
+
'return "";'
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def compile_js(html)
|
68
|
+
desktop_module_js = munge_module(fetch_desktop_module(html))
|
69
|
+
window_js = File.read(File.join(__dir__, '..', 'js', 'window.js'))
|
70
|
+
js = window_js + desktop_module_js
|
71
|
+
File.open('generated.js', 'w') { |f| f.puts(js) } if debug?
|
72
|
+
ExecJS.compile(js)
|
73
|
+
end
|
74
|
+
|
75
|
+
def update_token
|
76
|
+
# download main page
|
77
|
+
html = fetch_main
|
78
|
+
# extract tkk from html
|
79
|
+
@tkk = extract_tkk(html)
|
80
|
+
# compile desktop module javascript
|
81
|
+
@js_context = compile_js(html)
|
82
|
+
@token_updated_at = Time.now
|
83
|
+
end
|
84
|
+
|
85
|
+
def tk(string)
|
86
|
+
update_token unless valid_token?
|
87
|
+
@js_context.call('setWindowProperty', 'TKK', @tkk)
|
88
|
+
# tk = @js_context.call("wq", string)
|
89
|
+
tk = @js_context.call('generateToken', string, @tkk)
|
90
|
+
(tk.split('=') || [])[1]
|
91
|
+
end
|
92
|
+
|
93
|
+
def tk_js
|
94
|
+
File.read(File.join(__dir__, 'google_web.js'))
|
95
|
+
end
|
96
|
+
|
97
|
+
def extract_tkk(html)
|
98
|
+
raise 'TKK not found' unless html =~ /TKK=.*?\{(.*?)\}/
|
99
|
+
tkk_code = Regexp.last_match(1)
|
100
|
+
# tkk_code = Translatomatic::StringEscaping.unescape(tkk_code)
|
101
|
+
tkk_code = StringEscaping.unescape(tkk_code)
|
102
|
+
debug("tkk code unescaped: #{tkk_code}")
|
103
|
+
context = ExecJS.compile(tkk_code)
|
104
|
+
tkk = context.call(nil)
|
105
|
+
debug("evaluated tkk: #{tkk}")
|
106
|
+
tkk
|
107
|
+
end
|
108
|
+
|
109
|
+
def translate_url(string, from, to)
|
110
|
+
tk = tk(string)
|
111
|
+
debug("tk: #{tk}")
|
112
|
+
query = {
|
113
|
+
sl: from,
|
114
|
+
tl: to,
|
115
|
+
ie: 'UTF-8',
|
116
|
+
oe: 'UTF-8',
|
117
|
+
q: string,
|
118
|
+
dt: @dt,
|
119
|
+
tk: tk,
|
120
|
+
# not sure what these are for
|
121
|
+
client: 't',
|
122
|
+
hl: 'en',
|
123
|
+
otf: 1,
|
124
|
+
ssel: 4,
|
125
|
+
tsel: 6,
|
126
|
+
kc: 5
|
127
|
+
}
|
128
|
+
url = URI.parse(URL_TRANSLATE_1)
|
129
|
+
url.query = URI.encode_www_form(query)
|
130
|
+
url.to_s
|
131
|
+
end
|
132
|
+
|
133
|
+
def debug(msg)
|
134
|
+
puts msg if debug?
|
135
|
+
end
|
136
|
+
|
137
|
+
def debug?
|
138
|
+
@debug
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'thor'
|
2
|
+
|
3
|
+
module GoogleWebTranslate
|
4
|
+
class CLI < Thor
|
5
|
+
desc 'string from to', 'translate a string from one language to another'
|
6
|
+
method_option :dt, type: :array, desc: 'data types'
|
7
|
+
def translate(string, from, to)
|
8
|
+
api_options = { debug: ENV['DEBUG'] }
|
9
|
+
api_options[:dt] = options[:dt] if options[:dt]
|
10
|
+
|
11
|
+
api = API.new(api_options)
|
12
|
+
result = api.translate(string, from, to)
|
13
|
+
p result.to_h
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module GoogleWebTranslate
|
2
|
+
class Result
|
3
|
+
attr_reader :raw
|
4
|
+
|
5
|
+
attr_reader :translation
|
6
|
+
attr_reader :alternatives
|
7
|
+
attr_reader :dictionary
|
8
|
+
attr_reader :examples
|
9
|
+
|
10
|
+
def initialize(data)
|
11
|
+
@raw = data
|
12
|
+
@keys = []
|
13
|
+
@properties = {}
|
14
|
+
|
15
|
+
extract_data(:translation, 0, 0, 0) # dt:t
|
16
|
+
extract_data(:alternatives, 5, 0, 2) # dt:at
|
17
|
+
extract_data(:dictionary, 1) # dt: bd
|
18
|
+
extract_data(:synonyms, 11) # dt:ss
|
19
|
+
extract_data(:definitions, 12, 0) # dt:md
|
20
|
+
extract_data(:examples, 13, 0) # dt:ex
|
21
|
+
extract_data(:see_also, 14, 0) # dt:rw
|
22
|
+
|
23
|
+
@alternatives = @alternatives.collect { |i| i[0] } if @alternatives
|
24
|
+
@keys.each { |key| @properties[key] = instance_variable_get("@#{key}") }
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_h
|
28
|
+
@properties
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def extract_data(name, *indices)
|
34
|
+
value = array_value(@raw, *indices)
|
35
|
+
return if value.nil?
|
36
|
+
instance_variable_set("@#{name}", value)
|
37
|
+
@keys.push(name)
|
38
|
+
end
|
39
|
+
|
40
|
+
def array_value(array, *indices)
|
41
|
+
return nil if array.nil?
|
42
|
+
index = indices.shift
|
43
|
+
value = array[index]
|
44
|
+
return value if indices.empty?
|
45
|
+
array_value(value, *indices)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module GoogleWebTranslate
|
2
|
+
# String escaping/unescaping code from syck/encoding.rb
|
3
|
+
module StringEscaping
|
4
|
+
ESCAPES = %w[\x00 \x01 \x02 \x03 \x04 \x05 \x06 \a
|
5
|
+
\x08 \t \n \v \f
|
6
|
+
\r \x0e \x0f
|
7
|
+
\x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17
|
8
|
+
\x18 \x19 \x1a \e \x1c \x1d \x1e \x1f].freeze
|
9
|
+
UNESCAPES = {
|
10
|
+
'a' => "\x07", 'b' => "\x08", 't' => "\x09",
|
11
|
+
'n' => "\x0a", 'v' => "\x0b", 'f' => "\x0c",
|
12
|
+
'r' => "\x0d", 'e' => "\x1b", '\\' => '\\'
|
13
|
+
}.freeze
|
14
|
+
|
15
|
+
class << self
|
16
|
+
# Escape unprintable characters such as newlines.
|
17
|
+
# @param value [String] The string to escape
|
18
|
+
# @param skip [String] Characters to not escape
|
19
|
+
# @return [String] The string with special characters escaped.
|
20
|
+
def escape(value, skip = '')
|
21
|
+
value.gsub(/\\/, '\\\\\\')
|
22
|
+
.gsub(/"/, '\\"')
|
23
|
+
.gsub(/([\x00-\x1f])/) do
|
24
|
+
skip[$&] || ESCAPES[ $&.unpack('C')[0] ]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Unescape character escapes such as "\n" to their character equivalents.
|
29
|
+
# @param value [String] The string to unescape
|
30
|
+
# @return [String] The string with special characters unescaped.
|
31
|
+
def unescape(value)
|
32
|
+
regex = /\\(?:([nevfbart\\])|0?x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/
|
33
|
+
value.gsub(regex) do
|
34
|
+
if Regexp.last_match(3)
|
35
|
+
[Regexp.last_match(3).to_s.hex].pack('U*')
|
36
|
+
elsif Regexp.last_match(2)
|
37
|
+
[Regexp.last_match(2)].pack('H2')
|
38
|
+
else
|
39
|
+
UNESCAPES[Regexp.last_match(1)]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def unquote(value)
|
45
|
+
if value && value[0] == value[-1] && %w[' "].include?(value[0])
|
46
|
+
value[1...-1]
|
47
|
+
else
|
48
|
+
value
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/js/window.js
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
var window = {
|
2
|
+
jstiming: {
|
3
|
+
load: {
|
4
|
+
tick: function() {}
|
5
|
+
}
|
6
|
+
}
|
7
|
+
};
|
8
|
+
var navigator = {};
|
9
|
+
var document = {};
|
10
|
+
|
11
|
+
function setWindowProperty(key, value) {
|
12
|
+
window[key] = value;
|
13
|
+
}
|
14
|
+
function generateToken(string, tkk) {
|
15
|
+
setWindowProperty("TKK", tkk);
|
16
|
+
return wq(string);
|
17
|
+
}
|
metadata
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_web_translate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
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: webmock
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: execjs
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rest-client
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: thor
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description:
|
126
|
+
email:
|
127
|
+
- sobakasu@gmail.com
|
128
|
+
executables:
|
129
|
+
- google_web_translate
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec"
|
135
|
+
- ".travis.yml"
|
136
|
+
- CODE_OF_CONDUCT.md
|
137
|
+
- Gemfile
|
138
|
+
- LICENSE.txt
|
139
|
+
- README.md
|
140
|
+
- Rakefile
|
141
|
+
- bin/console
|
142
|
+
- bin/google_web_translate
|
143
|
+
- bin/setup
|
144
|
+
- google-web-translate.gemspec
|
145
|
+
- lib/google_web_translate.rb
|
146
|
+
- lib/google_web_translate/api.rb
|
147
|
+
- lib/google_web_translate/cli.rb
|
148
|
+
- lib/google_web_translate/result.rb
|
149
|
+
- lib/google_web_translate/string_escaping.rb
|
150
|
+
- lib/google_web_translate/version.rb
|
151
|
+
- lib/js/window.js
|
152
|
+
homepage: https://github.com/sobakasu/google_web_translate
|
153
|
+
licenses:
|
154
|
+
- MIT
|
155
|
+
metadata: {}
|
156
|
+
post_install_message:
|
157
|
+
rdoc_options: []
|
158
|
+
require_paths:
|
159
|
+
- lib
|
160
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
161
|
+
requirements:
|
162
|
+
- - ">="
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: '0'
|
165
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - ">="
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: '0'
|
170
|
+
requirements: []
|
171
|
+
rubyforge_project:
|
172
|
+
rubygems_version: 2.6.14
|
173
|
+
signing_key:
|
174
|
+
specification_version: 4
|
175
|
+
summary: Text translation using the google web interface
|
176
|
+
test_files: []
|