Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. from __future__ import print_function
  2. import datetime
  3. import pickle
  4. import os.path
  5. # from googleapiclient.discovery import build
  6. # from google_auth_oauthlib.flow import InstalledAppFlow
  7. # from google.auth.transport.requests import Request
  8. import os
  9. import time
  10. import pyttsx3
  11. import speech_recognition as sr
  12. import pytz
  13. import requests as rq
  14. import subprocess
  15. from newsapi import NewsApiClient
  16.  
  17. IOT_DEVICE_IP = "172.16.6.205"
  18.  
  19.  
  20. def speak(text):
  21. engine = pyttsx3.init()
  22. engine.say(text)
  23. engine.runAndWait()
  24.  
  25.  
  26. def get_audio():
  27. r = sr.Recognizer()
  28. with sr.Microphone() as source:
  29. audio = r.listen(source)
  30. said = ""
  31.  
  32. try:
  33. said = r.recognize_google(audio, language="fr-FR")
  34. print(said)
  35. except Exception as e:
  36. print("Exception: " + str(e))
  37.  
  38. return said.lower()
  39.  
  40. def note(text):
  41. date = datetime.datetime.now()
  42. file_name = str(date).replace(":", "-") + "-note.txt"
  43. with open(file_name, "w") as f:
  44. f.write(text)
  45.  
  46. subprocess.Popen(["notepad.exe", file_name])
  47.  
  48.  
  49. WAKE = "bonjour alexa"
  50.  
  51. print("Start")
  52.  
  53. while True:
  54. print("Listening")
  55. text = get_audio()
  56.  
  57. if text.count(WAKE) > 0:
  58. speak("Je vous ecoute")
  59. text = get_audio()
  60. print(text)
  61.  
  62. NOTE_STRS = ["prends une note", "note ceci", "note ça"]
  63. for phrase in NOTE_STRS:
  64. if phrase in text:
  65. speak("Qu'est-ce que je dois noter?")
  66. note_text = get_audio()
  67. note(note_text)
  68. speak("C'est noté.")
  69.  
  70. IOT_STRS_CMD_LED_ON = ["allume la lumière"]
  71. for phrase in IOT_STRS_CMD_LED_ON:
  72. if phrase in text:
  73. r = rq.get(f'http://{IOT_DEVICE_IP}/ON')
  74. # TODO check http 200
  75. print(r.status_code)
  76. speak("La lumière a été allumé")
  77.  
  78.  
  79.  
  80. IOT_STRS_CMD_LED_OFF = ["éteint la lumière","éteins la lumière"]
  81. for phrase in IOT_STRS_CMD_LED_OFF:
  82. if phrase in text:
  83. r = rq.get(f'http://{IOT_DEVICE_IP}/OFF')
  84. # TODO check http 200
  85. print(r.status_code)
  86. speak("J'ai eteins la lumière")
  87.  
  88.  
  89. IOT_STRS_CMD_WEATHER = ["météo" , "quel temps fait-t'il"]
  90. for phrase in IOT_STRS_CMD_WEATHER:
  91. if phrase in text :
  92. req = rq.get("http://api.openweathermap.org/data/2.5/weather?q=Paris,fr&appid=d937422704600fb3bd946890edcede5d&lang=fr&units=metric")
  93. data = req.json()
  94. print(data)
  95. temperature = int(data['main']['temp'])
  96. if (temperature < 10):
  97. speak("Couvrez vous bien")
  98. speak(f"Il fait {temperature} degré à Paris")
  99. IOT_STRS_CMD_NEWS = ["actualités" , "quelles sont les actualités"]
  100. for phrase in IOT_STRS_CMD_NEWS:
  101. if phrase in text:
  102. newsapi = NewsApiClient(api_key='c385548c54964a6d8ccab8d8c4d7b058')
  103. top_headlines = newsapi.get_top_headlines(
  104. category='general',
  105. language='fr',
  106. country='fr')
  107. speak(top_headlines)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement