-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
85 lines (74 loc) · 2.41 KB
/
main.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
import os
import discord
from time import sleep
from datetime import datetime
from discord.ext import commands, tasks
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from dotenv import load_dotenv
import asyncio
load_dotenv()
# selenium webdrivers
options = webdriver.ChromeOptions()
options.add_argument("--enable-javascript")
driver = webdriver.Chrome(options=options)
# discord clients
intents = discord.Intents.all()
client = discord.Client(intents=intents)
bot = commands.Bot(command_prefix='/', intents=intents)
# pandas dataframes
df = pd.read_csv("users.csv")
async def get_tee(username: str) -> int:
url = f"https://kenkoooo.com/atcoder/#/user/{username}"
driver.get(url)
await asyncio.sleep(20) # wait until page loaded
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
tee = soup.select("#root > div > div.my-5.container > div > div.my-3.row > div:nth-child(9) > h3")
return int(tee[0].text)
async def update_tee() -> None:
for id, row in df.iterrows():
df.at[id, "last_tee"] = row["tee"]
tee = await get_tee(row["username"])
df.at[id, "tee"] = tee - row["initial_tee"]
df.to_csv("users.csv", index=False)
return
async def create_message() -> str:
await update_tee()
updates = []
message = ""
for _, row in df.iterrows():
username = row["username"]
tee = row["tee"]
last_tee = row["last_tee"]
if tee != last_tee:
updates.append(f"{username}: {last_tee} -> {tee} (+{tee - last_tee})")
if updates:
message += ":information_source: **TEE速報**\n"
for update in updates:
message += update + "\n"
message += "\n"
message += ":crown: **Ranking**\n"
rank = 1
for _, row in df.sort_values("tee", ascending=False).iterrows():
username = row["username"]
tee = row["tee"]
message += f"{rank}. {username} (TEE: {tee})\n"
rank += 1
return message
@client.event
async def on_ready():
loop.start()
return
@bot.command(name='update_tee')
async def update(ext):
message = await create_message()
await ext.send(message)
@tasks.loop(seconds=60)
async def loop():
message = await create_message()
channel = client.get_channel(int(os.environ["CHANNEL_ID"]))
await channel.send(message)
client.run(os.environ["BOT_TOKEN"])