This commit is contained in:
Death916 2025-01-16 01:59:15 -08:00
parent 08bedb40eb
commit 79e88b640f
4 changed files with 67 additions and 21 deletions

28
deathclock/alarm.py Normal file
View file

@ -0,0 +1,28 @@
#alarm.py
# alarm component for dash app
from dash import html, dcc
import datetime
import time
class Alarm:
def __init__(self):
self.current_time = datetime.datetime.now()
self.alarms = []
self.alarm_times = []
def add_alarm(self, alarm_time,current_time):
self.alarms.append(alarm_time)
self.alarm_times.append(alarm_time.time())
self.current_time = current_time
print(f"Alarm set for {alarm_time}")
def check_alarm(self):
current_time = datetime.datetime.now()
if current_time.time() in self.alarm_times:
print("Alarm triggered!")
return True
return False

View file

@ -5,6 +5,7 @@ from time import strftime, localtime
from weather import Weather
from news import News
from scores import NBAScores
import alarm
app = Dash(__name__)
# Initialize classes
@ -30,10 +31,10 @@ app.layout = html.Div([
]),
html.Div(id='news-ticker', className='ticker'),
# Intervals
dcc.Interval(id='clock-interval', interval=1000, n_intervals=0),
dcc.Interval(id='clock-interval', interval=60000, n_intervals=0),
dcc.Interval(id='weather-interval', interval=300000, n_intervals=0),
dcc.Interval(id='news-interval', interval=300000, n_intervals=0),
dcc.Interval(id='nba-interval', interval=60000, n_intervals=0)
dcc.Interval(id='nba-interval', interval=300000, n_intervals=0)
])
@ -61,22 +62,25 @@ def update_weather(n):
])
except Exception as e:
return html.Div(f"Weather update error: {str(e)}")
@app.callback(
Output('news-ticker', 'children'),
Input('news-interval', 'n_intervals')
)
def update_news(n):
global _last_news_update, _cached_news, _initial_run
current_time = datetime.datetime.now()
# On first run or if 5 minutes have elapsed since last update
if _initial_run or (current_time - _last_news_update).total_seconds() >= 300:
print("Fetching fresh news...")
try:
try:
if _initial_run or (current_time - _last_news_update).total_seconds() >= 500:
print("Fetching fresh news due to timer...")
headlines_dict = news_obj.get_news()
if not isinstance(headlines_dict, dict):
return html.Div("News update error: Invalid data format")
raise ValueError("News update error: Invalid data format")
if not headlines_dict:
return html.Div("No news fetched")
raise ValueError("No news fetched")
combined_text = " | ".join(headlines_dict.keys())
text_px = len(combined_text) * 8 # Approx 8px per character
@ -89,11 +93,7 @@ def update_news(n):
ticker_style = {"animationDuration": f"{duration}s"}
# Ensure all news items are concatenated into a single line
combined_items = " | ".join([
f"{headline}"
for headline in headlines_dict.keys()
])
combined_items = " | ".join([f"{headline}" for headline in headlines_dict.keys()])
_cached_news = html.Div(
html.Span(combined_items, className="news-item", style=ticker_style),
@ -102,19 +102,30 @@ def update_news(n):
_last_news_update = current_time
_initial_run = False
return _cached_news
except Exception as e:
print(f"News update error: {str(e)}")
# Fallback to cached news or load from local file
if _cached_news:
print("Using cached news...")
else:
print("Loading news from news.txt...")
try:
with open("news.txt", "r") as file:
local_news = file.read().strip()
if local_news:
_cached_news = html.Div(
html.Span(local_news, className="news-item", style={"animationDuration": "20"}),
className='ticker'
)
except FileNotFoundError:
print("news.txt not found.")
_cached_news = html.Div("No news available.")
except Exception as e:
return html.Div(f"News feed error: {str(e)}")
print("Returning cached news...")
return _cached_news
#get the scores from the scores API
@app.callback(
Output('nba-scores-display', 'children'),
@ -140,6 +151,9 @@ def update_scores(n):
except Exception as e:
return html.Div("Scores unavailable")
#print time and date
def print_time():
print(strftime("%B %d, %I:%M %p", localtime()))
if __name__ == '__main__':
app.run_server(debug=True, host='0.0.0.0', port=8050)

View file

@ -1,4 +1,7 @@
import feedparser
from time import localtime, strftime
def print_time():
print(strftime("%B %d, %I:%M %p", localtime()))
class News:
def __init__(self):
@ -6,6 +9,7 @@ class News:
self._news_dict_length = 0
def get_news(self):
print_time()
feeds = []
self._news_dict = {} # Reset dict each time