termit 2.13.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +7 -22
- data/lib/termit/data_fetcher_base.rb +28 -0
- data/lib/termit/output_manager.rb +5 -6
- data/lib/termit/sound_fetcher.rb +14 -0
- data/lib/termit/sound_response_handler.rb +2 -2
- data/lib/termit/speech_synthesizer.rb +1 -1
- data/lib/termit/text_fetcher.rb +21 -0
- data/lib/termit/text_response_handler.rb +5 -19
- data/lib/termit/text_translator.rb +2 -2
- data/lib/termit/url_constructor.rb +12 -3
- data/lib/termit/user_input_parser.rb +1 -2
- data/lib/termit/version.rb +1 -1
- data/lib/termit.rb +3 -1
- data/spec/fixtures/bing_1.yml +827 -0
- data/spec/fixtures/bing_2.yml +826 -0
- data/spec/fixtures/text_response +11 -0
- data/spec/spec_helper.rb +6 -2
- data/spec/termit/sound_response_handler_spec.rb +2 -2
- data/spec/termit/speech_synthesizer_spec.rb +3 -3
- data/spec/termit/text_response_handler_spec.rb +6 -34
- data/spec/termit/text_translator_spec.rb +5 -5
- data/spec/termit/url_constructor_spec.rb +2 -2
- data/spec/termit/user_input_parser_spec.rb +3 -8
- data/termit.gemspec +1 -0
- metadata +26 -12
- data/lib/termit/data_fetcher.rb +0 -35
- data/spec/fixtures/google_1.yml +0 -66
- data/spec/fixtures/google_2.yml +0 -190
- data/spec/fixtures/text_response_with_synonyms +0 -1
- data/spec/fixtures/text_response_without_synonyms +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3680dbb44b5cee62158114bcf90c80a8ade54d44
|
4
|
+
data.tar.gz: bbe3e3a1a2b3ad7b7217e45d3b365d3908b9a50d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a7522104160c70136639feca9d136c6cbdea3aa63ddad28fabedee4311fba37c33ccb7e372cb9d621819d9ad97229d94042d52f74f6b3b01d88bf5039bd5a34
|
7
|
+
data.tar.gz: 624929e643c2218b9d10e41f28c8bb4376aea036c858b97289be446f115f1b3fa04797c8743c68c08d7159e59031f60f207ec5b0963be78d29463cc99aea010f
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
#Termit [![Build Status](https://travis-ci.org/pawurb/termit.png)](https://travis-ci.org/pawurb/termit) [![Gem Version](https://badge.fury.io/rb/termit.png)](http://badge.fury.io/rb/termit) [![Coverage Status](https://coveralls.io/repos/pawurb/termit/badge.png)](https://coveralls.io/r/pawurb/termit)
|
2
2
|
|
3
|
+
Termit is an easy way to translate stuff in your terminal. You can check out its node.js npm version [normit](https://github.com/pawurb/normit)
|
3
4
|
|
4
|
-
|
5
|
+
## Status
|
6
|
+
|
7
|
+
I rewrote it to work with [Bing Translator](https://www.bing.com/translator) . Thanks to [Ragnarson](https://ragnarson.com) for supporting it !
|
5
8
|
|
6
9
|
## Installation
|
7
10
|
```ruby
|
@@ -43,15 +46,6 @@ termit en en "hold your horses cowboy !" -t
|
|
43
46
|
=> "hold your horses cowboy !" # and an english voice asks you to hold on
|
44
47
|
```
|
45
48
|
|
46
|
-
#### Synonyms
|
47
|
-
|
48
|
-
Specify a **-s** (synonyms) flag to get the list of synonyms if available:
|
49
|
-
``` ruby
|
50
|
-
termit es en muchacho -s
|
51
|
-
=> boy
|
52
|
-
=> Synonyms: boy, lad, youngster, laddie, cully
|
53
|
-
```
|
54
|
-
|
55
49
|
#### Learning language when committing to git
|
56
50
|
|
57
51
|
Idea by [Nedomas](https://news.ycombinator.com/item?id=7545747). See and hear your messages translated to target lang every time you commit. You can do this two ways: overriding the `git` command, and using a post-commit hook in git.
|
@@ -82,16 +76,8 @@ If you want this to be in every one of your git repositories, see [this Stack Ov
|
|
82
76
|
|
83
77
|
## Language codes:
|
84
78
|
|
85
|
-
|
86
|
-
* polish - pl
|
87
|
-
* french - fr
|
88
|
-
* spanish - es
|
89
|
-
* slovakian - sk
|
90
|
-
* chinese - zh
|
91
|
-
* russian - ru
|
92
|
-
* automatic source language detection - auto
|
79
|
+
To find all available language codes visit https://msdn.microsoft.com/en-us/library/hh456380.aspx
|
93
80
|
|
94
|
-
To find all available language codes visit https://developers.google.com/translate/v2/using_rest#language-params.
|
95
81
|
|
96
82
|
## Requirements
|
97
83
|
|
@@ -107,7 +93,6 @@ For MacOSX:
|
|
107
93
|
|
108
94
|
brew install mpg123
|
109
95
|
|
96
|
+
## Disclaimer
|
110
97
|
|
111
|
-
|
112
|
-
|
113
|
-
|
98
|
+
Termit works by scraping the private APIs and is therefore not recommended for use in production or on a large scale.
|
@@ -0,0 +1,28 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
require 'rest-client'
|
3
|
+
|
4
|
+
module Termit
|
5
|
+
class DataFetcherBase
|
6
|
+
include CanOutput
|
7
|
+
delegate :display_no_internet_msg, :display_invalid_data_msg, to: :output_manager
|
8
|
+
|
9
|
+
def initialize url, text
|
10
|
+
@url = url
|
11
|
+
@text = text
|
12
|
+
end
|
13
|
+
|
14
|
+
def data
|
15
|
+
send_request
|
16
|
+
rescue RestClient::BadRequest
|
17
|
+
display_invalid_data_msg
|
18
|
+
rescue RestClient::Exception
|
19
|
+
display_error_msg
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def self.auth_cookies
|
25
|
+
@@_auth_cookies ||= RestClient.get('https://www.bing.com/translator').cookies
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -29,7 +29,6 @@ termit en fr 'hey cowboy where is your horse?'
|
|
29
29
|
|
30
30
|
Options:
|
31
31
|
-t - speech synthesis
|
32
|
-
-s - synonyms list
|
33
32
|
|
34
33
|
Check docs at: github.com/pawurb/termit
|
35
34
|
EOS
|
@@ -47,13 +46,13 @@ EOS
|
|
47
46
|
puts text
|
48
47
|
end
|
49
48
|
|
50
|
-
def
|
51
|
-
|
52
|
-
|
49
|
+
def display_invalid_data_msg
|
50
|
+
puts "TERMIT: Looks like you provided invalid options, or speech synthesis is not supported for this language."
|
51
|
+
exit
|
53
52
|
end
|
54
53
|
|
55
|
-
def
|
56
|
-
puts "TERMIT:
|
54
|
+
def display_error_msg
|
55
|
+
puts "TERMIT: We are sorry but something went wrong."
|
57
56
|
exit
|
58
57
|
end
|
59
58
|
end
|
@@ -10,10 +10,10 @@ module Termit
|
|
10
10
|
def call
|
11
11
|
location = "#{File.expand_path('~')}/.termit"
|
12
12
|
create_target_dir location
|
13
|
-
File.open("#{location}/sound_response.
|
13
|
+
File.open("#{location}/sound_response.mp3", "wb") do |file|
|
14
14
|
file.write(@data)
|
15
15
|
end
|
16
|
-
system "mpg123 -q #{location}/sound_response.
|
16
|
+
system "mpg123 -q #{location}/sound_response.mp3"
|
17
17
|
end
|
18
18
|
|
19
19
|
private
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
|
3
|
+
module Termit
|
4
|
+
class TextFetcher < DataFetcherBase
|
5
|
+
private
|
6
|
+
|
7
|
+
def send_request
|
8
|
+
RestClient.post(@url,
|
9
|
+
text_payload,
|
10
|
+
{
|
11
|
+
:cookies => self.class.auth_cookies,
|
12
|
+
'Content-Type' => 'application/json'
|
13
|
+
}
|
14
|
+
)
|
15
|
+
end
|
16
|
+
|
17
|
+
def text_payload
|
18
|
+
"[{\"id\":#{rand(20168810)},\"text\":\"#{@text}\"}]"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,35 +1,25 @@
|
|
1
1
|
#encoding: UTF-8
|
2
|
+
require 'json'
|
3
|
+
|
2
4
|
module Termit
|
3
5
|
class TextResponseHandler
|
4
6
|
include CanOutput
|
5
|
-
delegate :
|
7
|
+
delegate :display_translation, to: :output_manager
|
6
8
|
|
7
|
-
def initialize text
|
9
|
+
def initialize text
|
8
10
|
@text = decode text
|
9
|
-
@synonyms_wanted = synonyms_wanted
|
10
11
|
end
|
11
12
|
|
12
13
|
def call
|
13
14
|
translation = extract_translation
|
14
15
|
display_translation translation
|
15
|
-
display_synonyms extract_synonyms if @synonyms_wanted
|
16
16
|
translation
|
17
17
|
end
|
18
18
|
|
19
19
|
private
|
20
20
|
|
21
21
|
def extract_translation
|
22
|
-
@text
|
23
|
-
end
|
24
|
-
|
25
|
-
def extract_synonyms
|
26
|
-
synonyms_data = @text.split("[[")[2].split("[")[1]
|
27
|
-
length = synonyms_data && synonyms_data.length
|
28
|
-
if length && synonyms_available(synonyms_data)
|
29
|
-
synonyms_data[0..(length-3)].delete("\"").gsub(/(,)/, ", ")
|
30
|
-
else
|
31
|
-
" ---"
|
32
|
-
end
|
22
|
+
JSON.parse(@text)["items"][0]["text"]
|
33
23
|
end
|
34
24
|
|
35
25
|
def decode text
|
@@ -37,9 +27,5 @@ module Termit
|
|
37
27
|
text.gsub!(/(\\x26#39;)/, "'")
|
38
28
|
text.force_encoding(encoding).encode(encoding)
|
39
29
|
end
|
40
|
-
|
41
|
-
def synonyms_available synonyms_data
|
42
|
-
!synonyms_data.include?('true,false')
|
43
|
-
end
|
44
30
|
end
|
45
31
|
end
|
@@ -9,8 +9,8 @@ module Termit
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def call
|
12
|
-
response = Termit::
|
13
|
-
@text = Termit::TextResponseHandler.new(response.body
|
12
|
+
response = Termit::TextFetcher.new(@url, @options[:text]).data
|
13
|
+
@text = Termit::TextResponseHandler.new(response.body).call
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
#encoding: UTF-8
|
2
|
+
|
2
3
|
module Termit
|
3
4
|
class UrlConstructor
|
4
|
-
Host = "https://
|
5
|
+
Host = "https://www.bing.com"
|
5
6
|
|
6
7
|
def initialize options
|
7
8
|
@options = options
|
@@ -14,11 +15,19 @@ module Termit
|
|
14
15
|
private
|
15
16
|
|
16
17
|
def construct_text_url
|
17
|
-
"#{Host}/
|
18
|
+
"#{Host}/translator/api/Translate/TranslateArray?from=#{@options.fetch(:source_lang)}&to=#{@options.fetch(:target_lang)}"
|
18
19
|
end
|
19
20
|
|
20
21
|
def construct_sound_url
|
21
|
-
"#{Host}/
|
22
|
+
"#{Host}/translator/api/language/Speak?locale=#{transform_country_code(@options.fetch(:target_lang))}&gender=male&media=audio/mp3&text=#{URI.encode(@options.fetch(:text))}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def transform_country_code(code)
|
26
|
+
if code == :en
|
27
|
+
'en-US'
|
28
|
+
else
|
29
|
+
"#{code}-#{code.upcase}"
|
30
|
+
end
|
22
31
|
end
|
23
32
|
end
|
24
33
|
end
|
@@ -22,7 +22,6 @@ module Termit
|
|
22
22
|
def parse_input
|
23
23
|
{
|
24
24
|
talk: extract_flag('t'),
|
25
|
-
synonyms: extract_flag('s'),
|
26
25
|
source_lang: @user_input.shift.to_sym,
|
27
26
|
target_lang: @user_input.shift.to_sym,
|
28
27
|
text: @user_input.join(' ').gsub('.', ',')
|
@@ -38,7 +37,7 @@ module Termit
|
|
38
37
|
raise ArgumentError unless @user_input.is_a? Array
|
39
38
|
raise ArgumentError unless @user_input.length > 1
|
40
39
|
@user_input.first(2).each do |language_code|
|
41
|
-
raise ArgumentError unless
|
40
|
+
raise ArgumentError unless (2..7).cover? language_code.length
|
42
41
|
end
|
43
42
|
end
|
44
43
|
|
data/lib/termit/version.rb
CHANGED
data/lib/termit.rb
CHANGED
@@ -7,7 +7,9 @@ require 'termit/sound_response_handler'
|
|
7
7
|
require 'termit/text_translator'
|
8
8
|
require 'termit/speech_synthesizer'
|
9
9
|
require 'termit/url_constructor'
|
10
|
-
require 'termit/
|
10
|
+
require 'termit/data_fetcher_base'
|
11
|
+
require 'termit/text_fetcher'
|
12
|
+
require 'termit/sound_fetcher'
|
11
13
|
require 'termit/text_response_handler'
|
12
14
|
require 'termit/version'
|
13
15
|
|