Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- import datetime
- import pickle
- import os.path
- # from googleapiclient.discovery import build
- # from google_auth_oauthlib.flow import InstalledAppFlow
- # from google.auth.transport.requests import Request
- import os
- import time
- import pyttsx3
- import speech_recognition as sr
- import pytz
- import requests as rq
- import subprocess
- from newsapi import NewsApiClient
- IOT_DEVICE_IP = "172.16.6.205"
- def speak(text):
- engine = pyttsx3.init()
- engine.say(text)
- engine.runAndWait()
- def get_audio():
- r = sr.Recognizer()
- with sr.Microphone() as source:
- audio = r.listen(source)
- said = ""
- try:
- said = r.recognize_google(audio, language="fr-FR")
- print(said)
- except Exception as e:
- print("Exception: " + str(e))
- return said.lower()
- def note(text):
- date = datetime.datetime.now()
- file_name = str(date).replace(":", "-") + "-note.txt"
- with open(file_name, "w") as f:
- f.write(text)
- subprocess.Popen(["notepad.exe", file_name])
- WAKE = "bonjour alexa"
- print("Start")
- while True:
- print("Listening")
- text = get_audio()
- if text.count(WAKE) > 0:
- speak("Je vous ecoute")
- text = get_audio()
- print(text)
- NOTE_STRS = ["prends une note", "note ceci", "note ça"]
- for phrase in NOTE_STRS:
- if phrase in text:
- speak("Qu'est-ce que je dois noter?")
- note_text = get_audio()
- note(note_text)
- speak("C'est noté.")
- IOT_STRS_CMD_LED_ON = ["allume la lumière"]
- for phrase in IOT_STRS_CMD_LED_ON:
- if phrase in text:
- r = rq.get(f'http://{IOT_DEVICE_IP}/ON')
- # TODO check http 200
- print(r.status_code)
- speak("La lumière a été allumé")
- IOT_STRS_CMD_LED_OFF = ["éteint la lumière","éteins la lumière"]
- for phrase in IOT_STRS_CMD_LED_OFF:
- if phrase in text:
- r = rq.get(f'http://{IOT_DEVICE_IP}/OFF')
- # TODO check http 200
- print(r.status_code)
- speak("J'ai eteins la lumière")
- IOT_STRS_CMD_WEATHER = ["météo" , "quel temps fait-t'il"]
- for phrase in IOT_STRS_CMD_WEATHER:
- if phrase in text :
- req = rq.get("http://api.openweathermap.org/data/2.5/weather?q=Paris,fr&appid=d937422704600fb3bd946890edcede5d&lang=fr&units=metric")
- data = req.json()
- print(data)
- temperature = int(data['main']['temp'])
- if (temperature < 10):
- speak("Couvrez vous bien")
- speak(f"Il fait {temperature} degré à Paris")
- IOT_STRS_CMD_NEWS = ["actualités" , "quelles sont les actualités"]
- for phrase in IOT_STRS_CMD_NEWS:
- if phrase in text:
- newsapi = NewsApiClient(api_key='c385548c54964a6d8ccab8d8c4d7b058')
- top_headlines = newsapi.get_top_headlines(
- category='general',
- language='fr',
- country='fr')
- speak(top_headlines)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement