txt2speech 0.9 → 1.1

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: 5133c8bc434553c7bda4b58fdbb7b1ef60854348
4
- data.tar.gz: 5db3ac6a8f615dcc506409b79dfe1ee5a318c2e6
3
+ metadata.gz: 62d739323d097d547cf6d5f31896445be172bde5
4
+ data.tar.gz: dd9a36baf44ade2da2ba4b05a1ec50b18f05b2d3
5
5
  SHA512:
6
- metadata.gz: 699c606c2a03c7e18a4f4d3dd31f923d8634ec84f869db9281d0433a8760acb95cce9d30666f1e5c5a454a968bddb649cea2a6e8a9bbfa12c04f7eb48796a36c
7
- data.tar.gz: 5c3198184178eb77c0874cfe4696948ee79fa19d4ac6a9797df5c20fc175d515bca3adfbea83a56c1fdd5d6c1532b259bdd25569cf848041176984fafb1fc250
6
+ metadata.gz: 228907e50e2691db3347f5b60390bf6b972660cbb592ef8b6b241e7dc3be77b7e7d293564da692d267f3eff768554f1e97518bbdbcdf5c10b89668c92110a2a7
7
+ data.tar.gz: ae28be2efbb1eee95cf0b8b0e1929f9b50f3886295a4046a1ca5b7ee180435f2ed14aa903800b8d3b752a73d3f378b95781ff6b3bdf6dc291f76483be70eb035
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ .byebug_history
3
+ *.gem
data/Gemfile CHANGED
@@ -1,5 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'bundle'
3
+ gem 'bundler'
4
4
  gem 'rake'
5
- gem 'rspec', :require => 'spec'
5
+ gem 'rspec', require: 'spec'
@@ -1,8 +1,6 @@
1
1
  GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
- bundle (0.0.1)
5
- bundler
6
4
  diff-lcs (1.2.5)
7
5
  rake (10.4.2)
8
6
  rspec (3.3.0)
@@ -23,6 +21,9 @@ PLATFORMS
23
21
  ruby
24
22
 
25
23
  DEPENDENCIES
26
- bundle
24
+ bundler
27
25
  rake
28
26
  rspec
