From 79e88b640f37bf687ce93f0d2ace91ed0dc193dc Mon Sep 17 00:00:00 2001 From: Death916 Date: Thu, 16 Jan 2025 01:59:15 -0800 Subject: [PATCH] trying --- deathclock/__pycache__/news.cpython-311.pyc | Bin 2302 -> 2690 bytes deathclock/alarm.py | 28 ++++++++++ deathclock/app.py | 56 ++++++++++++-------- deathclock/news.py | 4 ++ 4 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 deathclock/alarm.py diff --git a/deathclock/__pycache__/news.cpython-311.pyc b/deathclock/__pycache__/news.cpython-311.pyc index 3d7b861089296846d3d1f3b8035affd8888c4c53..75323522ed967ee1a243a5ed8078ef6e20681419 100644 GIT binary patch delta 1109 zcmew-*d)rgoR^o2fq{Wx#n$%p64r@)67_Zr3=Gp5QW#Pga~PsPG*b>^E>jd!E^`z! zBSQ*v3quqO6N5WL3QG$^3hOc^28Pv4AQM11iZz8Xm_d{6C5Ypv$#jb|CqFqcrzA5s z^%h5QNl_Yzl?;-EVRi-v24)5ZhR+dTYfBjGVKNL1B_IhXH-)i;70P5_U|7b$z_1$5 zMRmh6Mh1q}j4(+?h7^Wi22CcvDt=Wb1=SQC1yxTgRbK_w0!`*ytOZ4xc_l?G3=9lK zAiGvFd6ss~YFf=gSW#R1AxFD>uG;?A0lI)9IMpw9u zF0vS3VKKe{Lq!}63=9*a*2@=hGB7aQ;)svW%*!l^kFVnOOD!+fgK^m>$1=(@6>&_i zW0W*&V*-VI3Eb`I{$ylGXQ*Y&WT;_?hpQ=JoxG7TpBEI2P%me(Ox9(RWn`V~&7@e* z1{GvTVa*1IUlDf=Ll#_jG7}>M2nRFNuw=o_$O5??tg(hQixbRXU|^_WMG*%D30NwH zZ4L{vnaJwYY(N?rDj2g64q>n2Wnf5ViieAouuc|aR+JP6n+7J3ja$Go*^WhNaxSwv zBk$y?%&ClwlYcX3vlOv2FicKo31k$Sypu(fkz?{>mS7G(a6}dHO}1oJW8|Nlz$(Ki zH@TKoQceIA2drtSsVT*`7}JW>KwQR@TPy|n#U(|ulee(yif}V9FciOM1V=B2P$y3h zPY2KDH>^(?)m1>*gV&Lf`5+^UBP;ts4n{{-_QMj6TrT?TNAy`-jJc1PF}fH}{>-ka zEC@M400t4IQ@ar9weE9 zf*oQQss+C|Y;yBcN^?@}icA?87(g*xoW;Pv@PV0;k?{tD-~|}E!Ju-1AyVZ6h(aMh eFflTkd|-eRY$}ZM9~dx+k07xxAOcMm?05he3Cqy{ delta 756 zcmZn?{U^w`oR^o2fq{Wxc7J>N7nX^95+)i93=Gp5QW#Pga~Pr+m>Ap{QkYs8Qka)9 zF)*xV0;vVzD8>}VU%q7z3=9m# z;tUK74GcG=WI9-S_-`mGb+BCFkh=gzpFzeXGlEDc=3ro8U}j)o_?*QAa!)!#En_A_ z4MRLsnt`DNBn#!GFs3kp+&B3vQ!FnlRFHvzA&Yr(JhLn#%j8;SMMl=i^O;2z;gZQr zj0_+g%uvIU#Re13VxRnwSv;H_#;;+`;)JnN*yga*Fl52F3^lA-aCJ5e3=B046^vPM z+f&%9co`Uynd0FhCCrnZSrjEj;RY}uo3?;^av`hmvIJW&Gj9>wPOU7y#adpJ zS(2*BUIYroTdZlRsVT*`7}JVCzQ4toa*L%Pzqq7GV)8pSU2c$);%AJYsM)N~{*+Nk zo`Hd37Oye$W)^eyU5w`Jhq)ZNTol=lD6+Vyav#xPbWxpb&8evjN;XB>3=9lK{2+oG zM1X>-2o%>~f_ZW~r*fbitNaHBOri*^0i;%wsYni_8N^02>lcSjZhlH>PO4pzJ_7>- hD18(=PZr=3G5^4#%qahX0h9O$68i!o&}6|D0svT1jcxz{ diff --git a/deathclock/alarm.py b/deathclock/alarm.py new file mode 100644 index 0000000..f80b346 --- /dev/null +++ b/deathclock/alarm.py @@ -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 + diff --git a/deathclock/app.py b/deathclock/app.py index f69a264..5f407a0 100644 --- a/deathclock/app.py +++ b/deathclock/app.py @@ -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) diff --git a/deathclock/news.py b/deathclock/news.py index 1e98347..7d87f4f 100644 --- a/deathclock/news.py +++ b/deathclock/news.py @@ -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