Skip to content

Commit

Permalink
Google tts API and clipboard reading
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitfragit committed Jan 30, 2014
1 parent 47569d6 commit aa7202c
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 115 deletions.
23 changes: 14 additions & 9 deletions google2ubuntu-manager.py
Expand Up @@ -217,22 +217,27 @@ def create_toolbar(self,store):
all_button.connect("clicked",self.removeall_clicked,store)
all_button.set_tooltip_text(_('Remove all commands'))
all_button.show()

# create a combobox to store user choice
self.combo = self.get_combobox()
toolcombo = Gtk.ToolItem()
toolcombo.add(self.combo)
toolcombo.show()
toolbar.insert(toolcombo,4)


# create a button for the "Help" action
help_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_HELP)
help_button.set_label(_("Help"))
help_button.set_is_important(True)
toolbar.insert(help_button,5)
toolbar.insert(help_button,4)
help_button.connect("clicked",self.help_clicked )
help_button.set_tooltip_text(_("Display help message"))
help_button.show()

# add a separator
separator = Gtk.ToolItem()
separator.set_expand(True)
toolbar.insert(separator,5)

# create a combobox to store user choice
self.combo = self.get_combobox()
toolcombo = Gtk.ToolItem()
toolcombo.add(self.combo)
toolcombo.show()
toolbar.insert(toolcombo,6)

# return the complete toolbar
return toolbar
Expand Down
123 changes: 94 additions & 29 deletions google2ubuntu.py
Expand Up @@ -2,19 +2,76 @@
# -*- coding: utf-8 -*-
from subprocess import *
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Notify
from os.path import expanduser
import sys
import subprocess
import os
import json
import urllib2
import urllib
import unicodedata
import time
import gettext
import locale
import re

PID = os.getpid()
lang = locale.getlocale()[0]
gettext.install('google2ubuntu',os.path.dirname(os.path.abspath(__file__))+'/i18n/')

class tts():
def __init__(self,text):
text = unicodedata.normalize('NFKD', unicode(text,"utf-8")).encode('ASCII', 'ignore')
text = text.replace('\n','')
text_list = re.split('(\,|\.)', text)
combined_text = []
output=open('/tmp/tts.mp3',"w")
lc = lang.split('_')[0]

for idx, val in enumerate(text_list):
if idx % 2 == 0:
combined_text.append(val)
else:
joined_text = ''.join((combined_text.pop(),val))
if len(joined_text) < 100:
combined_text.append(joined_text)
else:
subparts = re.split('( )', joined_text)
temp_string = ""
temp_array = []
for part in subparts:
temp_string = temp_string + part
if len(temp_string) > 80:
temp_array.append(temp_string)
temp_string = ""
#append final part
temp_array.append(temp_string)
combined_text.extend(temp_array)
#download chunks and write them to the output file
for idx, val in enumerate(combined_text):
mp3url = "http://translate.google.com/translate_tts?tl=%s&q=%s&total=%s&idx=%s" % (lc, urllib.quote(val), len(combined_text), idx)
headers = {"Host":"translate.google.com",
"Referer":"http://www.gstatic.com/translate/sound_player2.swf",
"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.163 Safari/535.19"}
req = urllib2.Request(mp3url, '', headers)
sys.stdout.write('.')
sys.stdout.flush()
if len(val) > 0:
try:
response = urllib2.urlopen(req)
output.write(response.read())
time.sleep(.5)
except urllib2.HTTPError as e:
print ('%s' % e)
output.close()


os.system("play /tmp/tts.mp3 &")




# Cette classe utilis Notify pour alerter l'utilisateur
class notification():
Expand Down Expand Up @@ -52,23 +109,27 @@ def close(self):
class interface():
def __init__(self):
# on joue un son pour signaler le démarrage
os.system('aplay '+os.path.dirname(os.path.abspath(__file__))+'/sound.wav')
os.system('aplay '+os.path.dirname(os.path.abspath(__file__))+'/sound.wav &')
notif.update(_('Recording')+':',_('Processing'),'RECORD')

# On lance le script d'enregistrement pour acquérir la voix pdt 5s
command =os.path.dirname(os.path.abspath(__file__))+'/record.sh'
p = subprocess.check_call([command])

command =os.path.dirname(os.path.abspath(__file__))+'/record.sh ' + str(PID)
#p = subprocess.check_call([command])
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output,error = p.communicate()
notif.update(_("End of recording"),_('Sending to Google'),'NETWORK')
self.sendto()

