99 lines
2.9 KiB
Python
99 lines
2.9 KiB
Python
from selenium import webdriver
|
|
from selenium.webdriver.firefox.options import Options
|
|
# from selenium.webdriver.common.keys import Keys
|
|
from selenium.webdriver.common.by import By
|
|
import time
|
|
from bs4 import BeautifulSoup
|
|
import lxml
|
|
import threading
|
|
|
|
import discord
|
|
from discord.ext import commands
|
|
|
|
#objs
|
|
options = webdriver.FirefoxOptions()
|
|
intents = discord.Intents.default()
|
|
prikols = commands.Bot(intents=intents, command_prefix = '!')
|
|
|
|
# consts
|
|
weekdays = ("Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота", "Воскресенье")
|
|
current_week = '/html/body/main/div[2]/div/div/div[3]/div[4]/div[2]'
|
|
options.headless = True
|
|
intents.message_content = True
|
|
intents.messages = True
|
|
|
|
url = "https://amursu.ru/obrazovanie/timetable/timetable-group/?id_group=1891"
|
|
|
|
# гайд на завтра:
|
|
# 1. собираем все хпатх | ок
|
|
# 2. расчекрыживаем при помощи bs4 | ок
|
|
# 3. приводим в читаемый вид | fail
|
|
# 4. отправляем в дс | ок
|
|
|
|
def get_element_from_driver(url : str, instruction : list) -> list:
|
|
driver = webdriver.Firefox()
|
|
driver.implicitly_wait(10)
|
|
driver.get(url)
|
|
assert "Расписание" in driver.title
|
|
|
|
elem = []
|
|
for i in instruction:
|
|
elem.append(driver.find_element(By.XPATH, i).get_attribute('innerHTML'))
|
|
driver.close()
|
|
|
|
return elem
|
|
|
|
def get_day_info(html : str) -> str:
|
|
'''Get's day of th week number
|
|
posible numbers: 3-9 (included)'''
|
|
|
|
soup = BeautifulSoup(html, 'lxml')
|
|
soupa = soup.findAll("a", attrs = {'class':'ui link blue speak'})
|
|
soup = BeautifulSoup(str(soupa)).find_all('a')
|
|
soupa = ''
|
|
|
|
for data in soup:
|
|
soupa += data.get('data-speech') + '\n'
|
|
|
|
return soupa
|
|
|
|
def make_it_better(raw_day_info : str, current_week_num : int) -> dict:
|
|
day_info = raw_day_info.split('\n')
|
|
temp = []
|
|
for i in day_info:
|
|
if i.find('По второй неделе') != -1:
|
|
temp.append(day_info.split(sep='По второй неделе')[current_week_num])
|
|
else:
|
|
temp.append(i)
|
|
|
|
return temp
|
|
|
|
@prikols.command()
|
|
async def day(ctx, arg):
|
|
try:
|
|
await ctx.send(get_day_info(arg, url=url))
|
|
except BaseException as err:
|
|
await ctx.send(err)
|
|
|
|
@prikols.command()
|
|
async def week(ctx):
|
|
try:
|
|
arr = []
|
|
arr.append(current_week)
|
|
for day in range(3,9):
|
|
arr.append(f'/html/body/main/div[2]/div/div/div[3]/div[4]/div[{day}]')
|
|
|
|
html = get_element_from_driver(url=url, instruction=arr)
|
|
|
|
for i in range(len(html)):
|
|
await ctx.send(f"{html[len(arr)-1].strip()} | " + weekdays[i] + f'\n~ {i-2} пара ~\n' +
|
|
"")
|
|
except BaseException as err:
|
|
await ctx.send(err)
|
|
|
|
@prikols.event
|
|
async def on_ready():
|
|
print('yolo')
|
|
|
|
# now my token secured (maybe)
|
|
prikols.run(f'{open('token', 'r').read()}') |