27
+
28
+ BUNDLED WITH
29
+ 1.12.5
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  [![Build Status](https://travis-ci.org/rudkovskyi/txt2speech.svg?branch=master)](https://travis-ci.org/rudkovsky/txt2speech)
2
2
 
3
- txt2Speech is a library that with a help of Google Translate let you convert your text to speech
3
+ txt2speech is a very simple library that by using Google Translate undocumented API allow you convert text to speech.
4
4
 
5
5
  ```
6
6
  bin/txt2speech -r README.md -f -o example.mpg
@@ -23,3 +23,5 @@ or you can load a text file to read it
23
23
  => #<Txt2Speech::Speech:0x000000022314f8 @text="txt2Speech is a simple ruby script that convert text to speech by using Google Translate.\r\n\r\n```\r\n2.1.2 :001 > require './txt2speech.rb'\r\n => true\r\n2.1.2 :002 > f = Txt2Speech.new \"Hello I am google! Nice to meet you\"\r\n => #<Txt2Speech:0x000000018aafd8 @text=\"Hello I am google! Nice to meet you\", @lang=\"en\">\r\n2.1.2 :003 > f.save(\"out.mpg\")\r\n => \"out.mpg\"\r\n```\r\n\r\nor you can load a text file to read it\r\n\r\n```\r\nt = Txt2Speech.load \"\#{Dir.home}/text.txt\"\r\nt.save(\"out.mpg\")\r\n```", @lang="en">
24
24
  2.1.2 :007 > f.save("out.mpg")
25
25
  ```
26
+
27
+ The differences between Google and Bing - [How Google and Bing protects their API](https://rudk.ws/2016/10/23/how-google-and-bing-protects-their-api/)
data/Rakefile CHANGED
@@ -1,3 +1,3 @@
1
1
  require 'rspec/core/rake_task'
2
2
  RSpec::Core::RakeTask.new(:spec)
3
- task :default => :spec
3
+ task default: :spec
@@ -1,7 +1,7 @@
1
1
  require_relative '../lib/txt2speech'
2
2
  require 'rss'
3
3
 
4
- rss = RSS::Parser.parse('http://techcrunch.com/feed/', false)
4
+ rss = RSS::Parser.parse('http://feeds.feedburner.com/TechCrunch/startups', false)
5
5
 
6
6
  f = Txt2Speech::Speech.new(rss.items.map{|item| item.title}.join('.'))
7
- f.save("rss_reader.mpg")
7
+ f.save("rss_reader.mpg")
@@ -1 +1 @@
1
- require_relative 'txt2speech/speech'
1
+ require_relative 'txt2speech/speech'
@@ -1,63 +1,71 @@
1
- # encoding: UTF-8
2
-
3
- require 'net/http'
4
-
5
- module Txt2Speech
6
- class Speech
7
- GOOGLE_TRANSLATE_URL = 'http://translate.google.com/translate_tts'
8
-
9
- attr_accessor :text, :lang
10
-
11
- def initialize(text, lang = 'en')
12
- @text = text
13
- @lang = lang
14
- end
15
-
16
- def self.load(file_path, lang = 'en')
17
- f = File.open(file_path)
18
- self.new f.read.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8'), lang
19
- end
20
-
21
- def save(file_path)
22
- uri = URI(GOOGLE_TRANSLATE_URL)
23
-
24
- response = []
25
- ar = text.split(/[,.\r\n]/i)
26
- ar.reject! {|t| t.empty? }
27
- ar.map! {|t| divide(t) }.flatten!
28
- ar.map! {|t| t.strip }
29
-
30
- puts ar.inspect
31
-
32
- ar.each_with_index do |q, idx|
33
- uri.query = URI.encode_www_form({ ie: 'UTF-8', q: q, tl: lang, total: ar.length, idx: 0, textlen: q.length, client: 'tw-ob', prev: 'input' })
34
- res = Net::HTTP.get_response(uri)
35
-
36
- response << res.body.force_encoding(Encoding::UTF_8) if res.is_a?(Net::HTTPSuccess)
37
- end
38
-
39
- File.open(file_path, 'wb') do |f|
40
- f.write response.join
41
- return f.path
42
- end
43
- end
44
-
45
- private
46
-
47
- def divide(text)
48
- return text if text.length < 150
49
-
50
- attempts = text.length / 150.0
51
- starts = 0
52
- ar = []
53
-
54
- attempts.ceil.times do
55
- ends = starts + 150
56
- ar << text[starts...ends]
57
- starts = ends
58
- end
59
-
60
- ar
61
- end
62
- end
63
- end
1
+ require 'net/http'
2
+
3
+ module Txt2Speech
4
+ class Speech
5
+ GOOGLE_TRANSLATE_URL = 'http://translate.google.com/translate_tts'.freeze
6
+
7
+ attr_accessor :text, :lang
8
+
9
+ def initialize(text, lang = 'en')
10
+ @text = text
11
+ @lang = lang
12
+ end
13
+
14
+ def self.load(file_path, lang = 'en')
15
+ f = File.open(file_path)
16
+ new f.read.encode('UTF-16be', invalid: :replace, replace: '?').encode('UTF-8'), lang
17
+ end
18
+
19
+ def save(file_path)
20
+ uri = URI(GOOGLE_TRANSLATE_URL)
21
+
22
+ response = []
23
+
24
+ sentences = text.split(/[,.\r\n]/i)
25
+ sentences.reject!(&:empty?)
26
+ sentences.map! { |t| divide(t.strip) }.flatten!
27
+
28
+ sentences.each_with_index do |q, _idx|
29
+ uri.query = URI.encode_www_form(
30
+ ie: 'UTF-8',
31
+ q: q,
32
+ tl: lang,
33
+ total: sentences.length,
34
+ idx: 0,
35
+ textlen: q.length,
36
+ client: 'tw-ob',
37
+ prev: 'input'
38
+ )
39
+
40
+ res = Net::HTTP.get_response(uri)
41
+
42
+ next unless res.is_a?(Net::HTTPSuccess)
43
+
44
+ response << res.body.force_encoding(Encoding::UTF_8)
45
+ end
46
+
47
+ File.open(file_path, 'wb') do |f|
48
+ f.write response.join
49
+ return f.path
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def divide(text)
56
+ return text if text.length < 150
57
+
58
+ attempts = text.length / 150.0
59
+ starts = 0
60
+ arr = []
61
+
62
+ attempts.ceil.times do
63
+ ends = starts + 150
64
+ arr << text[starts...ends]
65
+ starts = ends
66
+ end
67
+
68
+ arr
69
+ end
70
+ end
71
+ end
@@ -1,3 +1,3 @@
1
- module Txt2Speech
2
- VERSION = '0.9'
3
- end
1
+ module Txt2Speech
2
+ VERSION = '1.1'.freeze
3
+ end
@@ -4,25 +4,30 @@ require './lib/txt2speech'
4
4
 
5
5
  RSpec.describe Txt2Speech::Speech, '#new' do
6
6
  context 'with english text' do
7
- let!(:output_path) { "/tmp/txt2speech.mp4" }
7
+ let!(:output_path) { '/tmp/txt2speech.mp4' }
8
8
 
9
9
  it 'converts string to the audio file' do
10
- @speech = Txt2Speech::Speech.new("Hello World")
10
+ @speech = Txt2Speech::Speech.new('Hello World')
11
+ end
12
+
13
+ it 'converts string longer than 150 character into the audio file' do
14
+ @speech = Txt2Speech::Speech.new('Super long string abcdef' * 10)
11
15
  end
12
16
 
13
17
  it 'load a file and save it to audio file' do
14
- File.open("/tmp/txt2speech.txt", "w+") do |f|
15
- f.write("Hello World from file")
18
+ File.open('/tmp/txt2speech.txt', 'w+') do |f|
19
+ f.write('Hello World from file')
16
20
  end
17
- @speech = Txt2Speech::Speech.load("/tmp/txt2speech.txt")
21
+
22
+ @speech = Txt2Speech::Speech.load('/tmp/txt2speech.txt')
18
23
  end
19
24
 
20
25
  after(:each) do
21
26
  @speech.save(output_path)
22
- expect(File.exists?(output_path)).to eq(true)
27
+ expect(File.exist?(output_path)).to eq(true)
23
28
  expect(File.size(output_path)).to be > 0
24
- [output_path, "/tmp/txt2speech.txt"].each do |f|
25
- FileUtils.rm(f) if File.exists?(f)
29
+ [output_path, '/tmp/txt2speech.txt'].each do |f|
30
+ FileUtils.rm(f) if File.exist?(f)
26
31
  end
27
32
  end
28
33
  end
@@ -1,17 +1,17 @@
1
- lib = File.expand_path('../lib', __FILE__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require 'txt2speech/version'
4
-
5
- Gem::Specification.new do |s|
6
- s.name = 'txt2speech'
7
- s.version = Txt2Speech::VERSION
8
- s.date = '2014-12-17'
9
- s.summary = "txt2speech"
10
- s.description = "txt2Speech lifehack of using Google Translate. Allow you to convert text into speech."
11
- s.authors = ["Viacheslav Rudkovskyi"]
12
- s.email = 'rrubyist@gmail.com'
13
- s.files = `git ls-files`.split("\n")
14
- s.executables = ['txt2speech']
15
- s.homepage = 'http://github.com/rudkovsky/txt2speech'
16
- s.license = 'MIT'
17
- end
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'txt2speech/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'txt2speech'
7
+ s.version = Txt2Speech::VERSION
8
+ s.date = Time.now.strftime('%Y-%m-%d')
9
+ s.summary = 'txt2speech'
10
+ s.description = 'txt2speech is a very simple library that by using Google Translate undocumented API allow you convert text to speech.'
11
+ s.authors = ['Viacheslav Rudkovskyi']
12
+ s.email = 'rrubyist@gmail.com'
13
+ s.files = `git ls-files`.split("\n")
14
+ s.executables = ['txt2speech']
15
+ s.homepage = 'http://github.com/rudkovsky/txt2speech'
16
+ s.license = 'MIT'
17
+ end
metadata CHANGED
@@ -1,23 +1,24 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: txt2speech
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.9'
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viacheslav Rudkovskyi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-17 00:00:00.000000000 Z
11
+ date: 2017-08-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: txt2Speech lifehack of using Google Translate. Allow you to convert text
14
- into speech.
13
+ description: txt2speech is a very simple library that by using Google Translate undocumented
14
+ API allow you convert text to speech.
15
15
  email: rrubyist@gmail.com
16
16
  executables:
17
17
  - txt2speech
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - ".gitignore"
21
22
  - ".rspec"
22
23
  - ".travis.yml"
23
24
  - Gemfile
@@ -53,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
54
  version: '0'
54
55
  requirements: []
55
56
  rubyforge_project:
56
- rubygems_version: 2.5.1
57
+ rubygems_version: 2.6.10
57
58
  signing_key:
58
59
  specification_version: 4
59
60
  summary: txt2speech