login system

This commit is contained in:
2017-08-07 20:21:52 +08:00
parent 4ffa8f2646
commit b4d32cede7
2 changed files with 91 additions and 27 deletions

View File

@ -6,15 +6,13 @@ from bs4 import BeautifulSoup
# configurations
import config
class kuser:
def __init__(self, un='', pw=''):
self.username = un
self.password = pw
class kuser_api:
def __init__(self):
self.session = requests.Session()
# login kCOJ
def login(self):
payload = {'name': self.username,
'passwd': self.password,
def login(self, username, password):
payload = {'name': username,
'passwd': password,
'rdoCourse': 1}
return self.session.post(config.URL + '/Login', data=payload)
# check online status
@ -33,8 +31,9 @@ class kuser:
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)
submit = "期限已到" if tag.find_all('td')[4].get_text().strip() == "期限已過" else "期限未到"
status = tag.find_all('td')[5].get_text().strip()
questions[number] = (deadline, submit, status)
return questions
# show the content of the question
def show_question(self, number):
@ -44,6 +43,7 @@ class kuser:
content = content.replace('<body alink="#FFCCFF" bgcolor="#000000" link="#00FFFF" text="#FFFFFF" vlink="#CCFF33">\n', '')
content = content.replace('<!DOCTYPE html>\n\n', '').replace('<meta charset="utf-8"/>\n', '')
content = content.replace('<input onclick="history.go( -1 );return true;" type="button" value="上一頁"/>', '')
content = content.replace('<a href="upLoadHw?hwId=' + number + '"> 繳交作業 </a>', '')
content = content.replace('</body>', '').replace('<br/> ', '\n').replace('<br/>', '\n').replace(' ', '')
return content
# list passers of the question
@ -78,11 +78,7 @@ class kuser:
# for debug
def main():
users = {}
users[config.DEBUG_ID] = kuser(config.DEBUG_USER, config.DEBUG_PSWD)
me = users[config.DEBUG_ID]
me.login()
print(me.upload_answer('012'))
pass
if __name__ == '__main__':
main()

94
bot.py
View File

@ -1,7 +1,7 @@
#! /usr/bin/env python3
# necessary modules
import telepot, time, requests
import time, requests, telepot
from telepot.loop import MessageLoop
from pprint import pprint
# kCOJ API
@ -9,28 +9,96 @@ import access
# configurations
import config
users = {}
class kuser:
def __init__(self, un='', pw='', st=0):
self.username = un
self.password = pw
self.status = st
self.api = access.kuser_api()
def test_login(self, chat_id):
self.api.login(self.username, self.password)
if self.api.check_online() == True:
return True
else:
self.status = 1
bot.sendMessage(chat_id, "哇...登入失敗,讓我們重新開始一次")
time.sleep(0.6)
bot.sendMessage(chat_id, "請輸入您的學號:")
return False
def display_main(self, chat_id):
self.status = 3
q_list = self.api.list_questions()
q_available = "📝<i>可繳交的作業</i>\n"
q_unavailable = "📝<i>沒有可繳交的作業哦!</i>\n"
if q_list == {}:
q_str = q_unavailable
else:
q_str = q_available
for key in q_list.keys():
if q_list[key][1] == '期限未到':
q_str += "第 <b>" + key + "</b> 題到 <b>" + q_list[key][0] + "</b>\n/question_" + key + "\n"
if q_str == q_available:
q_str = q_unavailable
bot.sendMessage(chat_id, "💁 <b>" + self.username + "</b> /logout\n"
"\n" + q_str + "\n"
"你今天寫扣了嗎?", parse_mode='HTML')
def split_cmd(text):
if text[0] != '/':
return [text]
else:
cmd = text.replace('_', ' ').strip().lower().split(' ')
return cmd
def on_chat(msg):
pprint(msg)
content_type, chat_type, chat_id = telepot.glance(msg)
print(content_type, chat_type, chat_id)
# me = users[str(chat_id)]
me = users[config.DEBUG_ID] # for debug
me.login()
if (msg['text'] in me.list_questions()):
content = me.show_question(msg['text'])
bot.sendMessage(chat_id, '<code>' + content + '</code>', parse_mode='HTML')
me = kuser()
if str(chat_id) in users:
me = users[str(chat_id)]
else:
bot.sendMessage(chat_id, '```\n' + str(msg) + '\n```', parse_mode='Markdown')
users[str(chat_id)] = me
if content_type == 'text':
command = split_cmd(msg['text'])
if me.status == 1:
me.status = 2
me.username = msg['text']
bot.sendMessage(chat_id, "請輸入您的密碼:")
elif me.status == 2:
me.password = msg['text']
bot.sendMessage(chat_id, "登入中...")
if me.test_login(chat_id) == True:
me.display_main(chat_id)
elif command[0] == '/start':
if me.status == 0:
me.status = 1
bot.sendMessage(chat_id, "是初次見面的朋友呢,設定一下吧!\n")
time.sleep(0.6)
bot.sendMessage(chat_id, "請輸入您的學號:")
else:
if me.test_login(chat_id) == True:
me.display_main(chat_id)
else:
bot.sendMessage(chat_id, "我不是來看這些的。")
users = {}
bot = telepot.Bot(config.TOKEN)
MessageLoop(bot, {'chat': on_chat}).run_as_thread()
MessageLoop(bot, on_chat).run_as_thread()
# for debug
def main():
users[config.DEBUG_ID] = access.kuser(config.DEBUG_USER, config.DEBUG_PSWD)
pass
# over 20MB will stop this bot!
# bot.download_file('BQADBQADHQADPAc5VEZQejGkM9C4Ag', '../wa.txt')
main()
while True:
time.sleep(10)