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 weather import Weather
from news import News from news import News
from scores import NBAScores from scores import NBAScores
import alarm
app = Dash(__name__) app = Dash(__name__)
# Initialize classes # Initialize classes
@ -30,10 +31,10 @@ app.layout = html.Div([
]), ]),
html.Div(id='news-ticker', className='ticker'), html.Div(id='news-ticker', className='ticker'),
# Intervals # 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='weather-interval', interval=300000, n_intervals=0),
dcc.Interval(id='news-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: except Exception as e:
return html.Div(f"Weather update error: {str(e)}") return html.Div(f"Weather update error: {str(e)}")
@app.callback( @app.callback(
Output('news-ticker', 'children'), Output('news-ticker', 'children'),
Input('news-interval', 'n_intervals') Input('news-interval', 'n_intervals')
) )
def update_news(n): def update_news(n):
global _last_news_update, _cached_news, _initial_run global _last_news_update, _cached_news, _initial_run
current_time = datetime.datetime.now() 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: try:
print("Fetching fresh news...") if _initial_run or (current_time - _last_news_update).total_seconds() >= 500:
try: print("Fetching fresh news due to timer...")
headlines_dict = news_obj.get_news() headlines_dict = news_obj.get_news()
if not isinstance(headlines_dict, dict): 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: if not headlines_dict:
return html.Div("No news fetched") raise ValueError("No news fetched")
combined_text = " | ".join(headlines_dict.keys()) combined_text = " | ".join(headlines_dict.keys())
text_px = len(combined_text) * 8 # Approx 8px per character text_px = len(combined_text) * 8 # Approx 8px per character
@ -89,11 +93,7 @@ def update_news(n):
ticker_style = {"animationDuration": f"{duration}s"} 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( _cached_news = html.Div(
html.Span(combined_items, className="news-item", style=ticker_style), html.Span(combined_items, className="news-item", style=ticker_style),
@ -102,19 +102,30 @@ def update_news(n):
_last_news_update = current_time _last_news_update = current_time
_initial_run = False _initial_run = False
return _cached_news 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 return _cached_news
#get the scores from the scores API #get the scores from the scores API
@app.callback( @app.callback(
Output('nba-scores-display', 'children'), Output('nba-scores-display', 'children'),
@ -140,6 +151,9 @@ def update_scores(n):
except Exception as e: except Exception as e:
return html.Div("Scores unavailable") return html.Div("Scores unavailable")
#print time and date
def print_time():
print(strftime("%B %d, %I:%M %p", localtime()))
if __name__ == '__main__': if __name__ == '__main__':
app.run_server(debug=True, host='0.0.0.0', port=8050) app.run_server(debug=True, host='0.0.0.0', port=8050)

View file

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