gtts 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6c8688d9e696c00721372aab8096f5e8f18b7f28117b337fc71152d01e1273fd
4
+ data.tar.gz: 48f07c2ebf32d2efe08a12b372fd6610babcdc9825fd6d07af0a0eeacb073ea6
5
+ SHA512:
6
+ metadata.gz: a84f45da1703ddb17a4422a99d8da7e6688cefe2bd4fa30a5724c75b5507d6f58b17e3c63ad3a3fdae1a656e724a7498ccdd4d1e889cc14c031bd7e578d8689e
7
+ data.tar.gz: 04622f38048774ee15ffcb08f3dacfd478880af8804f12a419ffb07225bf0bd4dd751d70cccee7acafba8ee914dbd130c945c7d22912e9a992b6d39841cacea8
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Dinko Azema
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # GTTS Ruby
2
+
3
+ A Ruby library and CLI tool to interface with Google Translate's Text-to-Speech API. This is a Ruby port of the Python [gTTS](https://github.com/pndurette/gTTS) library.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gtts'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ $ gem install gtts
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### As a Library
28
+
29
+ ```ruby
30
+ require 'gtts'
31
+
32
+ # Simple example
33
+ tts = Gtts::GTTS.new("Hello World")
34
+ tts.save("hello.mp3")
35
+
36
+ # More options
37
+ tts = Gtts::GTTS.new(
38
+ "Hola Mundo",
39
+ lang: "es", # Language
40
+ tld: "com.mx", # Top-level domain
41
+ slow: true # Slower speed
42
+ )
43
+ tts.save("hola.mp3")
44
+
45
+ # Stream the audio data
46
+ tts.stream do |audio_chunk|
47
+ # Process audio data chunks
48
+ # For example, write to a file
49
+ File.open("output.mp3", "ab") { |f| f.write(audio_chunk) }
50
+ end
51
+
52
+ # Check supported languages
53
+ Gtts.languages.each do |code, name|
54
+ puts "#{code}: #{name}"
55
+ end
56
+
57
+ # Check if a language is supported
58
+ puts Gtts.language_supported?("es") # true
59
+ puts Gtts.language_supported?("xx") # false
60
+ ```
61
+
62
+ ### Command Line Tool
63
+
64
+ The gem comes with a command-line tool called `gtts-cli`:
65
+
66
+ ```bash
67
+ # Basic usage
68
+ gtts-cli "Hello World" -o hello.mp3
69
+
70
+ # Specify language
71
+ gtts-cli "Hola Mundo" -l es -o hola.mp3
72
+
73
+ # Read from file
74
+ gtts-cli -f input.txt -o output.mp3
75
+
76
+ # List available languages
77
+ gtts-cli --list-langs
78
+
79
+ # Show all options
80
+ gtts-cli --help
81
+ ```
82
+
83
+ ### CLI Options
84
+
85
+ - `-f, --file FILE` : Read text from file
86
+ - `-o, --output FILE` : Output file (default: output.mp3)
87
+ - `-l, --lang LANG` : Language (default: en)
88
+ - `-t, --tld TLD` : Top-level domain for Google Translate
89
+ - `-s, --slow` : Speak more slowly
90
+ - `--list-langs` : List available languages
91
+ - `--debug` : Debug mode
92
+ - `-h, --help` : Show help message
93
+
94
+ ## Supported Languages
95
+
96
+ The library supports multiple languages. Use `gtts-cli --list-langs` to see all available languages or check `Gtts.languages` in your code.
97
+
98
+ ## Contributing
99
+
100
+ Bug reports and pull requests are welcome on GitHub.
101
+
102
+ ## License
103
+
104
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/exe/gtts-cli ADDED
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "gtts"
5
+ require "optparse"
6
+
7
+ options = {
8
+ tld: "com",
9
+ lang: "en",
10
+ slow: false,
11
+ lang_check: true,
12
+ output: "output.mp3"
13
+ }
14
+
15
+ OptionParser.new do |opts|
16
+ opts.banner = "Usage: gtts-cli [options] 'text to speak'"
17
+
18
+ opts.on("-f", "--file FILE", "Read text from file") do |file|
19
+ options[:file] = file
20
+ end
21
+
22
+ opts.on("-o", "--output FILE", "Output file (default: output.mp3)") do |file|
23
+ options[:output] = file
24
+ end
25
+
26
+ opts.on("-l", "--lang LANG", "Language (default: en)") do |lang|
27
+ options[:lang] = lang
28
+ end
29
+
30
+ opts.on("-t", "--tld TLD", "Top-level domain for google translate (default: com)") do |tld|
31
+ options[:tld] = tld
32
+ end
33
+
34
+ opts.on("-s", "--slow", "Speak more slowly") do
35
+ options[:slow] = true
36
+ end
37
+
38
+ opts.on("--list-langs", "List available languages") do
39
+ puts "\nLanguage codes:"
40
+ Gtts.languages.each do |code, name|
41
+ puts "#{code}\t#{name}"
42
+ end
43
+ exit
44
+ end
45
+
46
+ opts.on("--debug", "Debug mode") do
47
+ options[:debug] = true
48
+ end
49
+
50
+ opts.on("-h", "--help", "Show this help message") do
51
+ puts opts
52
+ exit
53
+ end
54
+ end.parse!
55
+
56
+ begin
57
+ text = if options[:file]
58
+ File.read(options[:file])
59
+ elsif !ARGV.empty?
60
+ ARGV.join(" ")
61
+ else
62
+ puts "Error: No text provided. Use --help for usage information."
63
+ exit 1
64
+ end
65
+
66
+ tts = Gtts::GTTS.new(
67
+ text,
68
+ tld: options[:tld],
69
+ lang: options[:lang],
70
+ slow: options[:slow],
71
+ lang_check: options[:lang_check]
72
+ )
73
+
74
+ tts.save(options[:output])
75
+ puts "Successfully saved to #{options[:output]}"
76
+
77
+ rescue Gtts::GttsError, ArgumentError => e
78
+ puts "Error: #{e.message}"
79
+ exit 1
80
+ rescue StandardError => e
81
+ puts "Unknown error: #{e.message}"
82
+ puts e.backtrace if options[:debug]
83
+ exit 1
84
+ end
data/gtts.gemspec ADDED
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/gtts/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "gtts"
7
+ spec.version = Gtts::VERSION
8
+ spec.authors = ["rocket4ce"]
9
+ spec.email = ["rocket4ce@gmail.com"]
10
+
11
+ spec.summary = "Ruby implementation of gTTS (Google Text-to-Speech)"
12
+ spec.description = "gTTS (Google Text-to-Speech) is a Ruby library and CLI tool to interface with Google Translate text-to-speech API"
13
+ spec.homepage = "https://github.com/rocket4ce/gTTS_ruby"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.1.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/rocket4ce/gTTS_ruby"
19
+ spec.metadata["changelog_uri"] = "https://github.com/rocket4ce/gTTS_ruby/blob/main/CHANGELOG.md"
20
+ spec.metadata["rubygems_mfa_required"] = "true"
21
+
22
+ # Include all files in the gem
23
+ spec.files = Dir.glob(%w[
24
+ lib/**/*.rb
25
+ exe/**/*
26
+ *.gemspec
27
+ README.md
28
+ LICENSE.txt
29
+ ])
30
+
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ # Dependencies
36
+ spec.add_dependency "http", "~> 5.1" # For making HTTP requests
37
+ spec.add_dependency "json", "~> 2.6" # For JSON handling
38
+
39
+ # Development dependencies
40
+ spec.add_development_dependency "rake", "~> 13.0"
41
+ spec.add_development_dependency "rspec", "~> 3.0"
42
+ spec.add_development_dependency "rubocop", "~> 1.21"
43
+ end
data/lib/gtts/lang.rb ADDED
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gtts
4
+ module Lang
5
+ LANGUAGES = {
6
+ "af" => "Afrikaans",
7
+ "ar" => "Arabic",
8
+ "bg" => "Bulgarian",
9
+ "bn" => "Bengali",
10
+ "bs" => "Bosnian",
11
+ "ca" => "Catalan",
12
+ "cs" => "Czech",
13
+ "cy" => "Welsh",
14
+ "da" => "Danish",
15
+ "de" => "German",
16
+ "el" => "Greek",
17
+ "en" => "English",
18
+ "eo" => "Esperanto",
19
+ "es" => "Spanish",
20
+ "et" => "Estonian",
21
+ "fi" => "Finnish",
22
+ "fr" => "French",
23
+ "gu" => "Gujarati",
24
+ "hi" => "Hindi",
25
+ "hr" => "Croatian",
26
+ "hu" => "Hungarian",
27
+ "hy" => "Armenian",
28
+ "id" => "Indonesian",
29
+ "is" => "Icelandic",
30
+ "it" => "Italian",
31
+ "ja" => "Japanese",
32
+ "jw" => "Javanese",
33
+ "km" => "Khmer",
34
+ "kn" => "Kannada",
35
+ "ko" => "Korean",
36
+ "la" => "Latin",
37
+ "lv" => "Latvian",
38
+ "mk" => "Macedonian",
39
+ "ml" => "Malayalam",
40
+ "mr" => "Marathi",
41
+ "my" => "Myanmar (Burmese)",
42
+ "ne" => "Nepali",
43
+ "nl" => "Dutch",
44
+ "no" => "Norwegian",
45
+ "pl" => "Polish",
46
+ "pt" => "Portuguese",
47
+ "ro" => "Romanian",
48
+ "ru" => "Russian",
49
+ "si" => "Sinhala",
50
+ "sk" => "Slovak",
51
+ "sq" => "Albanian",
52
+ "sr" => "Serbian",
53
+ "su" => "Sundanese",
54
+ "sv" => "Swedish",
55
+ "sw" => "Swahili",
56
+ "ta" => "Tamil",
57
+ "te" => "Telugu",
58
+ "th" => "Thai",
59
+ "tl" => "Filipino",
60
+ "tr" => "Turkish",
61
+ "uk" => "Ukrainian",
62
+ "ur" => "Urdu",
63
+ "vi" => "Vietnamese",
64
+ "zh-CN" => "Chinese"
65
+ }.freeze
66
+
67
+ def self.tts_langs
68
+ LANGUAGES
69
+ end
70
+
71
+ def self.supported?(lang_code)
72
+ LANGUAGES.key?(lang_code)
73
+ end
74
+ end
75
+ end
data/lib/gtts/tts.rb ADDED
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "http"
4
+ require "json"
5
+ require "base64"
6
+ require "logger"
7
+ require "cgi"
8
+ require "uri"
9
+
10
+ module Gtts
11
+ class Speed
12
+ SLOW = true
13
+ NORMAL = nil
14
+ end
15
+
16
+ class GttsError < StandardError
17
+ attr_reader :tts, :response
18
+
19
+ def initialize(msg = nil, tts: nil, response: nil)
20
+ @tts = tts
21
+ @response = response
22
+ @msg = msg || infer_msg
23
+ super(@msg)
24
+ end
25
+
26
+ private
27
+
28
+ def infer_msg
29
+ cause = "Unknown"
30
+
31
+ if @response.nil?
32
+ premise = "Failed to connect"
33
+ cause = "Host '#{_translate_url}' is not reachable" if @tts&.tld != "com"
34
+ else
35
+ status = @response.status
36
+ reason = @response.reason
37
+
38
+ premise = "#{status} (#{reason}) from TTS API"
39
+
40
+ cause = case status
41
+ when 403 then "Bad token or upstream API changes"
42
+ when 404 then @tts&.tld != "com" ? "Unsupported tld '#{@tts.tld}'" : "Not found"
43
+ when 200 then "No audio stream in response. Unsupported language '#{@tts.lang}'" unless @tts&.lang_check
44
+ when 500..599 then "Upstream API error. Try again later."
45
+ end
46
+ end
47
+
48
+ "#{premise}. Probable cause: #{cause}"
49
+ end
50
+ end
51
+
52
+ class GTTS
53
+ GOOGLE_TTS_MAX_CHARS = 100
54
+ GOOGLE_TTS_HEADERS = {
55
+ "Referer" => "http://translate.google.com/",
56
+ "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36",
57
+ "Content-Type" => "application/x-www-form-urlencoded;charset=utf-8"
58
+ }.freeze
59
+ GOOGLE_TTS_RPC = "jQ1olc"
60
+
61
+ attr_reader :text, :tld, :lang, :lang_check, :speed
62
+
63
+ def initialize(text, tld: "com", lang: "en", slow: false, lang_check: true)
64
+ raise ArgumentError, "No text to speak" if text.nil? || text.empty?
65
+
66
+ @text = text
67
+ @tld = tld
68
+ @lang_check = lang_check
69
+ @lang = lang
70
+ @speed = slow ? Speed::SLOW : Speed::NORMAL
71
+
72
+ validate_language if @lang_check
73
+ end
74
+
75
+ def stream
76
+ prepared_requests.each_with_index do |request, idx|
77
+ client = HTTP.headers(GOOGLE_TTS_HEADERS)
78
+
79
+ begin
80
+ response = client.post(request[:url], form: {"f.req" => request[:body]})
81
+
82
+ unless response.status.success?
83
+ raise GttsError.new(tts: self, response: response)
84
+ end
85
+
86
+ response.body.to_s.each_line do |line|
87
+ decoded_line = line.force_encoding("UTF-8")
88
+ if decoded_line.include?("jQ1olc")
89
+ if (audio_match = decoded_line.match(/jQ1olc","\[\\"(.*?)\\"\]/))
90
+ as_bytes = audio_match[1].encode("ASCII")
91
+ yield Base64.decode64(as_bytes)
92
+ else
93
+ raise GttsError.new(tts: self, response: response)
94
+ end
95
+ end
96
+ end
97
+ rescue HTTP::Error => e
98
+ raise GttsError.new(tts: self, response: response)
99
+ end
100
+ end
101
+ end
102
+
103
+ def save(filename)
104
+ File.open(filename, "wb") do |file|
105
+ stream { |chunk| file.write(chunk) }
106
+ end
107
+ end
108
+
109
+ private
110
+
111
+ def validate_language
112
+ return unless @lang_check
113
+
114
+ unless Lang.supported?(@lang)
115
+ raise ArgumentError, "Language not supported: #{@lang}"
116
+ end
117
+ end
118
+
119
+ def prepared_requests
120
+ text_parts = tokenize(@text)
121
+ raise GttsError.new("No text to send to TTS API") if text_parts.empty?
122
+
123
+ text_parts.map do |part|
124
+ {
125
+ url: translate_url,
126
+ body: package_rpc(part)
127
+ }
128
+ end
129
+ end
130
+
131
+ def tokenize(text)
132
+ # Simple tokenization for now - split by maximum character length
133
+ # TODO: Implement more sophisticated tokenization like the Python version
134
+ text.scan(/.{1,#{GOOGLE_TTS_MAX_CHARS}}/m)
135
+ end
136
+
137
+ def translate_url
138
+ "https://translate.google.#{@tld}/_/TranslateWebserverUi/data/batchexecute"
139
+ end
140
+
141
+ def package_rpc(text)
142
+ params = [text, @lang, @speed, "null"]
143
+ escaped_params = params.to_json
144
+
145
+ rpc = [[[GOOGLE_TTS_RPC, escaped_params, nil, "generic"]]]
146
+ rpc.to_json
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gtts
4
+ VERSION = "0.1.0"
5
+ end
data/lib/gtts.rb ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "gtts/version"
4
+ require_relative "gtts/lang"
5
+ require_relative "gtts/tts"
6
+
7
+ module Gtts
8
+ class Error < StandardError; end
9
+
10
+ # Shortcut for creating a new GTTS instance
11
+ def self.create(text, **options)
12
+ GTTS.new(text, **options)
13
+ end
14
+
15
+ # Get list of supported languages
16
+ def self.languages
17
+ Lang.tts_langs
18
+ end
19
+
20
+ # Check if a language is supported
21
+ def self.language_supported?(lang_code)
22
+ Lang.supported?(lang_code)
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gtts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - rocket4ce
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-03-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.21'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.21'
83
+ description: gTTS (Google Text-to-Speech) is a Ruby library and CLI tool to interface
84
+ with Google Translate text-to-speech API
85
+ email:
86
+ - rocket4ce@gmail.com
87
+ executables:
88
+ - gtts-cli
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - LICENSE.txt
93
+ - README.md
94
+ - exe/gtts-cli
95
+ - gtts.gemspec
96
+ - lib/gtts.rb
97
+ - lib/gtts/lang.rb
98
+ - lib/gtts/tts.rb
99
+ - lib/gtts/version.rb
100
+ homepage: https://github.com/rocket4ce/gTTS_ruby
101
+ licenses:
102
+ - MIT
103
+ metadata:
104
+ homepage_uri: https://github.com/rocket4ce/gTTS_ruby
105
+ source_code_uri: https://github.com/rocket4ce/gTTS_ruby
106
+ changelog_uri: https://github.com/rocket4ce/gTTS_ruby/blob/main/CHANGELOG.md
107
+ rubygems_mfa_required: 'true'
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 3.1.0
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubygems_version: 3.4.10
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Ruby implementation of gTTS (Google Text-to-Speech)
127
+ test_files: []