termit 1.2.2 → 2.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: d3877c35ca03dbfaef3fff20bfadaa6ffbefdc5d
4
- data.tar.gz: 4137b5b15540835bc0d2af0f14dbe3489fa1a1ab
3
+ metadata.gz: 1cf7b6172a5154290b498284dd9905edf9a39a7f
4
+ data.tar.gz: 424b0e353b635f690344f4018d7a08a3a67f5c25
5
5
  SHA512:
6
- metadata.gz: 7ee8e8ee1f6b27ef00e4d1233bf4d5b5157083e23484faf2afc13c8a3101e31b901b54ddcdf2787c77cdf3163ce11a088b561a096690763b1bdea5706f7920c0
7
- data.tar.gz: e026e87b742764dd13376a431b0496883ab8d4b2b09d8b50681b42d057711b144c2733ffce89323e951bdfc61b34dcbb4a89f85e14491ef86faf8e6b7bf62dd0
6
+ metadata.gz: dc71554597190cebb2e2d3dc453cfd9fe5fb42fcf0bea4ba1854bb8fc44a99aacbec8f1a468a7cf9af0b0b34c2cf0f31ddcf174e12812e4f25fe664733e671c5
7
+ data.tar.gz: 739ed90976ed42c59773d754f43b48e9a8c635878cce6d60961cc7779f216eb19e0a7c68d341c5332e3eada1792baff9ad64dbdb54a7f2316f0bd4022bc5d8b8
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  Gemfile.lock
2
2
  .ruby-version
3
3
  pkg/
4
+ *.gem
data/README.md CHANGED
@@ -3,19 +3,10 @@
3
3
 
4
4
  Termit is an easy way to use Google Translate in your terminal. It does not use paid Google Translate API but instead it headlessly browses www.translate.google.com and parses the response.
5
5
 
6
- ## Requirements
7
-
8
- Works with Ruby 1.9.2 and higher.
9
-
10
- To use speech synthesis (yes it talks) you need to have mpg123 installed.
11
-
12
- For Ubuntu:
13
-
14
- sudo apt-get install mpg123
15
-
16
- For MacOSX:
17
-
18
- brew install mpg123
6
+ ## Installation
7
+ ```ruby
8
+ gem install termit
9
+ ```
19
10
 
20
11
  ## Usage
21
12
  ```ruby
@@ -25,7 +16,6 @@ termit 'source_language' 'target_language' 'text'
25
16
  Example:
26
17
 
27
18
  ```ruby
28
- gem install termit
29
19
 
30
20
  termit en es "hey cowboy where is your horse?"
31
21
  => "Hey vaquero dónde está tu caballo?"
@@ -41,12 +31,23 @@ termit fr ru qui est votre papa?
41
31
  ```
42
32
  #### Speech synthesis
43
33
 
44
- Specify a **-v** (voice) flag to use speech synthesis:
34
+ Specify a **-t** (talk) flag to use speech synthesis:
45
35
  ``` ruby
46
36
  termit en zh "hey cowboy where is your horse?" -v
