From 1d16db959a75a749f57d423436dc132373cdf43b Mon Sep 17 00:00:00 2001 From: Joe Rayhawk Date: Thu, 7 Jul 2022 23:33:41 -0700 Subject: bungmobott: generate voicelist.txt if missing --- crystal/bungmobott.cr | 95 ++++++++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 43 deletions(-) diff --git a/crystal/bungmobott.cr b/crystal/bungmobott.cr index 241db6d..5e0caed 100755 --- a/crystal/bungmobott.cr +++ b/crystal/bungmobott.cr @@ -284,6 +284,51 @@ def playaudiofile( filepath : String ) p.wait end +def regeneratevoicelist( settings : Hash( String, String ), aws : Bool, gcloud : Bool ) + voices = Hash(String, String).new + if aws + JSON.parse( `aws polly describe-voices` )["Voices"].as_a.each do | v | + voices[ v["Id"].as_s.downcase ] = v["Id"].as_s + end + end + if gcloud + ssl_context = OpenSSL::SSL::Context::Client.new + {% if flag?(:windows) %} + ssl_context.verify_mode = OpenSSL::SSL::VerifyMode::NONE + {% end %} + headers = HTTP::Headers.new + headers["Content-Type"] = "application/json; charset=utf-8" + + response = HTTP::Client.exec( "GET", "https://texttospeech.googleapis.com/v1/voices?key=#{settings["gcloud_token"]}", headers, nil, tls: ssl_context ) + + JSON.parse( response.body )["voices"].as_a.each do | v | + voices[ v["name"].as_s.downcase ] = v["name"].as_s + end + end + {% if flag?(:windows) %} + p = Process.new( + "powershell.exe", + [ "-Command", " + Add-Type -AssemblyName System.Speech; + $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer; + $speak.GetInstalledVoices().VoiceInfo | Select-Object Name + "], + output: Process::Redirect::Pipe + ) + p.output.each_line do | v | + v = v.gsub(/ +$/, "") + v = v.gsub(/ Desktop$/, "") + v = v.gsub( " ", "-" ) + if v =~ /[A-Za-z0-9]-[A-Za-z0-9]/ + voices[ v.downcase ] = v + end + end + {% end %} + + File.write( settings["configdir"] + "/voicelist.txt", voices.values.sort.join("\r\n") ) + return voices +end + macro testrefuser2uid( path ) {% if flag?(:windows) %} File.exists?( {{path}} ) && ( File.read( {{ path }} ) =~ /^[0-9]+$/ ) @@ -320,8 +365,12 @@ macro temporaryduplicate( item ) end voices = Hash(String, String).new -File.each_line( settings["configdir"] + "/voicelist.txt" ) do |line| - voices[ line.downcase ] = line +if File.exists?( settings["configdir"] + "/voicelist.txt" ) + File.each_line( settings["configdir"] + "/voicelist.txt" ) do |line| + voices[ line.downcase ] = line + end +else + voices = regeneratevoicelist( settings, aws, gcloud ) end lastvoice = Array(String).new @@ -615,47 +664,7 @@ loop do bot.message( "##{settings["channel"]}", "| No voices used so far." ) end elsif ( ( cmd == "regeneratevoicelist" ) && ( own ) ) - voicelist = Array( String ).new - if aws - JSON.parse( `aws polly describe-voices` )["Voices"].as_a.each do | v | - voicelist.push( v["Name"].as_s ) - end - end - if gcloud - ssl_context = OpenSSL::SSL::Context::Client.new - {% if flag?(:windows) %} - ssl_context.verify_mode = OpenSSL::SSL::VerifyMode::NONE - {% end %} - headers = HTTP::Headers.new - headers["Content-Type"] = "application/json; charset=utf-8" - - response = HTTP::Client.exec( "GET", "https://texttospeech.googleapis.com/v1/voices?key=#{settings["gcloud_token"]}", headers, nil, tls: ssl_context ) - - JSON.parse( response.body )["voices"].as_a.each do | v | - voicelist.push( v["name"].as_s ) - end - end - {% if flag?(:windows) %} - p = Process.new( - "powershell.exe", - [ "-Command", " - Add-Type -AssemblyName System.Speech; - $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer; - $speak.GetInstalledVoices().VoiceInfo | Select-Object Name - "], - output: Process::Redirect::Pipe - ) - p.output.each_line do | v | - v = v.gsub(/ +$/, "") - v = v.gsub(/ Desktop$/, "") - v = v.gsub( " ", "-" ) - if v =~ /[A-Za-z0-9]-[A-Za-z0-9]/ - voicelist.push( v ) - end - end - {% end %} - - File.write( settings["configdir"] + "/voicelist.txt", voicelist.join("\r\n") ) + voices = regeneratevoicelist( settings, aws, gcloud ) ircipc.send( { "##{settings["channel"]}", "| Regenerated voicelist." } ) elsif ( ( cmd =~ /^(voices|voicelist)$/ ) && ( mod || sub ) ) bot.message( "##{settings["channel"]}", "| https://bungmonkey.omgwallhack.org/voicelist.txt" ) -- cgit v1.2.3