From 8558480e6cef8cdf9890e1e38c7c8e8594bd2640 Mon Sep 17 00:00:00 2001 From: Oleg Smarandi Date: Tue, 30 Mar 2021 12:07:07 +0300 Subject: [PATCH] init --- .gitignore | 5 +++ README.md | 3 ++ sports_url.py | 53 ++++++++++++++++++++++ stankin_classes_alias.py | 12 +++++ urls.py | 12 +++++ vkbot.py | 97 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 182 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 sports_url.py create mode 100644 stankin_classes_alias.py create mode 100644 urls.py create mode 100644 vkbot.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8eb4bfd --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +Venv/ +.idea/ +__pycache__/ +.git/ +config.py \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..bb71e38 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# stankin-bot + +### This bot is for better edu.stankin.ru exp diff --git a/sports_url.py b/sports_url.py new file mode 100644 index 0000000..1ae75cd --- /dev/null +++ b/sports_url.py @@ -0,0 +1,53 @@ +import requests +from lxml import html +import re +import datetime + + +def get_logintoken(login_url, session_requests): + result = session_requests.get(login_url) + tree = html.fromstring(result.content) + return list(set(tree.xpath("//input[@name='logintoken']/@value")))[0] + + +def login(session_requests): + payload = {"username": "st119181", + "password": "St000QsYss%", + "logintoken": "rE4k5W4LgoOMKR7jJUFuQzQD1dJ4s18g"} + login_url = "https://edu.stankin.ru/login/" + payload['logintoken'] = get_logintoken(login_url, session_requests) + + result = session_requests.post( + login_url, + data=payload, + headers=dict(referer=login_url) + ) + return session_requests + + +def parse_result(result): + now = datetime.datetime.now() + p = re.compile(b'\xd0\x98\xd0\x94\xd0\x91-19-03 ') + + match = p.search(result.content) + print(match) + first = match.span()[0] - 54 + last = match.span()[0] - 2 + return result.content[first:last].decode("utf-8") + + +def get_url(): + session_requests = requests.session() + session_requests = login(session_requests) + url = 'https://edu.stankin.ru/mod/forum/view.php?id=140959' + result = session_requests.get( + url, + headers=dict(referer=url) + ) + print(result.status_code) + return parse_result(result) + + +if __name__ == "__main__": + print(get_url()) + diff --git a/stankin_classes_alias.py b/stankin_classes_alias.py new file mode 100644 index 0000000..3664b36 --- /dev/null +++ b/stankin_classes_alias.py @@ -0,0 +1,12 @@ +from enum import Enum + + +class Classes(Enum): + physics = "физика" + sports = "физра" + oop = "ооп" + elteh = "элтех" + probability_theory = "тервер" + calc_math = "вычмат" + hmi = "чмв" + mo = "мо" \ No newline at end of file diff --git a/urls.py b/urls.py new file mode 100644 index 0000000..dfbec31 --- /dev/null +++ b/urls.py @@ -0,0 +1,12 @@ +from enum import Enum +import sports_url + +class Urls(Enum): + physics_url = "https://edu.stankin.ru/mod/forum/discuss.php?d=37245" + sports_url = sports_url.get_url() + probability_theory_url = "https://us04web.zoom.us/postattendee?id=3" + oop_url = "https://us02web.zoom.us/j/87992120807?pwd=bktocTR1aGhFQks2OUU2V0QwcTRnUT09" + elteh_url = "https://us04web.zoom.us/j/5452496132?pwd=UWdBRjlKdlNHSXNzZjIybXlVRzhnQT09" + calc_math_url = "https://us02web.zoom.us/j/79640032120?pwd=ZXNpZmxGVXVSdFpxVWFTcUFIRitRZz09" + hmi_url = "https://discord.gg/smDRAXqxMR" + mo_url = "на мо ссылку не скину, там в дискорде сервер без приглашений(9" diff --git a/vkbot.py b/vkbot.py new file mode 100644 index 0000000..ed8747d --- /dev/null +++ b/vkbot.py @@ -0,0 +1,97 @@ +from config import group_id, api_token +from vk_api import VkApi +from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType +from urls import Urls +from stankin_classes_alias import Classes +import time + +class Bot: + def __init__(self, group_id, token): + self.group_id = group_id + self.token = token + self.vk = VkApi(token=token) + self.api = self.vk.get_api() + self.long_poller = VkBotLongPoll(self.vk, group_id=self.group_id) + self.time_stamp = time.time() + + def _send(self, url: Urls, event): + try: + self.api.messages.send(message="ща скину...", + random_id=event.object.random_id, + peer_id=event.object.peer_id) + self.api.messages.send(message=url.value, + random_id=event.object.random_id, + peer_id=event.object.peer_id) + except Exception: + self.api.messages.send(message="чёт ссылку не нашел...", + random_id=event.object.random_id, + peer_id=event.object.peer_id) + + def _update_sports_url(self): + if (time.time() - self.time_stamp) / 1800 >= 1: + Urls.sports_url.value = sports_url.get_url() + print(f"sports link had been updated: {Urls.sports_url.value}") + + + def send_sports_url(self, event): + self._send(Urls.sports_url, event) + + def send_physics_url(self, event): + self._send(Urls.physics_url, event) + + def send_probability_theory_url(self, event): + self._send(Urls.probability_theory_url, event) + + def send_oop_url(self, event): + self._send(Urls.oop_url, event) + + def send_elteh_url(self, event): + self._send(Urls.elteh_url, event) + + def send_calc_math_url(self, event): + self._send(Urls.calc_math_url, event) + + def send_hmi_url(self, event): + self._send(Urls.hmi_url, event) + + def send_mo_url(self, event): + self._send(Urls.mo_url, event) + + def _on_event(self, event): + cmd = event.object.text.lower() + if event.type == VkBotEventType.MESSAGE_NEW: + if Classes.sports.value in cmd: + self.send_sports_url(event) + if Classes.physics.value in cmd: + self.send_physics_url(event) + if Classes.probability_theory.value in cmd: + self.send_probability_theory_url(event) + if Classes.elteh.value in cmd: + self.send_elteh_url(event) + if Classes.oop.value in cmd: + self.send_oop_url(event) + if Classes.calc_math.value in cmd: + self.send_calc_math_url(event) + if Classes.hmi.value in cmd: + self.send_hmi_url(event) + if Classes.mo.value in cmd: + self.send_mo_url(event) + else: + print(f'can not proceed {event.type}') + + def run(self): + try: + for event in self.long_poller.listen(): + try: + self._update_sports_url() + self._on_event(event) + except Exception as err: + print(err) + except Exception as err: + print(err) + print(self.long_poller.listen()) + + +if __name__ == '__main__': + bot = Bot(group_id, api_token) + bot.run()