#!/usr/env/python3
import speech_recognition as sr
import os

# This script was written by Greg Colburn (ThePony on github). Feel free
# to contact me on FB at https://www.facebook.com/9millionthGreg BUT...
# when contacting me please be sure to tell my WHY you are messaging me!
# donations to keep snacks on hand for my coding spurts are welcome at
# paypal at (colburn greg at.symbol.goes.here ya hoo dot com), thanks

r = sr.Recognizer()
m = sr.Microphone()

# anciennes variables
corona      = ['Corona', 'corona']

domotique_fr= ['Boutique', 'dermatique', 'the Boutique', 'the matic']

exitWord    = ['quit', 'exit']

game        = ['game']

internet    = ['internet']
internet_fr = ["actor on it", "Adele net", "auntie on it", "bayonet", "ethanate", "hotel net", "I founded", "I found it", "I'll be on it", "Infinite", "nothing on it", "Telnet"]

lumiere_fr = ['leave me alone', 'Lumia', 'meet me on', 'read me out', 'Romeo', 'Romania', 'rythmia', 'wake me up']

meteo_fr    = ['meteor', 'meteo', 'midday', 'video']
musique_fr  = ['music']

television_en = ['television']

# nouvelle variable unique a utiliser par la suite
# action 'browser' necessite 'site'
cle = {
    'corona': {
        'action' : 'browser' ,
        'site'   : 'http://gangand.net',
        'mots'   : ['corona']
        },
    
    'domotique_fr' : { 
        'action' : 'gpio_domotique' ,
        'mots'   : ['Boutique', 'dermatique', 'the Boutique', 'the matic']
        },

    'exitWord': {
        'action' : 'main_pgm' ,
        'mots'   : ['quit', 'exit']
        },

    'game': {
        'action' : 'exec' ,
        'command': 'pysolfc',
        'mots'   : ['game']
        },
    
    'internet_en': {
        'action' : 'browser' ,
        'site'   : 'http://gangand.net',
        'mots'   : ['internet']
        },
        
    'internet_fr': {
        'action' : 'browser' ,
        'site'   : 'http://gangand.net',
        'mots'   : ['actor on it', 'Adele net', 'auntie on it',
            'bayonet', 'ethanate', 'hotel net', 'I founded',
            'I found it', 'I\'ll be on it', 'Infinite',
            'nothing on it', 'Telnet']
        },

    'libreoffice_en': {
        'action' : 'exec' ,
        'command': 'libreoffice',
        'mots'   : ['libreoffice']
        },

    'lumiere_fr': {
        'action' : 'gpio_domotique' ,
        'mots'   : ['leave me alone', 'Lumia', 'meet me on',
            'read me out', 'Romeo', 'Romania', 'rythmia',
            'wake me up']
        },

    'meteo_fr': {
        'action' : 'browser' ,
        'site'   : 'https://www.google.com/search?q=meteo+reims',
        'mots'   : ['meteor', 'meteo', 'midday', 'video']
        },

    'musique_fr' : {
        'action' : 'exec' ,
        'command': 'vlc ',
        'args'   : ['/media/fbx/xavbox/type1/', '>/dev/null', '2>&1'],
        'mots'   : ['music']
        },

    'television_en' : {
        'action' : 'exec' ,
        'command': 'vlc ',
        'args'   : ['http://gangand.net/tt/tv/free_sd.m3u'],
        'mots'   : ['Billy', 'CD', 'DD', 'Siri', 'television']
        },

}



import subprocess

try:
    print("One moment...")
    with m as source: r.adjust_for_ambient_noise(source)
    # The above line takes an ambient sample of noise to set threshhold levels.
    # This may not work on all microphones and should be tweaked as needed
    while True:
        print("Now Ready, talk to me. (Press Ctrl+c to or say exit or quit to quit)")
        with m as source: audio = r.listen(source)
        print(".")
        try:
            value = r.recognize_google(audio)

            if str is bytes:
                reply = "{}".format(value).encode("utf-8")
            else:
                reply = "{}".format(value)
                
            if reply in exitWord:
                quit()
            
            elif reply in game:             # game anglais
                print("game")
                subprocess.Popen( ['pysolfc', '']).pid
            
            elif reply in internet_fr:      # internet fr
                print("internet_fr %s" % reply)
                subprocess.Popen( ['chromium-browser', 'http://gangand.net']).pid                
            elif reply in internet:         # internet anglais
                print("internet %s" % reply)
                subprocess.Popen( ['firefox-esr', 'http://gangand.net']).pid

            elif reply in internet:         # lumiere fr
                print("lumiere_fr %s" % reply)
                subprocess.Popen( ['chromium-browser', 'https://www.google.com/search?q=domotique+lumiere']).pid

            elif reply in meteo_fr:         # meteo fr
                print("meteo_fr %s" % reply)
                subprocess.Popen( ['chromium-browser', 'https://www.google.com/search?q=meteo+reims']).pid
            
            elif reply in musique_fr:         # music fr
                print("musique_fr %s" % reply)
                print("Utiliser exit pour quitter l'assistant vocal sans killer vlc (Ctrl-C le kille)")
                subprocess.Popen( ['vlc', '/media/fbx/xavbox/type1/', '>/dev/null', '2>&1']).pid
            
            # ~ elif reply in television_en:         # television en
                # ~ print("television_en %s" % reply)
                # ~ subprocess.Popen( ['sh /home/pi/bin/1.sh', 'tv']).pid
            
            elif reply in corona:
                print("corona")
                subprocess.Popen( ['firefox-esr', 'http://gangand.net/cc/coro/stats/index.php?device=PC']).pid
            else:
                print("You said: %s" % reply)
                os.system("flite -o /tmp/flite.wav -t %s " % reply)
        except sr.UnknownValueError:
            print("The Google API could not understand the audio...")
        except sr.RequestError as e:
            print("!")
            #print("Uh oh! Couldn't request results from Google Speech Recognition service; {0}".format(e))

except KeyboardInterrupt:
    pass
