Tasks printed for multiple days

This commit is contained in:
Dejvino 2026-02-01 20:32:28 +01:00
parent 618b1641ff
commit 6f5d744628
2 changed files with 71 additions and 45 deletions

View File

@ -13,6 +13,9 @@ except ImportError:
HAS_ICAL_DEPS = False
class TasksJob(Job):
def __init__(self):
self.days_to_print = 1
def get_name(self):
return "UKOLY DNE"
@ -30,6 +33,14 @@ class TasksJob(Job):
f.write(url)
print("URL saved.")
print("-" * 30)
try:
d_input = input(f"Number of days to print [{self.days_to_print}]: ").strip()
if d_input:
self.days_to_print = int(d_input)
except ValueError:
pass
def print_body(self, p):
if not HAS_ICAL_DEPS:
print("Error: Missing iCal Libraries.")
@ -55,43 +66,52 @@ class TasksJob(Job):
# 2. Parse Calendar
cal = icalendar.Calendar.from_ical(response.content)
# 3. Calculate Time Range (Today)
now = datetime.datetime.now().astimezone()
start_of_day = now.replace(hour=0, minute=0, second=0, microsecond=0)
end_of_day = now.replace(hour=23, minute=59, second=59, microsecond=0)
# 3. Loop over days
base_now = datetime.datetime.now().astimezone()
for i in range(self.days_to_print):
p.cut()
target_date = base_now + datetime.timedelta(days=i)
# Print Calendar Name
cal_name = cal.get('X-WR-CALNAME')
if cal_name:
p.set(align='center', bold=True)
p.text(f"{str(cal_name)}\n")
days_cz = ["Pondělí", "Úterý", "Středa", "Čtvrtek", "Pátek", "Sobota", "Neděle"]
p.text(f"{days_cz[target_date.weekday()]} {target_date.strftime('%d.%m.%Y')}\n")
p.set(align='left', bold=False)
p.text("-" * 32 + "\n")
start_of_day = target_date.replace(hour=0, minute=0, second=0, microsecond=0)
end_of_day = target_date.replace(hour=23, minute=59, second=59, microsecond=0)
# 4. Expand Recurring Events
# This library handles RRULEs (e.g. "Every Monday") automatically
events = recurring_ical_events.of(cal).between(start_of_day, end_of_day)
# 5. Sort by start time
# Helper to get sortable time
def get_start_time(e):
dt = e.get('DTSTART').dt
# If it's a date object (all day), convert to datetime for sorting
if not isinstance(dt, datetime.datetime):
return datetime.datetime.combine(dt, datetime.time.min).replace(tzinfo=now.tzinfo)
return datetime.datetime.combine(dt, datetime.time.min).replace(tzinfo=base_now.tzinfo)
return dt
events.sort(key=get_start_time)
if not events:
p.text("Zadne ukoly pro dnesni den.\n")
p.text("Zadne ukoly.\n")
else:
for event in events:
summary = str(event.get('SUMMARY', '(bez nazvu)'))
dtstart = event.get('DTSTART').dt
# Format time
if not isinstance(dtstart, datetime.datetime):
time_str = "Cely den"
else:
# Convert to local system time for display
# (If dtstart is timezone aware, astimezone(None) converts to local)
local_dt = dtstart.astimezone(now.tzinfo)
local_dt = dtstart.astimezone(base_now.tzinfo)
time_str = local_dt.strftime('%H:%M')
# Print Entry
p.set(bold=True)
p.text(f"{time_str} [_]")
p.set(bold=False)

View File

@ -1,17 +1,23 @@
#!/usr/bin/env python3
import time
import argparse
from print_server import get_printer
from jobs.tasks import TasksJob
def main():
parser = argparse.ArgumentParser(description="Print daily tasks schedule.")
parser.add_argument("--days", type=int, default=1, help="Number of days to print (default: 1)")
args = parser.parse_args()
print("Initializing printer...")
p = get_printer()
if not p:
print("Failed to connect to printer.")
return
print("Fetching and printing tasks...")
print(f"Fetching and printing tasks for {args.days} day(s)...")
job = TasksJob()
job.days_to_print = args.days
try:
# Run the job