#!/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()

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']


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( ['firefox-esr', '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)
                subprocess.Popen( ['vlc', '/media/fbx/xavbox/type1/']).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 -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
