fixing output

This commit is contained in:
Pat Teruel committed 2026-02-22 16:01:12 +08:00
1 parent 9e799049a7
commit 2698fe7114
4 files changed
+234 -62

No files matched your search

+68 -6
View File
@@ -13,6 +13,37 @@ app = Flask(__name__)
PHT = timezone(timedelta(hours=8))
def _sunday_midnight_pht_for(now_pht=None):
"""Return the Sunday midnight (start of day) in PHT used for week-start logic.
This function treats Sunday as belonging to the previous week (used for producing
week ranges like "Feb 15-21" when today is Sunday Feb 22).
"""
now_pht = now_pht or datetime.now(PHT)
wd = now_pht.weekday()
days_since_sunday = (wd + 1) % 7
if wd == 6:
days_since_sunday = 7
sunday_midnight_pht = (now_pht - timedelta(days=days_since_sunday)).replace(
hour=0, minute=0, second=0, microsecond=0
)
return sunday_midnight_pht
def _latest_sunday_midnight_pht(now_pht=None):
"""Return the latest Sunday midnight (start of day) in PHT including today if it's Sunday.
This is for computing the exact UTC cutoff timestamps for a given latest Sunday.
"""
now_pht = now_pht or datetime.now(PHT)
wd = now_pht.weekday()
days_since_sunday = (wd + 1) % 7 # Sunday -> 0
sunday_midnight_pht = (now_pht - timedelta(days=days_since_sunday)).replace(
hour=0, minute=0, second=0, microsecond=0
)
return sunday_midnight_pht
def current_week():
"""Return (week_start, week_end, cutoff_utc) for the current week in PHT.
@@ -20,17 +51,34 @@ def current_week():
week_end = that Sunday + 6 days (Saturday)
cutoff_utc = Sunday 00:00 PHT expressed in UTC (= Saturday 16:00 UTC)
"""
now_pht = datetime.now(PHT)
days_since_sunday = (now_pht.weekday() + 1) % 7
sunday_midnight_pht = (now_pht - timedelta(days=days_since_sunday)).replace(
hour=0, minute=0, second=0, microsecond=0
)
sunday_midnight_pht = _sunday_midnight_pht_for()
week_start = sunday_midnight_pht.date()
week_end = week_start + timedelta(days=6)
cutoff_utc = sunday_midnight_pht.astimezone(timezone.utc)
return week_start, week_end, cutoff_utc
def _iso_z(dt):
"""Format timezone-aware datetime to 'YYYY-MM-DDTHH:MM:SS.mmmZ' in UTC."""
s = dt.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")
return s[:-3] + "Z"
def get_latest_sunday():
"""Return latest Sunday start-of-day PHT expressed in UTC ISO Z format (inclusive)."""
sunday_midnight_pht = _latest_sunday_midnight_pht()
cutoff_utc = sunday_midnight_pht.astimezone(timezone.utc)
return _iso_z(cutoff_utc)
def get_previous_sunday():
"""Return the Sunday prior to the latest Sunday (UTC ISO Z)."""
sunday_midnight_pht = _latest_sunday_midnight_pht()
prev_midnight = sunday_midnight_pht - timedelta(weeks=1)
prev_cutoff_utc = prev_midnight.astimezone(timezone.utc)
return _iso_z(prev_cutoff_utc)
def format_date_range(week_start, week_end) -> str:
"""Format date range: 'Feb 8-14' (same month) or 'Mar 29 - Apr 4' (different)."""
if week_start.month == week_end.month:
@@ -48,12 +96,16 @@ def index():
"TOTAL_EPISODES": os.environ.get("TOTAL_EPISODES", "1155"),
"DB_PATH": os.environ.get("DB_PATH", "/app/data/episodes.db"),
}
latest = get_latest_sunday()
previous = get_previous_sunday()
return render_template(
"index.html",
week_start=week_start.isoformat(),
week_end=week_end.isoformat(),
date_range=format_date_range(week_start, week_end),
config=config,
latest_sunday=latest,
previous_sunday=previous,
)
@@ -61,11 +113,16 @@ def index():
def report():
week_start, week_end, cutoff_utc = current_week()
total_episodes = int(os.environ.get("TOTAL_EPISODES", 1155))
latest = get_latest_sunday()
previous = get_previous_sunday()
jellyfin.clear_debug_logs()
try:
ep_info = jellyfin.find_last_one_piece_before(cutoff_utc)
except Exception as e:
return jsonify({"error": str(e)}), 502
debug_output = jellyfin.get_debug_logs()
return jsonify({"error": str(e), "debug": debug_output}), 502
db.save_episode(
episode_number=ep_info["episode_number"],
@@ -87,6 +144,8 @@ def report():
f"Around {watched_this_week} episodes watched this week!"
)
debug_output = jellyfin.get_debug_logs()
return jsonify({
"week_start": week_start.isoformat(),
"week_end": week_end.isoformat(),
@@ -98,6 +157,9 @@ def report():
"media_name": ep_info.get("media_name"),
"watched_at": ep_info.get("watched_at"),
"report": report_text,
"latest_sunday": latest,
"previous_sunday": previous,
"debug": debug_output,
})