From fc5cef0dc36a62e3ee71eac2b4411599eb6e5ba7 Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 28 May 2020 10:50:33 +0200 Subject: [PATCH] Move object related actions to Calendar DB object --- db/Calendar.py | 28 ++++++++++++++++++++++++++++ extensions/calendar.py | 22 +++++----------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/db/Calendar.py b/db/Calendar.py index dfb0989..4c0e776 100644 --- a/db/Calendar.py +++ b/db/Calendar.py @@ -1,7 +1,23 @@ +import re +from datetime import datetime, timezone + +import ics +import requests + from db import Base from sqlalchemy import Column, Integer, String, BigInteger +name_re = re.compile(r"([A-Z]+ [A-Z]+)") + + +def url(resources: int, project_id: int, first_date: datetime, last_date: datetime): + first_date = first_date.strftime("%Y-%m-%d") + last_date = last_date.strftime("%Y-%m-%d") + return "http://adelb.univ-lyon1.fr/jsp/custom/modules/plannings/anonymous_cal.jsp?" \ + f"resources={resources}&projectId={project_id}&calType=ical&firstDate={first_date}&lastDate={last_date}" + + class Calendar(Base): __tablename__ = "calendars" id = Column(Integer, primary_key=True) @@ -15,3 +31,15 @@ class Calendar(Base): self.resources = resources self.project_id = project_id self.server = server + + def cal(self, first_date: datetime, last_date: datetime) -> ics.Calendar: + return ics.Calendar(requests.get(url(self.resources, self.project_id, first_date, last_date)).text) + + def events(self, first_date: datetime, last_date: datetime) -> [ics.Event]: + events = [] + for e in list(self.cal(first_date, last_date).events)[::-1]: + e.begin = e.begin.replace(tzinfo=timezone.utc).astimezone(tz=None) + e.end = e.begin.replace(tzinfo=timezone.utc).astimezone(tz=None) + e.organizer = name_re.findall(e.description)[0] + events.append(e) + return events diff --git a/extensions/calendar.py b/extensions/calendar.py index c798cf7..b60d8c5 100644 --- a/extensions/calendar.py +++ b/extensions/calendar.py @@ -15,14 +15,6 @@ extension_name = "calendar" logger = logger.getChild(extension_name) url_re = re.compile(r"http:\/\/adelb\.univ-lyon1\.fr\/jsp\/custom\/modules\/plannings\/anonymous_cal\.jsp\?resources=" r"([0-9]+)&projectId=([0-9]+)") -name_re = re.compile(r"([A-Z]+ [A-Z]+)") - - -def url(resources: int, project_id: int, first_date: datetime, last_date: datetime): - first_date = first_date.strftime("%Y-%m-%d") - last_date = last_date.strftime("%Y-%m-%d") - return "http://adelb.univ-lyon1.fr/jsp/custom/modules/plannings/anonymous_cal.jsp?" \ - f"resources={resources}&projectId={project_id}&calType=ical&firstDate={first_date}&lastDate={last_date}" class Calendar(commands.Cog): @@ -91,7 +83,7 @@ class Calendar(commands.Cog): @calendar.group("day", pass_context=True) async def calendar_day(self, ctx: commands.Context, name: str, day: str = None): s = db.Session() - c = s.query(db.Calendar).filter(db.Calendar.server == ctx.guild.id).filter(db.Calendar.name == name).first() + c: db.Calendar = s.query(db.Calendar).filter(db.Calendar.server == ctx.guild.id).filter(db.Calendar.name == name).first() if not c: raise BadArgument() if day is None: @@ -101,14 +93,10 @@ class Calendar(commands.Cog): date = datetime.strptime(day, "%d/%m/%Y") except ValueError: raise BadArgument() - calendar = ics.Calendar(requests.get(url(c.resources, c.project_id, date, date)).text) - embed = Embed(title="Day calendar", description=date.strftime("%d/%m/%Y")) - for e in list(calendar.events)[::-1]: - start = e.begin.replace(tzinfo=timezone.utc).astimezone(tz=None).strftime('%H:%M') - end = e.end.replace(tzinfo=timezone.utc).astimezone(tz=None).strftime('%H:%M') - by = name_re.findall(e.description)[0] - embed.add_field(name=f"{start} - {end}", - value=f"{e.name} | {e.location} - {by}", inline=False) + embed = Embed(title=f"Day calendar: {c.name}", description=date.strftime("%d/%m/%Y")) + for e in c.events(date, date): + embed.add_field(name=f"{e.begin.strftime('%M:%H')} - {e.end.strftime('%M:%H')}", + value=f"{e.name} | {e.location} - {e.organizer}", inline=False) await ctx.send(embed=embed) @commands.Cog.listener()