47
- => "嘿,牛仔是你的马在哪里?" #and a nice chinese girl voice says something about a horse
37
+ => "嘿,牛仔是你的马在哪里?" #and a chinese voice says something about a horse
48
38
  ```
49
39
 
40
+ #### Synonyms
41
+
42
+ Specify a **-s** (synonyms) flag to get the list of synonyms if available:
43
+ ``` ruby
44
+ termit es en muchacho -s
45
+ => boy
46
+ => Synonyms: boy, lad, youngster, laddie, cully
47
+ ```
48
+
49
+
50
+
50
51
  ## Language codes:
51
52
  * english - en
52
53
  * polish - pl
@@ -59,15 +60,24 @@ termit en zh "hey cowboy where is your horse?" -v
59
60
 
60
61
  To find all available language codes visit www.translate.google.com. Choose langauges and their codes will appear in the url.
61
62
 
62
- ## Status
63
+ ## Requirements
64
+
65
+ Works with Ruby 1.9.2 and higher.
66
+
67
+ To use speech synthesis (yes it talks) you need to have mpg123 installed.
68
+
69
+ For Ubuntu:
63
70
 
64
- Project is in "but it works on my machine..." development phase. Any feedback will be appreciated. You can contact me on: p.urbanek89@gmail.com
71
+ sudo apt-get install mpg123
65
72
 
73
+ For MacOSX:
66
74
 
75
+ brew install mpg123
67
76
 
68
77
 
69
78
 
70
79
 
80
+ Any feedback will be appreciated. You can contact me on: p.urbanek89@gmail.com
71
81
 
72
82
 
73
83
 
data/bin/termit CHANGED
@@ -5,20 +5,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib') unless $LOAD_PATH.include
5
5
 
6
6
  require 'termit'
7
7
 
8
- if ARGV.empty?
9
- puts ""
10
- puts "=========Termit========="
11
- puts "USAGE:"
12
- puts "termit 'source_language' 'target_language' 'text'"
13
- puts ""
14
- puts 'Example:'
15
- puts "termit en fr 'hey cowboy where is your horse?'"
16
- puts "=> 'hey cow-boy ou est votre cheval?'"
17
- puts ""
18
- puts "Check docs at: github.com/pawurb/termit "
19
- puts ""
20
- else
21
- options = Termit::UserInputParser.new(ARGV).options
22
- Termit::Main.new(options).translate
23
- end
8
+ options = Termit::UserInputParser.new(ARGV).options
9
+ Termit::Main.new(options).translate
10
+
24
11
 
@@ -1,3 +1,5 @@
1
+ require 'termit/modules/delegation'
2
+ require 'termit/output_manager'
1
3
  require 'termit/user_input_parser'
2
4
  require 'termit/main'
3
5
  require 'termit/sound_response_handler'
@@ -8,4 +10,4 @@ require 'termit/data_fetcher'
8
10
  require 'termit/text_response_handler'
9
11
 
10
12
  module Termit
11
- end
13
+ end
@@ -1,3 +1,4 @@
1
+ #encoding: UTF-8
1
2
  require 'net/http'
2
3
  require 'uri'
3
4
 
@@ -1,3 +1,4 @@
1
+ #encoding: UTF-8
1
2
  module Termit
2
3
  class Main
3
4
  def initialize options
@@ -7,7 +8,7 @@ module Termit
7
8
  def translate
8
9
  text_translator = Termit::TextTranslator.new(@options)
9
10
  text_translator.call
10
- speech_synthesis text_translator.text if @options[:voice]
11
+ speech_synthesis text_translator.text if @options[:talk]
11
12
  end
12
13
 
13
14
  private
@@ -0,0 +1,11 @@
1
+ module Delegation
2
+ def delegate *methods, receiver
3
+ define_method :method_missing do |method, *args, &block|
4
+ if methods.include?(method)
5
+ receiver[:to].send(method, *args, &block)
6
+ else
7
+ raise NoMethodError
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,34 @@
1
+ #encoding: UTF-8
2
+ class Termit::OutputManager
3
+ def display_error_info
4
+ puts "TERMIT: Wrong data. Example: 'termit en es the cowboy ' => 'el vaquero"
5
+ end
6
+
7
+ def display_help
8
+ puts <<-EOS
9
+ =========TERMIT=========
10
+ USAGE:
11
+ termit 'source_language' 'target_language' 'text'
12
+
13
+ Example:
14
+ termit en fr 'hey cowboy where is your horse?'
15
+ => 'hey cow-boy ou est votre cheval?'
16
+
17
+ Check docs at: github.com/pawurb/termit
18
+ EOS
19
+ end
20
+
21
+ def display_version
22
+ puts "Termit #{Termit::VERSION}"
23
+ end
24
+
25
+ def display_translation text
26
+ print "=> "
27
+ puts text
28
+ end
29
+
30
+ def display_synonyms synonyms
31
+ print '=> Synonyms:'
32
+ puts synonyms
33
+ end
34
+ end
@@ -1,3 +1,4 @@
1
+ #encoding: UTF-8
1
2
  require 'fileutils'
2
3
 
3
4
  module Termit
@@ -7,7 +8,7 @@ module Termit
7
8
  end
8
9
 
9
10
  def call
10
- location = "#{File.expand_path('~')}/.Termit"
11
+ location = "#{File.expand_path('~')}/.termit"
11
12
  create_target_dir location
12
13
  File.open("#{location}/sound_response.mpeg", "wb") do |file|
13
14
  file.write(@data)
@@ -1,3 +1,4 @@
1
+ #encoding: UTF-8
1
2
  module Termit
2
3
  class SpeechSynthesizer
3
4
  def initialize options
@@ -1,20 +1,42 @@
1
+ #encoding: UTF-8
1
2
  module Termit
2
3
  class TextResponseHandler
3
- def initialize text
4
- @text = text
4
+ extend ::Delegation
5
+ @output_manager = Termit::OutputManager.new
6
+ delegate :display_synonyms, :display_translation, to: @output_manager
7
+
8
+ def initialize text, synonyms_wanted
9
+ @text = decode text
10
+ @synonyms_wanted = synonyms_wanted
5
11
  end
6
12
 
7
13
  def call
8
- p parsed_data
14
+ translation = extract_translation
15
+ display_translation translation
16
+ display_synonyms extract_synonyms if @synonyms_wanted
17
+ translation
9
18
  end
10
19
 
11
20
  private
12
21
 
13
- def parsed_data
14
- translation = @text.scan(/(TRANSLATED_TEXT=')(.*?)(')/).pop[1]
22
+ def extract_translation
23
+ @text.split("[[")[1].split("\"")[1]
24
+ end
25
+
26
+ def extract_synonyms
27
+ synonyms_data = @text.split("[[")[2].split("[")[1]
28
+ length = synonyms_data.length
29
+ if synonyms_data[0] == '4' # my code works i have no idea why ..
30
+ " ---"
31
+ else
32
+ synonyms_data[0..(length-3)].delete("\"").gsub(/(,)/, ", ")
33
+ end
34
+ end
35
+
36
+ def decode text
15
37
  encoding = 'UTF-8'
16
- translation.gsub!(/(\\x26#39;)/, "'")
17
- translation.force_encoding(encoding).encode(encoding)
38
+ text.gsub!(/(\\x26#39;)/, "'")
39
+ text.force_encoding(encoding).encode(encoding)
18
40
  end
19
41
  end
20
42
  end
@@ -1,20 +1,16 @@
1
+ #encoding: UTF-8
1
2
  module Termit
2
3
  class TextTranslator
3
4
  attr_reader :text
4
5
 
5
6
  def initialize options
6
- @options = options.merge(voice: false)
7
+ @options = options.merge(talk: false)
7
8
  @url = Termit::UrlConstructor.new(@options).url
8
9
  end
9
10
 
10
11
  def call
11
12
  response = Termit::DataFetcher.new(@url, @options[:text]).data
12
- print_info
13
- @text = Termit::TextResponseHandler.new(response.body).call
14
- end
15
-
16
- def print_info
17
- puts "Translation from #{@options[:source_lang].upcase} to #{@options[:target_lang].upcase}:"
13
+ @text = Termit::TextResponseHandler.new(response.body, @options[:synonyms]).call
18
14
  end
19
15
  end
20
16
  end
@@ -1,12 +1,15 @@
1
+ #encoding: UTF-8
1
2
  module Termit
2
3
  class UrlConstructor
4
+ Host = "http://translate.google.com"
5
+
3
6
  def initialize options
4
7
  @options = options
5
8
  end
6
9
 
7
10
  def url
8
- if @options[:voice]
9
- construct_voice_url
11
+ if @options[:talk]
12
+ construct_sound_url
10
13
  else
11
14
  construct_text_url
12
15
  end
@@ -15,15 +18,11 @@ module Termit
15
18
  private
16
19
 
17
20
  def construct_text_url
18
- "#{host}/?tl=#{@options[:target_lang]}&sl=#{@options[:source_lang]}&ie=UTF-8&oe=UTF-8"
19
- end
20
-
21
- def construct_voice_url
22
- "#{host}/translate_tts?tl=#{@options[:target_lang]}&ie=UTF-8&oe=UTF-8"
21
+ "#{Host}/translate_a/t?client=t&sl=#{@options[:source_lang]}&tl=#{@options[:target_lang]}&hl=pl&sc=2&ie=UTF-8&oe=UTF-8&prev=enter&ssel=0&tsel=0&"
23
22
  end
24
23
 
25
- def host
26
- "http://translate.google.com"
24
+ def construct_sound_url
25
+ "#{Host}/translate_tts?tl=#{@options[:target_lang]}&ie=UTF-8&oe=UTF-8"
27
26
  end
28
27
  end
29
- end
28
+ end
@@ -1,39 +1,55 @@
1
+ #encoding: UTF-8
1
2
  module Termit
2
3
  class UserInputParser
4
+ extend ::Delegation
5
+ @output_manager = Termit::OutputManager.new
6
+
7
+ delegate :display_error_info, :display_help, :display_version, to: @output_manager
3
8
  def initialize user_input
4
- raise ArgumentError unless user_input.is_a? Array
5
9
  @user_input = user_input
10
+ quit_if_required
11
+ validate_user_input
6
12
  end
7
13
 
8
14
  def options
9
- parsed = parse_input
10
- validate_user_input parsed
11
- parsed
12
- rescue ArgumentError, NoMethodError
13
- puts "TERMIT: Wrong data. Example: 'termit pl en morowy' => pestiferous"
15
+ parse_input
16
+ rescue ArgumentError
17
+ display_error_info
14
18
  exit
15
19
  end
16
20
 
17
-
18
21
  private
19
22
 
20
23
  def parse_input
21
- parsed = {
22
- voice: voice_flag,
24
+ {
25
+ talk: extract_flag('t'),
26
+ synonyms: extract_flag('s'),
23
27
  source_lang: @user_input.shift.to_sym,
24
28
  target_lang: @user_input.shift.to_sym,
25
29
  text: @user_input.join(' ')
26
30
  }
27
31
  end
28
32
 
29
- def voice_flag
30
- flag_index = @user_input.index('-v')
33
+ def extract_flag(symbol)
34
+ flag_index = @user_input.index("-#{symbol}")
31
35
  flag_index ? !!@user_input.delete_at(flag_index) : false
32
36
  end
33
37
 
34
- def validate_user_input hash
35
- [:source_lang, :target_lang].each do |key|
36
- raise ArgumentError unless [2, 4].include?(hash[key].length)
38
+ def validate_user_input
39
+ raise ArgumentError unless @user_input.is_a? Array
40
+ [0, 1].each do |index|
41
+ raise ArgumentError unless [2, 4].include?(@user_input[index].length)
42
+ end
43
+ end
44
+
45
+ def quit_if_required
46
+ if @user_input.index("-h") || @user_input.empty?
47
+ display_help
48
+ exit
49
+ end
50
+ if @user_input.index("-v")
51
+ display_version
52
+ exit
37
53
  end
38
54
  end
39
55
  end
@@ -1,3 +1,3 @@
1
1
  module Termit
2
- VERSION = "1.2.2"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -0,0 +1,73 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://translate.google.com/translate_a/t?client=t&hl=pl&ie=UTF-8&oe=UTF-8&prev=enter&sc=2&sl=en&ssel=0&tl=pl&tsel=0
6
+ body:
7
+ encoding: US-ASCII
8
+ string: text=hey+cowboy+where+is+your+horse
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - '*/*'
14
+ User-Agent:
15
+ - Ruby
16
+ Content-Type:
17
+ - application/x-www-form-urlencoded
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Pragma:
24
+ - no-cache
25
+ Date:
26
+ - Wed, 23 Oct 2013 15:15:36 GMT
27
+ Expires:
28
+ - Wed, 23 Oct 2013 15:15:36 GMT
29
+ Cache-Control:
30
+ - private, max-age=600
31
+ Content-Type:
32
+ - text/javascript; charset=UTF-8
33
+ Content-Language:
34
+ - pl
35
+ Set-Cookie:
36
+ - PREF=ID=59f956040e7bb9f8:TM=1382541336:LM=1382541336:S=8F7wmAjCUSXM3gWD; expires=Fri,
37
+ 23-Oct-2015 15:15:36 GMT; path=/; domain=.google.com
38
+ X-Content-Type-Options:
39
+ - nosniff
40
+ Content-Disposition:
41
+ - attachment
42
+ Server:
43
+ - HTTP server (unknown)
44
+ X-Xss-Protection:
45
+ - 1; mode=block
46
+ Alternate-Protocol:
47
+ - 80:quic
48
+ Transfer-Encoding:
49
+ - chunked
50
+ body:
51
+ encoding: ASCII-8BIT
52
+ string: !binary |-
53
+ W1tbImhleSBjb3dib3kgZ2R6aWU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