Skip to content

Commit

Permalink
Code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
zaf committed Jul 29, 2015
1 parent a516858 commit 26643e3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 37 deletions.
10 changes: 3 additions & 7 deletions cli/googletts-cli-tiny.pl
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@
my %options;
my @text;
my @mp3list;
my @soxargs;
my $samplerate;
my $input;
my $url;
my $http;
my $speed = 1;
my $lang = "en-US";
my $tmpdir = "/tmp";
my $timeout = 10;
my $host = "translate.google.com/translate_tts";
my $url = "https://translate.google.com/translate_tts";
my $mpg123 = `/usr/bin/which mpg123`;
my $sox = `/usr/bin/which sox`;

Expand Down Expand Up @@ -62,8 +59,7 @@
}

# Initialise User angent #
$url = "https://" . $host;
$http = HTTP::Tiny->new(
my $http = HTTP::Tiny->new(
agent => 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
timeout => $timeout,
verify_SSL => 1,
Expand Down Expand Up @@ -109,7 +105,7 @@
}

# Set sox args and process wav file #
@soxargs = ($sox, "-q", $wav_name);
my @soxargs = ($sox, "-q", $wav_name);
defined $options{o} ? push(@soxargs, ($options{o})) : push(@soxargs, ("-t", "alsa", "-d"));
push(@soxargs, ("tempo", "-s", $speed)) if ($speed != 1);
push(@soxargs, ("rate", $samplerate)) if ($samplerate);
Expand Down
10 changes: 3 additions & 7 deletions cli/googletts-cli.pl
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@
my %options;
my @text;
my @mp3list;
my @soxargs;
my $samplerate;
my $input;
my $url;
my $ua;
my $speed = 1;
my $lang = "en-US";
my $tmpdir = "/tmp";
my $timeout = 10;
my $host = "translate.google.com/translate_tts";
my $url = "https://translate.google.com/translate_tts";
my $mpg123 = `/usr/bin/which mpg123`;
my $sox = `/usr/bin/which sox`;

Expand Down Expand Up @@ -63,8 +60,7 @@
}

# Initialise User angent #
$url = "https://" . $host;
$ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
$ua->agent("Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36");
$ua->env_proxy;
$ua->conn_cache(LWP::ConnCache->new());
Expand Down Expand Up @@ -111,7 +107,7 @@
}

# Set sox args and process wav file #
@soxargs = ($sox, "-q", $wav_name);
my @soxargs = ($sox, "-q", $wav_name);
defined $options{o} ? push(@soxargs, ($options{o})) : push(@soxargs, ("-t", "alsa", "-d"));
push(@soxargs, ("tempo", "-s", $speed)) if ($speed != 1);
push(@soxargs, ("rate", $samplerate)) if ($samplerate);
Expand Down
17 changes: 6 additions & 11 deletions googletts-tiny.agi
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,16 @@ my $debug = 0;

my %AGI;
my @text;
my $http;
my $res;
my $fh;
my $tmpname;
my $filename;
my $fexten;
my @result;
my $name;
my $url;
my $sox;
my $mpg123;
my $intkey = "";
my $tmpdir = "/tmp";
my $maxlen = 4096;
my $timeout = 10;
my $host = "translate.google.com/translate_tts";
my $url = "https://translate.google.com/translate_tts";

# Store AGI input #
($AGI{arg_1}, $AGI{arg_2}, $AGI{arg_3}, $AGI{arg_4}) = @ARGV;
Expand All @@ -97,7 +91,7 @@ while (<STDIN>) {
last if (!length);
$AGI{$1} = $2 if (/^agi_(\w+)\:\s+(.*)$/);
}
$name = " -- $AGI{request}:";
my $name = " -- $AGI{request}:";

# Sanitising input #
$AGI{arg_1} = decode('utf8', $AGI{arg_1});
Expand Down Expand Up @@ -139,7 +133,7 @@ if ($usecache && ((length($cachedir) + 32 + 6) < $maxlen)) {

# Answer channel if not already answered #
print "CHANNEL STATUS\n";
@result = checkresponse();
my @result = checkresponse();
if ($result[0] == 4) {
print "ANSWER\n";
checkresponse();
Expand All @@ -155,14 +149,15 @@ elsif ($samplerate == 48000) { $fexten = "sln48"; }
else { ($fexten, $samplerate) = ("sln", 8000); }

# Initialise User angent #
$url = "https://" . $host;
$http = HTTP::Tiny->new(
my $http = HTTP::Tiny->new(
agent => 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
timeout => $timeout,
verify_SSL => 1,
);

foreach my $line (@text) {
my $filename;
my $res;
$line = encode('utf8', $line);
$line =~ s/^\s+|\s+$//g;
next if (length($line) == 0);
Expand Down
19 changes: 7 additions & 12 deletions googletts.agi
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,14 @@ my $debug = 0;

my %AGI;
my @text;
my $ua;
my $res;
my $fh;
my $tmpname;
my $filename;
my $fexten;
my @result;
my $name;
my $url;
my $intkey = "";
my $tmpdir = "/tmp";
my $maxlen = 4096;
my $timeout = 10;
my $host = "translate.google.com/translate_tts";
my $url = "https://translate.google.com/translate_tts";
my $sox = `/usr/bin/which sox`;
my $mpg123 = `/usr/bin/which mpg123`;

Expand All @@ -99,7 +93,7 @@ while (<STDIN>) {
$AGI{$1} = $2 if (/^agi_(\w+)\:\s+(.*)$/);
}

$name = " -- $AGI{request}:";
my $name = " -- $AGI{request}:";

# Abort if required programs not found. #
die "$name sox or mpg123 is missing. Aborting.\n" if (!$sox || !$mpg123);
Expand Down Expand Up @@ -146,7 +140,7 @@ if ($usecache && ((length($cachedir) + 32 + 6) < $maxlen)) {

# Answer channel if not already answered #
print "CHANNEL STATUS\n";
@result = checkresponse();
my @result = checkresponse();
if ($result[0] == 4) {
print "ANSWER\n";
checkresponse();
Expand All @@ -162,14 +156,15 @@ elsif ($samplerate == 48000) { $fexten = "sln48"; }
else { ($fexten, $samplerate) = ("sln", 8000); }

# Initialise User angent #
$url = "https://" . $host;
$ua = LWP::UserAgent->new(ssl_opts => {verify_hostname => 1});
my $ua = LWP::UserAgent->new(ssl_opts => {verify_hostname => 1});
$ua->agent("Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36");
$ua->env_proxy;
$ua->conn_cache(LWP::ConnCache->new());
$ua->timeout($timeout);

foreach my $line (@text) {
my $filename;
my $res;
$line = encode('utf8', $line);
$line =~ s/^\s+|\s+$//g;
next if (length($line) == 0);
Expand All @@ -195,7 +190,7 @@ foreach my $line (@text) {
$SIG{'INT'} = \&int_handler;
$SIG{'HUP'} = \&int_handler;

($fh, $tmpname) = tempfile("ggl_XXXXXX", DIR => $tmpdir, UNLINK => 1);
my ($fh, $tmpname) = tempfile("ggl_XXXXXX", DIR => $tmpdir, UNLINK => 1);
my $ua_request = HTTP::Request->new('GET' => "$url?ie=UTF-8&q=$line&tl=$lang&total=1&idx=0&client=t");
$ua_request->header(
'Accept' => 'audio/webm,audio/ogg,audio/wav,audio/*;q=0.9,application/ogg;q=0.7,video/*;q=0.6,*/*;q=0.5',
Expand Down

0 comments on commit 26643e3

Please sign in to comment.