-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-seminars.py
206 lines (158 loc) · 5.34 KB
/
update-seminars.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from __future__ import annotations
import os
from dataclasses import dataclass
from datetime import datetime
from IPython.display import display,HTML
import calendar
import dateparser
import requests
from markdown import markdown
def request_github_api(query_url: str, owner="geem-lab", token=None) -> dict:
if token is None:
token = os.environ["GITHUB_TOKEN"]
gh_session = requests.Session()
gh_session.auth = (owner, token)
params = {} # {"state": "open"}
authorization = f"token {token}"
headers = {
"Accept": "application/vnd.github.v3+json",
"Authorization": authorization,
}
return gh_session.get(query_url, headers=headers, params=params).json()
def tag(tag_name):
def _tag(*args, **kwargs):
def _normalize_key(key):
if key.endswith("_"):
return key[:-1]
return key
attrs = " ".join(f'{_normalize_key(k)}="{v}"' for k, v in kwargs.items())
contents = "".join(arg for arg in args if arg)
if attrs and contents:
return f"<{tag_name} {attrs}>{contents}</{tag_name}>"
elif attrs:
return f"<{tag_name} {attrs} />"
elif contents:
return f"<{tag_name}>{contents}</{tag_name}>"
else:
return f"<{tag_name} />"
return _tag
em = tag("em")
time = tag("time")
h2 = tag("h2")
p = tag("p")
strong = tag("strong")
a = tag("a")
details = tag("details")
summary = tag("summary")
span = tag("span")
li = tag("li")
ul = tag("ul")
small = tag("small")
img = tag("img")
@dataclass
class Seminar:
title: str
speaker: dict
description: str
date: datetime
STRFTIME_FORMAT = "%b %-d %Y"
def __post_init__(self):
if isinstance(self.speaker, str):
self.speaker = request_github_api(
f"https://api.github.com/users/{self.speaker}"
)
def _date_to_markdown(self):
dt = time(
"📅 ",
self.date.strftime(self.STRFTIME_FORMAT),
datetime=self.date.isoformat(),
)
return small(strong(dt))
def _title_to_markdown(self):
return em(self.title)
@property
def speaker_name(self):
if "name" in self.speaker:
return self.speaker["name"]
else:
return f"@{self.speaker['login']}"
@property
def speaker_url(self):
return f"https://github.com/{self.speaker['login']}"
def _speaker_name_to_markdown(self):
return a(self.speaker_name, href=self.speaker_url)
AVATAR_WIDTH = 128
def _speaker_avatar_to_markdown(self):
if "avatar_url" in self.speaker:
return a(
img(
src=self.speaker["avatar_url"],
alt=self.speaker["login"],
title=self.speaker_name,
align="left",
width=self.AVATAR_WIDTH,
),
href=self.speaker_url,
)
def _description_to_markdown(self):
return markdown(self.description)
def to_markdown(self):
return details(
summary(
self._date_to_markdown(),
" ",
self._title_to_markdown(),
" (",
self._speaker_name_to_markdown(),
")",
),
self._speaker_avatar_to_markdown(),
self._description_to_markdown(),
)
DATE_MARKER = "**Date**:"
@classmethod
def from_github_issue(cls, issue):
title = issue["title"].replace("[SEMINAR]", "").strip()
description, date = issue["body"].split(cls.DATE_MARKER)[:2]
description = description.rstrip(cls.DATE_MARKER).strip()
date = date.splitlines()[0].strip()
date = dateparser.parse(date)
if issue["assignees"]:
speaker = issue["assignees"][0]["login"]
else:
speaker = issue["user"]["login"]
return Seminar(title=title, speaker=speaker, description=description, date=date)
@dataclass
class SeminarList:
seminars: list[Seminar]
def __post_init__(self):
self.seminars = sorted(
self.seminars, key=lambda seminar: seminar.date, reverse=True
)
HEADER = """# Seminars
Click on each seminar to see more details.
> Want to add a seminar? Check if the date of interest is available and take a look at [the instructions page](/seminars/instructions).
"""
CALENDAR = calendar.HTMLCalendar().formatmonth(datetime.today().year, datetime.today().month)
def to_markdown(self):
return self.HEADER + "".join(seminar.to_markdown() for seminar in self.seminars)
@staticmethod
def from_github_issues(issues):
seminars = [
Seminar.from_github_issue(issue)
for issue in issues
if issue["title"].startswith("[SEMINAR]")
]
return SeminarList(seminars)
@staticmethod
def from_github_repo(owner, repo, token=None):
issues = request_github_api(
f"https://api.github.com/repos/{owner}/{repo}/issues",
owner=owner,
token=token,
)
return SeminarList.from_github_issues(issues)
if __name__ == "__main__":
seminars = SeminarList.from_github_repo(owner="geem-lab", repo="seminars")
display(HTML(SeminarList.CALENDAR))
print(seminars.to_markdown())