6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/wasabi_pea.rb', line 6
def to_speech(language, file_name = nil)
base_url = "http://translate.google.com/translate_tts?tl=#{language}&q=#{URI.escape self}"
file = File.join("tmp", (file_name || self) + ".mp3")
user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.68 Safari/534.24"
begin
content = open(base_url, "User-Agent" => user_agent).read
File.open(file, 'wb') do |f|
f.puts content
end
rescue Exception => error
$stderr.puts(" #{error.message}")
return nil
end
begin
io = StringIO.new(open(file, "rb") {|io| io.read })
io.original_filename = "#{file_name}.mp3"
io.content_type = "audio/mpeg"
return io
rescue
return nil
end
end
|