translator-srt 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +28 -0
- data/lib/translator_srt.rb +4 -0
- data/lib/translator_srt/google_translate.rb +22 -0
- data/lib/translator_srt/google_translate/result_parser.rb +51 -0
- data/lib/translator_srt/google_translate/service.rb +50 -0
- data/lib/translator_srt/version.rb +3 -0
- data/translator_srt.gemspec +19 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6ee07e78b451695d2e7d03c9da80126f734ea885
|
4
|
+
data.tar.gz: bc16d2a73ece72f2c9873abbc3f98f9f73e75fc6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 945bb15f2df8f0d60bc25a1df4e3c613a13c01b1695507ddefc83a10c904f1e4b882b58d7342fe4ec73ada4d749c696ac24aa707ca9998d127024f4312509c92
|
7
|
+
data.tar.gz: 74661f4c4ab8dc2557bd929214119f0125412d0e96d89a8cbdeffa7db33bce58219d2c1bec476182a35cb2fa8716d22236bcebf210bccc2297de8a21800c2318
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
Copyright (c) 2014, Vadim Poplavskiy
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
* Neither the name of translator-srt nor the names of its
|
15
|
+
contributors may be used to endorse or promote products derived from
|
16
|
+
this software without specific prior written permission.
|
17
|
+
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
19
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
20
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
22
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
23
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
24
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
25
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
26
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
27
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'translator_srt/google_translate/result_parser'
|
2
|
+
require 'translator_srt/google_translate/service'
|
3
|
+
|
4
|
+
module TranslatorSrt
|
5
|
+
module GoogleTranslate
|
6
|
+
def self.translate_srt_file(from_lang, to_lang, path_srt_file)
|
7
|
+
srt_file_content = File.read(path_srt_file)
|
8
|
+
result = Service.new.translate(from_lang, to_lang, srt_file_content)
|
9
|
+
|
10
|
+
puts "Translated srt string"
|
11
|
+
|
12
|
+
ResultParser.new(result).to_s
|
13
|
+
end
|
14
|
+
def self.translate_srt_string(from_lang, to_lang, text)
|
15
|
+
result = Service.new.translate(from_lang, to_lang, text)
|
16
|
+
|
17
|
+
puts "Translated srt string"
|
18
|
+
|
19
|
+
ResultParser.new(result).to_s
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module TranslatorSrt
|
2
|
+
module GoogleTranslate
|
3
|
+
class ResultParser
|
4
|
+
def initialize(result)
|
5
|
+
@result = result
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_s
|
9
|
+
normalize_translation
|
10
|
+
|
11
|
+
@result.join
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def normalize_translation
|
17
|
+
to_one_ar
|
18
|
+
|
19
|
+
list_keys = {
|
20
|
+
"-->" => ["###"],
|
21
|
+
"," => [" ## ", "##"],
|
22
|
+
":" => [" # ", "#"],
|
23
|
+
}
|
24
|
+
|
25
|
+
list_keys.each do |key_group, group|
|
26
|
+
group.each do |key|
|
27
|
+
@result.each_index do |index_translation|
|
28
|
+
@result[index_translation] = @result[index_translation].gsub key, key_group
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_one_ar
|
36
|
+
permit_translation
|
37
|
+
|
38
|
+
result_in_one_array = []
|
39
|
+
@result.each do |item|
|
40
|
+
result_in_one_array.append item.first
|
41
|
+
end
|
42
|
+
|
43
|
+
@result = result_in_one_array
|
44
|
+
end
|
45
|
+
|
46
|
+
def permit_translation
|
47
|
+
@result = @result.first
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
require 'tempfile'
|
4
|
+
require 'resource_accessor'
|
5
|
+
|
6
|
+
module TranslatorSrt
|
7
|
+
module GoogleTranslate
|
8
|
+
class Service
|
9
|
+
GOOGLE_TRANSLATE_SERVICE_URL = "translate.google.com"
|
10
|
+
|
11
|
+
def translate(from_lang, to_lang, text)
|
12
|
+
raise("Missing 'from' language") unless from_lang
|
13
|
+
raise("Missing 'to' language") unless to_lang
|
14
|
+
raise("Missing text for translation") unless text
|
15
|
+
|
16
|
+
r = call_translate_service(from_lang, to_lang, text)
|
17
|
+
|
18
|
+
result = JSON.parse(r.gsub('[,', '['))
|
19
|
+
|
20
|
+
raise("Translate Server is down") if (!result || result.empty?)
|
21
|
+
|
22
|
+
result
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def translate_url(from_lang, to_lang)
|
28
|
+
"https://#{GOOGLE_TRANSLATE_SERVICE_URL}/translate_a/single?client=t&sl=#{from_lang}&tl=#{to_lang}&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t&dt=at&dt=sw&ie=UTF-8&oe=UTF-8&prev=btn&rom=1&ssel=0&tsel=0"
|
29
|
+
end
|
30
|
+
|
31
|
+
def call_translate_service from_lang, to_lang, text
|
32
|
+
url = translate_url(from_lang, to_lang)
|
33
|
+
|
34
|
+
response = call_service(url, normalize_to_translate(text))
|
35
|
+
|
36
|
+
response.body.split(',').collect { |s| s == '' ? "\"\"" : s }.join(",") # fix json object
|
37
|
+
end
|
38
|
+
|
39
|
+
def call_service url, q
|
40
|
+
accessor = ResourceAccessor.new
|
41
|
+
|
42
|
+
accessor.get_response url: url, :method => :post, :body => {q: q}
|
43
|
+
end
|
44
|
+
|
45
|
+
def normalize_to_translate text
|
46
|
+
text.gsub(":", "#").gsub(",", "##").gsub("-->", "###")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'translator_srt/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'translator-srt'
|
7
|
+
spec.version = TranslatorSrt::VERSION
|
8
|
+
spec.authors = ['Vadim Poplavskiy']
|
9
|
+
spec.email = ['im@demetrodon.com']
|
10
|
+
spec.summary = %q{Translator srt files}
|
11
|
+
spec.description = ''
|
12
|
+
spec.homepage = 'https://github.com/demetrodon/translator-srt'
|
13
|
+
spec.license = 'New BSD'
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.require_paths = ['lib']
|
17
|
+
|
18
|
+
spec.add_dependency 'resource_accessor'
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: translator-srt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vadim Poplavskiy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: resource_accessor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: ''
|
28
|
+
email:
|
29
|
+
- im@demetrodon.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- Gemfile
|
36
|
+
- LICENSE
|
37
|
+
- lib/translator_srt.rb
|
38
|
+
- lib/translator_srt/google_translate.rb
|
39
|
+
- lib/translator_srt/google_translate/result_parser.rb
|
40
|
+
- lib/translator_srt/google_translate/service.rb
|
41
|
+
- lib/translator_srt/version.rb
|
42
|
+
- translator_srt.gemspec
|
43
|
+
homepage: https://github.com/demetrodon/translator-srt
|
44
|
+
licenses:
|
45
|
+
- New BSD
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 2.2.2
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: Translator srt files
|
67
|
+
test_files: []
|