Move object related actions to Calendar DB object
This commit is contained in:
parent
24e26f20ae
commit
fc5cef0dc3
2 changed files with 33 additions and 17 deletions
|
@ -1,7 +1,23 @@
|
||||||
|
import re
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
import ics
|
||||||
|
import requests
|
||||||
|
|
||||||
from db import Base
|
from db import Base
|
||||||
from sqlalchemy import Column, Integer, String, BigInteger
|
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):
|
class Calendar(Base):
|
||||||
__tablename__ = "calendars"
|
__tablename__ = "calendars"
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
|
@ -15,3 +31,15 @@ class Calendar(Base):
|
||||||
self.resources = resources
|
self.resources = resources
|
||||||
self.project_id = project_id
|
self.project_id = project_id
|
||||||
self.server = server
|
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
|
||||||
|
|
|
@ -15,14 +15,6 @@ extension_name = "calendar"
|
||||||
logger = logger.getChild(extension_name)
|
logger = logger.getChild(extension_name)
|
||||||
url_re = re.compile(r"http:\/\/adelb\.univ-lyon1\.fr\/jsp\/custom\/modules\/plannings\/anonymous_cal\.jsp\?resources="
|
url_re = re.compile(r"http:\/\/adelb\.univ-lyon1\.fr\/jsp\/custom\/modules\/plannings\/anonymous_cal\.jsp\?resources="
|
||||||
r"([0-9]+)&projectId=([0-9]+)")
|
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):
|
class Calendar(commands.Cog):
|
||||||
|
@ -91,7 +83,7 @@ class Calendar(commands.Cog):
|
||||||
@calendar.group("day", pass_context=True)
|
@calendar.group("day", pass_context=True)
|
||||||
async def calendar_day(self, ctx: commands.Context, name: str, day: str = None):
|
async def calendar_day(self, ctx: commands.Context, name: str, day: str = None):
|
||||||
s = db.Session()
|
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:
|
if not c:
|
||||||
raise BadArgument()
|
raise BadArgument()
|
||||||
if day is None:
|
if day is None:
|
||||||
|
@ -101,14 +93,10 @@ class Calendar(commands.Cog):
|
||||||
date = datetime.strptime(day, "%d/%m/%Y")
|
date = datetime.strptime(day, "%d/%m/%Y")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise BadArgument()
|
raise BadArgument()
|
||||||
calendar = ics.Calendar(requests.get(url(c.resources, c.project_id, date, date)).text)
|
embed = Embed(title=f"Day calendar: {c.name}", description=date.strftime("%d/%m/%Y"))
|
||||||
embed = Embed(title="Day calendar", description=date.strftime("%d/%m/%Y"))
|
for e in c.events(date, date):
|
||||||
for e in list(calendar.events)[::-1]:
|
embed.add_field(name=f"{e.begin.strftime('%M:%H')} - {e.end.strftime('%M:%H')}",
|
||||||
start = e.begin.replace(tzinfo=timezone.utc).astimezone(tz=None).strftime('%H:%M')
|
value=f"{e.name} | {e.location} - {e.organizer}", inline=False)
|
||||||
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)
|
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
|
|
Reference in a new issue