diff options
Diffstat (limited to 'events.py')
| -rw-r--r-- | events.py | 49 | 
1 files changed, 38 insertions, 11 deletions
@@ -1,9 +1,13 @@  import subprocess  from datetime import datetime +from config import config +import requests  class Event(): -    def __init__(self, dial_manager): +    def __init__(self, dial_manager, phone_held, phone_hung_up):          self.dial_manager = dial_manager +        self.phone_held = phone_held +        self.phone_hung_up = phone_hung_up      def get_name(self):          return "generic event" @@ -17,9 +21,6 @@ class Event():          subprocess.run(["espeak", text])  class FortuneEvent(Event): -    def __init__(self, dial_manager): -        super().__init__(dial_manager) -      def get_name(self):          return "fortune" @@ -28,9 +29,6 @@ class FortuneEvent(Event):          return res.stdout.decode("utf-8")  class DirectoryEvent(Event): -    def __init__(self, dial_manager): -        super().__init__(dial_manager) -      def get_name(self):          return "directory" @@ -47,14 +45,11 @@ class DirectoryEvent(Event):          sequences = self.dial_manager.sequences          text = ""          for sequence, event in recursive_build("", sequences): -            text += f"Dial {sequence} for {event.get_name()}. " +            text += f"For {event.get_name()} dial {sequence}. "          return text  class OperatorEvent(Event): -    def __init__(self, dial_manager): -        super().__init__(dial_manager) -      def get_name(self):          return "operator" @@ -62,3 +57,35 @@ class OperatorEvent(Event):          now = datetime.now()          return f"Right now, it is {now.hour} {now.minute}. Dial 4 1 1 for directory." + +class WeatherEvent(Event): +    def get_name(self): +        return "weather" + +    def get_text(self): +        key = config["openweathermap"]["key"] +        lat = config["openweathermap"]["lat"] +        lon = config["openweathermap"]["lon"] +        location = config["openweathermap"]["location"] +        units = config["openweathermap"]["units"] +        onecall_url = f"https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&appid={key}&units={units}" +        onecall = requests.get(onecall_url).json() +        current_temp = onecall["current"]["temp"] +        current_desc = onecall["current"]["weather"][0]["description"] +        weather = f"In {location}, it is {current_temp} degrees with {current_desc}. " +        high_temp = onecall["daily"][0]["temp"]["max"] +        pop = onecall["daily"][0]["pop"] * 100 +        forecast = f"Today there is a high of {high_temp} with a {pop} percent chance of percipitation." +        return f"{weather}{forecast}" + + +class TimerEvent(Event): +    def get_name(self): +        return "timer" + +    def get_text(self): +        start = datetime.now() +        self.phone_hung_up.wait() +        end = datetime.now() +        print(end - start) +        return "time"  | 
