-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogletts
56 lines (46 loc) · 1.3 KB
/
googletts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
set -o nounset
set -o errexit
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
# Initialize our own variables:
verbose=0
force=0
PLAYER=/usr/bin/mplayer
PLAYER_ARGS="-ao alsa -really-quiet -volume 100"
forceSay() {
[ "$verbose" = "1" ] && echo "Playing the mp3 directly from the url"
${PLAYER} ${PLAYER_ARGS} "http://translate.google.com/translate_tts?tl=en&q=$*";
}
cacheSay() {
hashcode="/var/cache/googletts/."$(echo "$*" | md5sum | cut -d" " -f1)".mp3"
# Check that the file exists
if [ ! -f "${hashcode}" ]; then
[ "$verbose" = "1" ] && echo "Retrieving the mp3, ${hashcode}"
mkdir --parents /var/cache/googletts/
wget --quiet --user-agent="Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0" --output-document $hashcode "http://translate.google.com/translate_tts?tl=en&q=$*"
else
[ "$verbose" = "1" ] && echo "The mp3 is already cached, ${hashcode}"
fi
${PLAYER} ${PLAYER_ARGS} $hashcode;
}
while getopts "h?vf" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
v) verbose=1
;;
f) force=1
;;
esac
done
shift $((OPTIND-1))
[ ! -z ${var+x} ] && [ "$1" = "--" ] && shift
[ "$verbose" = "1" ] && echo "verbose=$verbose, force=$force, Text: $@"
if [ "$force" = "1" ]; then
forceSay $*
else
cacheSay $*
fi