def sendto(self):
# lecture du fichier audio
filename='/tmp/voix.flac'
filename='/tmp/voix_'+str(PID)+'.flac'
f = open(filename)
data = f.read()
f.close()

# suppression du fichier audio
if os.path.isfile('/tmp/voix_'+str(PID)+'.flac'):
os.system('rm /tmp/voix_'+str(PID)+'.flac')

# envoi d'une requête à Google
req = urllib2.Request('https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang='+lang, data=data, headers={'Content-type': 'audio/x-flac; rate=16000'})
try:
Expand Down Expand Up @@ -98,22 +159,26 @@ def sendto(self):
sp = stringParser(text,config_file)
else:
notif.update(_('Error'),_("I don't understand what you are saying"),'ERROR')
tts(_('Error')+' '+_("I don't understand what you are saying"))
time.sleep(3)
notif.close()
sys.exit(1)

except ValueError, IndexError:
notif.update(_('Error'),_('Unable to translate'),'ERROR')
tts(_('Error')+' '+_('Unable to translate'))
time.sleep(3)
notif.close()
sys.exit(1)

except urllib2.URLError:
notif.update(_('Error'),_('Unable to send to Google'),'ERROR')
notif.update(_('Error'),_('Unable to send to Google'),'ERROR')
tts(_('Error')+' '+_('Unable to send to Google'))
time.sleep(3)
notif.close()
sys.exit(1)


# Permet d'exécuter la commande associée à un mot prononcé
class stringParser():
def __init__(self,text,file):
Expand Down Expand Up @@ -168,13 +233,15 @@ def __init__(self,text,file):
b = basicCommands(check[1])
else:
# on exécute directement l'action
tts(_('Processing'))
os.system(do)

time.sleep(1)
notif.close()

except IOError:
notif.update(_('Error'),_('Setup file missing'),'ERROR')
tts(_('Error')+' '+_('Setup file missing'))
time.sleep(3)
notif.close()
sys.exit(1)
Expand Down Expand Up @@ -234,21 +301,25 @@ def __init__(self,module_path,module_name,text):
print text
if text.count(linker) > 0:
param =(text.split(linker)[1]).encode("utf-8")

# on regarde si l'utilisateur veut transformer les ' ' en +
if plus == 1:
param=param.replace(' ','+')

# commande qui sera exécutée

tts (_('Search results for')+' '+param)
execute = expanduser('~') +'/.config/google2ubuntu/modules/'+module_path+'/'+module_name+' '+param
os.system(execute)
else:
notif.update(_('Error'),_("you didn't say the linking word")+'\n'+linker,'ERROR')
tts(_('Error')+' '+_("you didn't say the linking word"))
time.sleep(3)
notif.close()

except IOError:
notif.update(_('Error'),_('args file missing'),'ERROR')
tts(_('Error')+' '+-('args file missing'))
time.sleep(3)
notif.close()
sys.exit(1)
Expand All @@ -261,29 +332,22 @@ def __init__(self,text):
self.getTime()
elif text == _('power'):
self.getPower()
elif text == _('clipboard'):
self.read_clipboard()
else:
print "no action found"

# en cours ...
#def repeat(self, text):
#path = '/tmp/previoux_command.txt'
#if os.path.isfile(path):
#try:
#f = open(path,"r")
#command_type = f.readline().rstrip('\n\r')
#if command_type == _('module'):
#name = f.readline().rstrip('\n\r')
#lword = f.readline().rstrip('\n\r')
#if lword in text:


#command_args =
#f.close()


#except IOError:
#print _('An error occured when I try to load previous commande')

def read_clipboard(self):
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_PRIMARY)

text = clipboard.wait_for_text()
if text != None:
text=text.replace("'",' ')
print text
tts(text)
else:
tts(_('Nothing in the clipboard'))

def getTime(self):
var=time.strftime('%d/%m/%y %H:%M',time.localtime())
notif.update(_('time'),var,'INFO')
Expand All @@ -298,13 +362,14 @@ def getPower(self):
rtime = output.split(' ')[4]

if output.count('Charging') > 0:
message = _('Charging')+': '+pcent+'\n'+rtime++' '+_('before charging')
message = _('Charging')+': '+pcent+'\n'+rtime+' '+_('before charging')
else:
message = _('Discharging')+': '+pcent+'\n'+rtime+' '+_('remaining')
else:
message = _('battery is not plugged')

notif.update(_('Power'),message,'INFO')
tts(message)
time.sleep(3)

# Initialisation des notifications
Expand Down

0 comments on commit aa7202c

Please sign in to comment.