-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtournaments_calendar.py
51 lines (41 loc) · 1.62 KB
/
tournaments_calendar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from datetime import datetime
from icalendar import Calendar, Event
from tournaments_api import Tournament
_EVENT_DESCRIPTION = """
🏆 {title}
💰 Prize pool: {prize}
👥 Teams playing: {team_count}
ℹ️ Event info can be found at: {url}
☕️ If you find this useful, please consider buying me a coffee:
https://buymeacoffee.com/elongl
🐛 Report a bug/problem or request a feature:
elongliks@gmail.com
""".strip()
class TournamentsCalendar:
def __init__(self, tournaments: list[Tournament]):
self.tournaments = tournaments
def get_ical(self) -> str:
cal = Calendar()
for tournament in self.tournaments:
event = Event()
event.add("summary", f"[{tournament.tier.value}] {tournament.title}")
event.add("dtstart", tournament.start_date)
event.add("dtend", tournament.end_date)
event.add("dtstamp", datetime.now())
event.add("uid", id(tournament.title))
event.add("location", tournament.location)
event.add("url", tournament.url)
event.add("description", self._get_event_description(tournament))
cal.add_component(event)
return cal.to_ical()
def write_ical(self, path: str) -> None:
with open(path, "wb") as f:
f.write(self.get_ical())
def _get_event_description(self, tournament: Tournament) -> str:
return _EVENT_DESCRIPTION.format(
title=tournament.title,
prize=tournament.prize,
location=tournament.location,
team_count=tournament.team_count_description,
url=tournament.url,
)