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