Files
webcal-dashboard/app/views.py
2022-05-25 16:59:29 -05:00

94 lines
3.5 KiB
Python

from app import app
from datetime import datetime, timedelta, timezone
import os
import caldav
import datetime
from icalendar import cal, Event
from flask import render_template
from .models import Event
import requests
caldav_url = os.getenv('caldav_url')
username = os.getenv('username')
password = os.getenv('password')
cal_id = os.getenv('cal_id')
def remove_emoji(string):
import re
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
u"\U00002500-\U00002BEF" # chinese char
u"\U00002702-\U000027B0"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
u"\U0001f926-\U0001f937"
u"\U00010000-\U0010ffff"
u"\u2640-\u2642"
u"\u2600-\u2B55"
u"\u200d"
u"\u23cf"
u"\u23e9"
u"\u231a"
u"\ufe0f" # dingbats
u"\u3030"
"]+", flags=re.UNICODE)
return emoji_pattern.sub(r'', string)
def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days)):
yield datetime.datetime.date(start_date + timedelta(n))
@app.route('/')
def dashboard():
today = datetime.datetime.now(tz=datetime.datetime.now(timezone.utc).astimezone().tzinfo)
today = datetime.datetime(2022,6,5, tzinfo=datetime.datetime.now(timezone.utc).astimezone().tzinfo)
start_of_week = today - timedelta(days=(today.weekday()+1)) # Monday
end_of_week = start_of_week + timedelta(days=8) # Sunday
events = []
for url in [
'https://www.calendarlabs.com/ical-calendar/ics/76/US_Holidays.ics',
]:
r = requests.get(url)
c = cal.Calendar.from_ical(r.content)
events += Event.fromIcalendar(c)
with caldav.DAVClient(url=caldav_url, username=username, password=password) as client:
my_principal = client.principal()
calendars = my_principal.calendars()
for id in [cal_id]:
calendar = my_principal.calendar(cal_id=id)
events += Event.fromCalDavEvents(calendar.date_search(
start=start_of_week, end=end_of_week, expand=False))
for url in [
'http://ical-cdn.teamsnap.com/team_schedule/5f1ddc9e-15b0-4912-84a2-11cc70e9e375.ics'
]:
r = requests.get(url)
c = cal.Calendar.from_ical(r.content)
events += Event.fromIcalendar(c)
days = []
for single_date in daterange(start_of_week, end_of_week):
days_events = []
for e in events:
if (e.dtstart.date() <= single_date <= e.dtend.date()):
e.summary = remove_emoji(e.summary)
days_events.append(e)
days.append((single_date, days_events))
# breakpoint()
pass
# r = "<br>".join([event.vobject_instance.vevent.summary.value for event in events_fetched if event.vobject_instance.vevent.dtstart.value < datetime.now()])
return render_template("dashboard.html",
days=days,
today=today)