Add weather api and daemonize clock
This commit is contained in:
parent
a81ce5bcbe
commit
cb9e38d97b
@ -18,7 +18,6 @@
|
|||||||
pyright
|
pyright
|
||||||
black
|
black
|
||||||
];
|
];
|
||||||
OPENAI_API_BASE="https://ai.trkt.in";
|
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
source .env.sh
|
source .env.sh
|
||||||
exec ${shell}
|
exec ${shell}
|
||||||
|
BIN
src/__pycache__/clock.cpython-311.pyc
Normal file
BIN
src/__pycache__/clock.cpython-311.pyc
Normal file
Binary file not shown.
BIN
src/__pycache__/weather.cpython-311.pyc
Normal file
BIN
src/__pycache__/weather.cpython-311.pyc
Normal file
Binary file not shown.
87
src/clock.py
Normal file
87
src/clock.py
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
from time import sleep
|
||||||
|
import datetime as dt
|
||||||
|
from datetime import datetime
|
||||||
|
from openai import OpenAI
|
||||||
|
import threading
|
||||||
|
|
||||||
|
number_map = {
|
||||||
|
"0": "o'clock",
|
||||||
|
"1": "one",
|
||||||
|
"2": "two",
|
||||||
|
"3": "three",
|
||||||
|
"4": "four",
|
||||||
|
"5": "six",
|
||||||
|
"6": "six",
|
||||||
|
"7": "seven",
|
||||||
|
"8": "eight",
|
||||||
|
"9": "nine",
|
||||||
|
"10": "ten",
|
||||||
|
"11": "eleven",
|
||||||
|
"12": "twelve",
|
||||||
|
"13": "thirteen",
|
||||||
|
"14": "fourteen",
|
||||||
|
"15": "fifteen",
|
||||||
|
"16": "sixteen",
|
||||||
|
"17": "seventeen",
|
||||||
|
"18": "eighteen",
|
||||||
|
"19": "nineteen",
|
||||||
|
"20": "twenty",
|
||||||
|
"30": "thirty",
|
||||||
|
"40": "forty",
|
||||||
|
"50": "fifty",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Clock:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def serialize_time_part(self, part):
|
||||||
|
part_string = ""
|
||||||
|
if part in number_map:
|
||||||
|
part_string += number_map[part]
|
||||||
|
else:
|
||||||
|
tens_str = number_map[f"{part[0]}0"]
|
||||||
|
ones_str = number_map[part[1]]
|
||||||
|
part_string += f"{tens_str}-{ones_str}"
|
||||||
|
return part_string
|
||||||
|
|
||||||
|
def serialize_time(self, time):
|
||||||
|
hour = str(time.hour)
|
||||||
|
if time.hour > 12:
|
||||||
|
hour = str(time.hour - 12)
|
||||||
|
hour = self.serialize_time_part(hour)
|
||||||
|
minute = str(time.minute)
|
||||||
|
if time.minute < 10:
|
||||||
|
minute = f"o'{self.serialize_time_part(minute)}"
|
||||||
|
else:
|
||||||
|
minute = self.serialize_time_part(minute)
|
||||||
|
time_string = f"{hour} {minute}"
|
||||||
|
return time_string
|
||||||
|
|
||||||
|
def generate_rhyme(self, time):
|
||||||
|
client = OpenAI()
|
||||||
|
response = client.chat.completions.create(
|
||||||
|
model="gpt-4",
|
||||||
|
messages=[
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": f"Finish this sentence about the time with a rhyme. It's {time}, ",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
)
|
||||||
|
rhyme = response.choices[0].message.content
|
||||||
|
return f"It's {time}, {rhyme}"
|
||||||
|
|
||||||
|
def start_update_loop(self):
|
||||||
|
thread = threading.Thread(target=self.update_loop)
|
||||||
|
thread.daemon = True
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
def update_loop(self):
|
||||||
|
while True:
|
||||||
|
now = datetime.now()
|
||||||
|
then = datetime(now.year, now.month, now.day, now.hour, now.minute + 1, 0)
|
||||||
|
seconds_until = (then - now).total_seconds()
|
||||||
|
print(seconds_until)
|
||||||
|
sleep(1)
|
29
src/main.py
29
src/main.py
@ -1,10 +1,11 @@
|
|||||||
import os
|
from weather import Weather
|
||||||
from pyowm.owm import OWM
|
from time import sleep
|
||||||
|
from clock import Clock
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
number_map = {
|
number_map = {
|
||||||
"0": "zero",
|
"0": "o'clock",
|
||||||
"1": "one",
|
"1": "one",
|
||||||
"2": "two",
|
"2": "two",
|
||||||
"3": "three",
|
"3": "three",
|
||||||
@ -68,23 +69,19 @@ def generate_rhyme(time):
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
rhyme = response.choices[0].message.content
|
rhyme = response.choices[0].message.content
|
||||||
return f"It's {time} {rhyme}"
|
return f"It's {time}, {rhyme}"
|
||||||
|
|
||||||
|
|
||||||
def get_weather():
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
use_time = datetime.now()
|
# weather_manager = Weather()
|
||||||
serialized_time = serialize_time(use_time)
|
# weather_manager.start_update_loop()
|
||||||
print(serialized_time)
|
# use_time = datetime.now()
|
||||||
sentence = generate_rhyme(serialized_time)
|
# serialized_time = serialize_time(use_time)
|
||||||
weather = get_weather()
|
# sentence = generate_rhyme(serialized_time)
|
||||||
|
|
||||||
print(sentence)
|
|
||||||
print(weather)
|
|
||||||
|
|
||||||
|
clock_manager = Clock()
|
||||||
|
clock_manager.start_update_loop()
|
||||||
|
sleep(1000)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
28
src/weather.py
Normal file
28
src/weather.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
from time import sleep
|
||||||
|
import requests
|
||||||
|
import os
|
||||||
|
import threading
|
||||||
|
|
||||||
|
|
||||||
|
class Weather:
|
||||||
|
def __init__(self):
|
||||||
|
self.weather = self.get_weather()
|
||||||
|
|
||||||
|
def get_weather(self):
|
||||||
|
api_key = os.environ["WEATHER_API_KEY"]
|
||||||
|
location = os.environ["WEATHER_LOCATION"]
|
||||||
|
units = "imperial" # TODO: Make environment variable
|
||||||
|
url = f"https://api.tomorrow.io/v4/weather/realtime?location={location}&units={units}&apikey={api_key}"
|
||||||
|
headers = {"accept": "application/json"}
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
def start_update_loop(self):
|
||||||
|
thread = threading.Thread(target=self.update_loop)
|
||||||
|
thread.daemon = True
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
def update_loop(self):
|
||||||
|
while True:
|
||||||
|
sleep(1200)
|
||||||
|
self.weather = self.get_weather()
|
Loading…
Reference in New Issue
Block a user