baidu-translate-api 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 +9 -0
- data/Gemfile +6 -0
- data/README.md +38 -0
- data/Rakefile +2 -0
- data/baidu-translate-api.gemspec +27 -0
- data/lib/baidu-translate-api.rb +55 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 20b2fd0939690c0b3c732bc0370ca2df64ec31876d47701f4a50afe1b1330209
|
4
|
+
data.tar.gz: b52751ba9abdd82499acdb181514c3733cb70444dcad08b5d47dadf08b5ac14b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 12750c0b3cb067c4fe510bab5908a19a44e20d63fdbe263866d981399e946510423774555ab50aa380d25a9332e1e2d88c0b49c253d5c8fff43e632c235369c0
|
7
|
+
data.tar.gz: b5b90f1015b68d10a7ff7c9933600e149b4e888e8569c5cbe1e24cbb28027f375526efde1c610bf45b5bd12c700cacc2e4ca8a1f84eb12f5903bd93a5cab9b7b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Baidu translate api client for Ruby
|
2
|
+
|
3
|
+
- https://api.fanyi.baidu.com/api/trans/product/apidoc
|
4
|
+
- http://api.fanyi.baidu.com/api/trans/product/prodinfo
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'baidu-translate-api'
|
12
|
+
```
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
```
|
17
|
+
require 'baidu-translate-api'
|
18
|
+
|
19
|
+
appid = '2018nnnnnn'
|
20
|
+
secret_key = 'foobarbaz'
|
21
|
+
|
22
|
+
bta = BaiduTranslateApi.new(appid: appid, secret_key: secret_key)
|
23
|
+
puts bta.translate("hello\nWhere are you from?")
|
24
|
+
puts '---'
|
25
|
+
puts bta.translate("こんにちは\nあなたはどこから来ましたか?")
|
26
|
+
puts '---'
|
27
|
+
puts bta.translate("早上好", from: :zh, to: :jp)
|
28
|
+
```
|
29
|
+
|
30
|
+
```
|
31
|
+
你好
|
32
|
+
你从哪里来的?
|
33
|
+
---
|
34
|
+
你好
|
35
|
+
你从哪里来的?
|
36
|
+
---
|
37
|
+
おはよう
|
38
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "baidu-translate-api"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "baidu-translate-api"
|
8
|
+
spec.version = BaiduTranslateApi::VERSION
|
9
|
+
spec.authors = ["Yuichi Tateno"]
|
10
|
+
spec.email = ["hotchpotch@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Baidu translate api client for Ruby.}
|
13
|
+
spec.description = %q{Baidu translate api client for Ruby.}
|
14
|
+
spec.homepage = "https://github.com/hotchpotch/baidu-translate-api-ruby"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
|
2
|
+
require 'digest'
|
3
|
+
require 'json'
|
4
|
+
require 'open-uri'
|
5
|
+
require 'uri'
|
6
|
+
|
7
|
+
class BaiduTranslateApi
|
8
|
+
VERSION = '0.1.0'
|
9
|
+
|
10
|
+
SALT_MAXNUM = 10000000
|
11
|
+
HTTP_ENDPOINT = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
|
12
|
+
HTTPS_ENDPOINT = 'https://fanyi-api.baidu.com/api/trans/vip/translate'
|
13
|
+
|
14
|
+
attr_accessor :appid, :secret_key, :default_from, :default_to, :use_https
|
15
|
+
def initialize(appid:, secret_key:, default_from: :auto, default_to: :zh, use_https: true)
|
16
|
+
@appid = appid
|
17
|
+
@secret_key = secret_key
|
18
|
+
@default_from = default_from
|
19
|
+
@default_to = default_to
|
20
|
+
@use_https = use_https
|
21
|
+
end
|
22
|
+
|
23
|
+
def request(word, from: self.default_from, to: self.default_to)
|
24
|
+
if word.bytesize > 6000
|
25
|
+
raise AugumentsError.new("word bytesize over 6000, https://api.fanyi.baidu.com/api/trans/product/apidoc")
|
26
|
+
end
|
27
|
+
salt = rand(SALT_MAXNUM).to_s
|
28
|
+
q = word
|
29
|
+
sign = Digest::MD5.hexdigest "#{self.appid}#{q}#{salt}#{self.secret_key}"
|
30
|
+
params = {
|
31
|
+
salt: salt,
|
32
|
+
q: q,
|
33
|
+
to: to,
|
34
|
+
from: from,
|
35
|
+
appid: self.appid,
|
36
|
+
sign: sign,
|
37
|
+
}
|
38
|
+
open(endpoint + '?' + URI.encode_www_form(params))
|
39
|
+
end
|
40
|
+
|
41
|
+
def translate(word, from: self.default_from, to: self.default_to)
|
42
|
+
res = request(word, from: from, to: to)
|
43
|
+
data = JSON.parse(res.read)
|
44
|
+
data['trans_result'].map {|d| d['dst'] }.join("\n")
|
45
|
+
end
|
46
|
+
|
47
|
+
def endpoint
|
48
|
+
if self.use_https
|
49
|
+
HTTPS_ENDPOINT
|
50
|
+
else
|
51
|
+
HTTP_ENDPOINT
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: baidu-translate-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuichi Tateno
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-08 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
|
+
description: Baidu translate api client for Ruby.
|
42
|
+
email:
|
43
|
+
- hotchpotch@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- baidu-translate-api.gemspec
|
53
|
+
- lib/baidu-translate-api.rb
|
54
|
+
homepage: https://github.com/hotchpotch/baidu-translate-api-ruby
|
55
|
+
licenses: []
|
56
|
+
metadata: {}
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 2.7.6
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Baidu translate api client for Ruby.
|
77
|
+
test_files: []
|