-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
80 lines (66 loc) · 2.6 KB
/
main.py
File metadata and controls
80 lines (66 loc) · 2.6 KB
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
import base64
from datetime import date, datetime
from kegscraper import bromcom as bc
import os
import discord
import asyncio
import aiohttp
import io
import colorgram
from PIL import Image
URL = os.environ["BSCRT_URL"]
def img_from_bytes(img_bytes: bytes) -> Image.Image:
return Image.open(io.BytesIO(img_bytes))
def to_b64(img: Image.Image):
# https://stackoverflow.com/questions/31826335/how-to-convert-pil-image-image-object-to-base64-string
buffer = io.BytesIO()
img.save(buffer, "JPEG")
return "data:image/jpeg;base64," + base64.b64encode(buffer.getvalue()).decode()
def get_anomalies(table: list[bc.Lesson], mode: dict[str, dict[str, dict[str, bc.Lesson]]]) -> list[bc.Lesson]:
ret = []
for lesson in table:
associated_lesson = mode[lesson.week_a_b][lesson.start.strftime("%A")][lesson.period]
if any((
lesson.room != associated_lesson.room,
lesson.subject != associated_lesson.subject,
lesson.teacher != associated_lesson.teacher
)):
ret.append(lesson)
return ret
async def main():
sess = bc.login(
int(os.environ["BSCRT_ID"]),
os.environ["BSCRT_UN"],
os.environ["BSCRT_PW"]
)
# user_id = os.environ["BSCRPT_DID"]
# mention = f"<@{user_id}>"
assert sess.name
username = sess.name.title()
pfp_img = img_from_bytes(sess.pfp)
pfp = to_b64(pfp_img)
# return
async with aiohttp.ClientSession() as aiohttp_session:
webhook = discord.Webhook.from_url(URL, session=aiohttp_session)
async def set_pfp(name: str, avatar: str):
return await aiohttp_session.patch(URL, json={
"name": name,
"avatar": avatar
})
lessons = get_anomalies(sess.get_timetable_list(), sess.get_mode_timetables())
week = "?"
if lessons:
week = lessons[0].week_a_b.upper()
embed = discord.Embed(title=f"{date.today():%A} ({week})")
r, g ,b = colorgram.extract(pfp_img, 1)[0].rgb
embed.color = b + 256 * (g + 256 * r)
embed.add_field(name="Timetable", value='\n'.join(f"{lesson.start:%a} {lesson.subject} @{lesson.room} ({lesson.teacher})" for lesson in lessons))
embed.add_field(name="Attendance", value=f"{
'\n'.join(
f"{x.get("periods")}: {x.get("markDescription")}"
for x in sess.attendance_status.get("attendanceList", []
))}")
await set_pfp(username, pfp)
await webhook.send(embed=embed, username=username)
if __name__ == "__main__":
asyncio.run(main())