File size: 2,692 Bytes
fb2916f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""The problem-set right rail: daily calendar, weekly premium, companies.

The captured page renders three cards beside the question list. Two of them are
time-based, so they are computed from the request clock rather than frozen: the
calendar highlights today and counts down to local midnight, and the weekly
strip highlights the current week of the month. The company list is evidence:
``data/trending_companies.json`` holds the names and counts the captured rail
printed, in the order it printed them.
"""

from __future__ import annotations

import calendar as _calendar
import json
from datetime import datetime, timedelta
from pathlib import Path
from typing import Any

DATA_DIR = Path(__file__).resolve().parent / "data"

_COMPANY_DATA: dict[str, Any] = json.loads(
    (DATA_DIR / "trending_companies.json").read_text(encoding="utf-8")
)
COMPANIES: tuple[dict[str, Any], ...] = tuple(_COMPANY_DATA["companies"])

WEEKDAY_LABELS: tuple[str, ...] = ("S", "M", "T", "W", "T", "F", "S")


def _remaining_label(now: datetime) -> tuple[str, str]:
    """``HH:MM:SS`` until local midnight, plus the ISO deadline for the client."""

    midnight = (now + timedelta(days=1)).replace(
        hour=0, minute=0, second=0, microsecond=0
    )
    remaining = midnight - now
    seconds = max(0, int(remaining.total_seconds()))
    hours, rest = divmod(seconds, 3600)
    minutes, secs = divmod(rest, 60)
    return f"{hours:02d}:{minutes:02d}:{secs:02d}", midnight.isoformat()


def _weeks(year: int, month: int) -> list[list[int | None]]:
    """Month grid with Sunday-first columns, matching the captured header."""

    grid = _calendar.Calendar(firstweekday=6).monthdayscalendar(year, month)
    return [[day or None for day in week] for week in grid]


def daily_panel(now: datetime | None = None) -> dict[str, Any]:
    moment = now or datetime.now()
    remaining, deadline = _remaining_label(moment)
    week_index = min(4, (moment.day - 1) // 7)
    days_left = _calendar.monthrange(moment.year, moment.month)[1] - moment.day
    return {
        "today": moment.day,
        "month_label": moment.strftime("%B %Y"),
        "weekday_labels": WEEKDAY_LABELS,
        "weeks": _weeks(moment.year, moment.month),
        "remaining": remaining,
        "midnight": deadline,
        "weekly_left": ("1 day left" if days_left <= 1 else f"{days_left} days left"),
        "weekly_slots": [
            {"label": f"W{index + 1}", "active": index == week_index}
            for index in range(5)
        ],
    }


def companies(limit: int = 168) -> tuple[dict[str, Any], ...]:
    """The head of the captured company rail, paged 28 at a time by the client."""

    return COMPANIES[:limit]