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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 03da012e4736546f272bfc815d2c0ab42f5834dd
4
- data.tar.gz: eb1f139218f730cb41f6b2bce15d13c2655e0c47
3
+ metadata.gz: 3680dbb44b5cee62158114bcf90c80a8ade54d44
4
+ data.tar.gz: bbe3e3a1a2b3ad7b7217e45d3b365d3908b9a50d
5
5
  SHA512:
6
- metadata.gz: 5da92ba28b64fa0357609da5d096d83f19a6cd8836b6733bf236c48cdadfd83c3c97d33aec032b492e9fab5733f669a0ab121b374fa9d859c0cd85d7ee1d0959
7
- data.tar.gz: ec56a89a3e9f032f0cd09ff71a106d81f1906c82d0ffc1d290dc0e8056053d050fceb2ea2994535c23912c9fcbad8ae8b96f2af40231aae9c79549be407b76ba
6
+ metadata.gz: 6a7522104160c70136639feca9d136c6cbdea3aa63ddad28fabedee4311fba37c33ccb7e372cb9d621819d9ad97229d94042d52f74f6b3b01d88bf5039bd5a34
7
+ data.tar.gz: 624929e643c2218b9d10e41f28c8bb4376aea036c858b97289be446f115f1b3fa04797c8743c68c08d7159e59031f60f207ec5b0963be78d29463cc99aea010f
data/.travis.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  rvm:
2
- - 1.9.2
3
2
  - 2.0.0
4
3
  - 2.1.1
5
4
  - 2.2.0
5
+ - 2.3.1
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
- Termit is an easy way to use Google Translate in your terminal. You can check out its node.js npm version [normit](https://github.com/pawurb/normit)
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
- * english - en
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 display_synonyms synonyms
51
- print '=> Synonyms: '
52
- puts synonyms
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 display_no_internet_msg
56
- puts "TERMIT: There seems to be a problem with your internet connection"
54
+ def display_error_msg
55
+ puts "TERMIT: We are sorry but something went wrong."
57
56
  exit
58
57
  end
59
58
  end
@@ -0,0 +1,14 @@
1
+ #encoding: UTF-8
2
+
3
+ module Termit
4
+ class SoundFetcher < DataFetcherBase
5
+ private
6
+
7
+ def send_request
8
+ RestClient.get(
9
+ @url,
10
+ { cookies: self.class.auth_cookies }
11
+ )
12
+ end
13
+ end
14
+ 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.mpeg", "wb") do |file|
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.mpeg"
16
+ system "mpg123 -q #{location}/sound_response.mp3"
17
17
  end
18
18
 
19
19
  private
@@ -11,7 +11,7 @@ module Termit
11
11
  end
12
12
 
13
13
  def call
14
- response = Termit::DataFetcher.new(@url, @text).data
14
+ response = Termit::SoundFetcher.new(@url, @text).data
15
15
  Termit::SoundResponseHandler.new(response.body).call
16
16
  end
17
17
 
@@ -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 :display_synonyms, :display_translation, to: :output_manager
7
+ delegate :display_translation, to: :output_manager
6
8
 
7
- def initialize text, synonyms_wanted
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.split("[[")[1].split("\"")[1]
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::DataFetcher.new(@url, @options[:text]).data
13
- @text = Termit::TextResponseHandler.new(response.body, @options[:synonyms]).call
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://translate.google.com"
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}/translate_a/single?client=t&sl=#{@options[:source_lang]}&tl=#{@options[:target_lang]}&hl=pl&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&otf=2&srcrom=1&ssel=3&tsel=6&kc=2&tk=522578"
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}/translate_tts?ie=UTF-8&tl=#{@options[:target_lang]}&total=1&idx=0&textlen=5&tk=735012&client=t"
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 [2,3,4,5].include? language_code.length
40
+ raise ArgumentError unless (2..7).cover? language_code.length
42
41
  end
43
42
  end
44
43
 
@@ -1,3 +1,3 @@
1
1
  module Termit
2
- VERSION = "2.13.0"
2
+ VERSION = "3.0.0"
3
3
  end
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/data_fetcher'
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