diff --git a/access.py b/access.py new file mode 100644 index 0000000..050f3c5 --- /dev/null +++ b/access.py @@ -0,0 +1,88 @@ +#! /usr/bin/env python3 + +# necessary modules +import requests +from bs4 import BeautifulSoup +# configurations +import config + +class kuser: + def __init__(self, un='', pw=''): + self.username = un + self.password = pw + self.session = requests.Session() + # login kCOJ + def login(self): + payload = {'name': self.username, + 'passwd': self.password, + 'rdoCourse': 1} + return self.session.post(config.URL + '/Login', data=payload) + # check online status + def check_online(self): + response = self.session.get(config.URL + '/TopMenu') + soup = BeautifulSoup(response.text, 'html.parser') + return soup.find('a').get_text() == '線上考試' + # list all questions, deadline and hand-in status + def list_questions(self): + questions = {} + response = self.session.get(config.URL + '/HomeworkBoard') + soup = BeautifulSoup(response.text, 'html.parser') + for tag in soup.find_all('tr'): + if tag.find('a') == None: + continue + else: + number = tag.find('a').get_text() + deadline = tag.find_all('td')[3].get_text() + status = tag.find_all('td')[5].get_text() + questions[number] = (deadline, status) + return questions + # show the content of the question + def show_question(self, number): + response = self.session.get(config.URL + '/showHomework', params={'hwId': number}) + soup = BeautifulSoup(response.text, 'html.parser') + content = str(soup.find('body')) + content = content.replace('
\n', '') + content = content.replace('\n\n', '').replace('\n', '') + content = content.replace('', '') + content = content.replace('', '').replace('' + content + '
', parse_mode='HTML')
+ else:
+ bot.sendMessage(chat_id, '```\n' + str(msg) + '\n```', parse_mode='Markdown')
+
+users = {}
+bot = telepot.Bot(config.TOKEN)
+MessageLoop(bot, {'chat': on_chat}).run_as_thread()
+
+# for debug
+def main():
+ users[config.DEBUG_ID] = access.kuser(config.DEBUG_USER, config.DEBUG_PSWD)
+
+main()
+while True:
+ time.sleep(10)
\ No newline at end of file
diff --git a/config.py b/config.py
new file mode 100644
index 0000000..865b373
--- /dev/null
+++ b/config.py
@@ -0,0 +1,8 @@
+# kCOJ_bot
+TOKEN = ""
+# kCOJ
+URL = ""
+# debug config
+DEBUG_ID = ""
+DEBUG_USER = ""
+DEBUG_PSWD = ""
\ No newline at end of file