Class: Gtts::GTTS
- Inherits:
-
Object
- Object
- Gtts::GTTS
- Defined in:
- lib/gtts/tts.rb
Constant Summary collapse
- GOOGLE_TTS_MAX_CHARS =
100- GOOGLE_TTS_HEADERS =
{ "Referer" => "http://translate.google.com/", "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36", "Content-Type" => "application/x-www-form-urlencoded;charset=utf-8" }.freeze
- GOOGLE_TTS_RPC =
"jQ1olc"
Instance Attribute Summary collapse
-
#lang ⇒ Object
readonly
Returns the value of attribute lang.
-
#lang_check ⇒ Object
readonly
Returns the value of attribute lang_check.
-
#speed ⇒ Object
readonly
Returns the value of attribute speed.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#tld ⇒ Object
readonly
Returns the value of attribute tld.
Instance Method Summary collapse
-
#initialize(text, tld: "com", lang: "en", slow: false, lang_check: true) ⇒ GTTS
constructor
A new instance of GTTS.
- #save(filename) ⇒ Object
- #stream ⇒ Object
Constructor Details
#initialize(text, tld: "com", lang: "en", slow: false, lang_check: true) ⇒ GTTS
Returns a new instance of GTTS.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/gtts/tts.rb', line 63 def initialize(text, tld: "com", lang: "en", slow: false, lang_check: true) raise ArgumentError, "No text to speak" if text.nil? || text.empty? @text = text @tld = tld @lang_check = lang_check @lang = lang @speed = slow ? Speed::SLOW : Speed::NORMAL validate_language if @lang_check end |
Instance Attribute Details
#lang ⇒ Object (readonly)
Returns the value of attribute lang.
61 62 63 |
# File 'lib/gtts/tts.rb', line 61 def lang @lang end |
#lang_check ⇒ Object (readonly)
Returns the value of attribute lang_check.
61 62 63 |
# File 'lib/gtts/tts.rb', line 61 def lang_check @lang_check end |
#speed ⇒ Object (readonly)
Returns the value of attribute speed.
61 62 63 |
# File 'lib/gtts/tts.rb', line 61 def speed @speed end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
61 62 63 |
# File 'lib/gtts/tts.rb', line 61 def text @text end |
#tld ⇒ Object (readonly)
Returns the value of attribute tld.
61 62 63 |
# File 'lib/gtts/tts.rb', line 61 def tld @tld end |
Instance Method Details
#save(filename) ⇒ Object
103 104 105 106 107 |
# File 'lib/gtts/tts.rb', line 103 def save(filename) File.open(filename, "wb") do |file| stream { |chunk| file.write(chunk) } end end |
#stream ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/gtts/tts.rb', line 75 def stream prepared_requests.each_with_index do |request, idx| client = HTTP.headers(GOOGLE_TTS_HEADERS) begin response = client.post(request[:url], form: {"f.req" => request[:body]}) unless response.status.success? raise GttsError.new(tts: self, response: response) end response.body.to_s.each_line do |line| decoded_line = line.force_encoding("UTF-8") if decoded_line.include?("jQ1olc") if (audio_match = decoded_line.match(/jQ1olc","\[\\"(.*?)\\"\]/)) as_bytes = audio_match[1].encode("ASCII") yield Base64.decode64(as_bytes) else raise GttsError.new(tts: self, response: response) end end end rescue HTTP::Error => e raise GttsError.new(tts: self, response: response) end end end |