This commit is contained in:
Tenzing Kandang 2026-03-30 15:44:34 +02:00
parent a93810fbde
commit cd5b65b3cc

View File

@ -1,7 +1,6 @@
import os import os
import discord import discord
import datetime import datetime
import pytz
from google_auth_oauthlib.flow import InstalledAppFlow from google_auth_oauthlib.flow import InstalledAppFlow
from weather import OpenWeatherMapAPIClient from weather import OpenWeatherMapAPIClient
from googleapiclient.discovery import build from googleapiclient.discovery import build
@ -24,8 +23,7 @@ intents = discord.Intents(messages=True, guilds=True)
intents.message_content = True intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents) bot = commands.Bot(command_prefix='!', intents=intents)
user_location_db = UserLocation() user_location_db = UserLocation()
TZ = pytz.timezone("Europe/Paris") TZ = datetime.datetime.now().astimezone().tzinfo
def build_calendar_service(): def build_calendar_service():
creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES) creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES)
@ -34,21 +32,22 @@ def build_calendar_service():
def build_tasks_service(): def build_tasks_service():
creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES) creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES)
service = build('tasks', 'v1', credentials=creds) service = build('tasks', 'v1', credentials=creds)
return service, creds return service
def parse_google_time(s: str) -> datetime.datetime: def parse_rfc3339_to_local_date(ts):
dt = datetime.datetime.fromisoformat(s) if not ts:
if 'T' not in s: return None
return dt.replace(tzinfo=datetime.timezone.utc) try:
s = ts.replace("Z", "+00:00") if ts.endswith("Z") else ts
if "T" in ts:
dt = datetime.datetime.fromisoformat(s)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=datetime.timezone.utc)
return dt.astimezone(TZ).date()
return datetime.date.fromisoformat(s)
except Exception:
return None
if s.endswith('Z'):
return datetime.datetime.fromisoformat(s.replace('Z', '+00:00'))
if dt.tzinfo is None:
return dt.replace(tzinfo=datetime.timezone.utc)
return dt
#Calendar authentification #Calendar authentification
async def authenticate(): async def authenticate():
if os.path.exists(TOKEN_FILE): if os.path.exists(TOKEN_FILE):
@ -68,10 +67,12 @@ async def authenticate():
async def today(interaction: discord.Interaction): async def today(interaction: discord.Interaction):
await interaction.response.defer(thinking=True) await interaction.response.defer(thinking=True)
try: try:
service, creds = build_tasks_service() service = build_tasks_service()
tl_res = service.tasklists().list(maxResults=100).execute() tl_res = service.tasklists().list(maxResults=100).execute()
lists = tl_res.get("items", []) or [] lists = tl_res.get("items", []) or []
today = datetime.datetime.now(tz=TZ).date()
embed = discord.Embed( embed = discord.Embed(
title="Today's tasks", title="Today's tasks",
color=0x2ecc71, color=0x2ecc71,
@ -83,23 +84,35 @@ async def today(interaction: discord.Interaction):
tl_id = tl.get("id") tl_id = tl.get("id")
tl_title = tl.get("title") or "<untitled>" tl_title = tl.get("title") or "<untitled>"
tasks_res = service.tasks().list(tasklist=tl_id, showCompleted=True, showHidden=True, maxResults=250).execute() or {} tasks_res = service.tasks().list(
tasklist=tl_id, showCompleted=True, showHidden=True, maxResults=250).execute() or {}
items = tasks_res.get("items", []) or [] items = tasks_res.get("items", []) or []
lines = [] lines = []
for t in items: for t in items:
if not isinstance(t, dict): if not isinstance(t, dict):
continue continue
due_date = parse_rfc3339_to_local_date(t.get("due"))
completed_date = parse_rfc3339_to_local_date(t.get("completed"))
print("title:", t.get("title"), "due:", t.get("due"), "completed:", t.get("completed"), "status:", t.get("status"))
if due_date != today and completed_date != today:
continue
status = "" if t.get("status") == "completed" else "🔲" status = "" if t.get("status") == "completed" else "🔲"
title = t.get("title") or "(no title)" title = t.get("title") or "(no title)"
completed_ts = t.get("completed")
if completed_date == today and t.get("completed"):
if completed_ts: completed_short = t["completed"].replace("T", " ").split(".")[0]
completed_short = completed_ts.replace("T", "").split(".")[0]
lines.append(f"{status} {title} - completed {completed_short}") lines.append(f"{status} {title} - completed {completed_short}")
else: else:
lines.append(f"{status} {title}") if due_date:
lines.append(f"{status} {title} (due {due_date.isoformat()})")
else:
lines.append(f"{status}{title}")
if not lines: if not lines:
continue continue