change architecture of api
This commit is contained in:
@ -1,28 +1,25 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
# necessary modules
|
||||
# modules
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
# configurations
|
||||
import config
|
||||
|
||||
class KuserAPI:
|
||||
def __init__(self):
|
||||
class KCOJ:
|
||||
def __init__(self, url):
|
||||
self._url = url
|
||||
self._session = requests.Session()
|
||||
# login kCOJ
|
||||
# login KCOJ
|
||||
def login_kcoj(self, username, password):
|
||||
try:
|
||||
payload = {'name': username,
|
||||
'passwd': password,
|
||||
'rdoCourse': 1}
|
||||
return self._session.post(config.URL + '/Login', data=payload, timeout=0.5)
|
||||
return self._session.post(self._url.URL + '/Login', data=payload, timeout=0.5)
|
||||
except requests.exceptions.Timeout:
|
||||
return None
|
||||
|
||||
# check online status
|
||||
def check_online(self):
|
||||
try:
|
||||
response = self._session.get(config.URL + '/TopMenu', timeout=0.5)
|
||||
response = self._session.get(self._url.URL + '/TopMenu', timeout=0.5)
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
return soup.find('a').get_text().strip() == '線上考試'
|
||||
except requests.exceptions.Timeout:
|
||||
@ -32,7 +29,7 @@ class KuserAPI:
|
||||
def list_questions(self):
|
||||
try:
|
||||
questions = {}
|
||||
response = self._session.get(config.URL + '/HomeworkBoard', timeout=0.5)
|
||||
response = self._session.get(self._url.URL + '/HomeworkBoard', timeout=0.5)
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
for tag in soup.find_all('tr'):
|
||||
if tag.find('a') == None:
|
||||
@ -50,7 +47,7 @@ class KuserAPI:
|
||||
# show the content of the question
|
||||
def show_question(self, number):
|
||||
try:
|
||||
response = self._session.get(config.URL + '/showHomework', params={'hwId': number}, timeout=0.5)
|
||||
response = self._session.get(self._url.URL + '/showHomework', params={'hwId': number}, timeout=0.5)
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
raw = soup.find('body').get_text().replace('繳交作業', '').strip()
|
||||
content = ''
|
||||
@ -64,7 +61,7 @@ class KuserAPI:
|
||||
def list_passers(self, number):
|
||||
try:
|
||||
passers = []
|
||||
response = self._session.get(config.URL + '/success.jsp', params={'HW_ID': number}, timeout=0.5)
|
||||
response = self._session.get(self._url.URL + '/success.jsp', params={'HW_ID': number}, timeout=0.5)
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
for tr in soup.find_all('tr'):
|
||||
passer = tr.get_text().replace('\n', '').strip()
|
||||
@ -78,7 +75,7 @@ class KuserAPI:
|
||||
def list_results(self, number, username):
|
||||
try:
|
||||
results = []
|
||||
response = self._session.get(config.URL + '/CheckResult.jsp', params={'questionID': number, 'studentID': username}, timeout=0.5)
|
||||
response = self._session.get(self._url.URL + '/CheckResult.jsp', params={'questionID': number, 'studentID': username}, timeout=0.5)
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
for tr in soup.find_all('tr'):
|
||||
td = tr.find('td')
|
||||
@ -93,7 +90,7 @@ class KuserAPI:
|
||||
try:
|
||||
payload = {'pass': password,
|
||||
'submit': 'sumit'}
|
||||
response = self._session.post(config.URL + '/changePasswd', data=payload, timeout=0.5)
|
||||
response = self._session.post(self._url.URL + '/changePasswd', data=payload, timeout=0.5)
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
return str(soup.find('body')).split()[-2].strip() == 'Success'
|
||||
except requests.exceptions.Timeout:
|
||||
@ -102,7 +99,7 @@ class KuserAPI:
|
||||
# delete the answer of the question
|
||||
def delete_answer(self, number):
|
||||
try:
|
||||
response = self._session.get(config.URL + '/delHw', params={'title': number}, timeout=0.5)
|
||||
response = self._session.get(self._url.URL + '/delHw', params={'title': number}, timeout=0.5)
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
return soup.find('body').get_text().replace('\n', '').strip() == 'delete success'
|
||||
except requests.exceptions.Timeout:
|
||||
@ -111,19 +108,12 @@ class KuserAPI:
|
||||
# hand in a answer
|
||||
def upload_answer(self, number, file_path):
|
||||
try:
|
||||
self._session.get(config.URL + '/upLoadHw', params={'hwId': number}, timeout=0.5)
|
||||
response = self._session.post(config.URL + '/upLoadFile',
|
||||
self._session.get(self._url.URL + '/upLoadHw', params={'hwId': number}, timeout=0.5)
|
||||
response = self._session.post(self._url.URL + '/upLoadFile',
|
||||
data={'FileDesc': 'Send from kcoj_bot'},
|
||||
files={'hwFile': open(file_path, 'rb')},
|
||||
timeout=0.5)
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
return soup.find('body').get_text().strip() != '您沒有上傳檔案 請重新操作'
|
||||
except requests.exceptions.Timeout:
|
||||
return False
|
||||
|
||||
# for debug
|
||||
def main():
|
||||
pass
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
return False
|
Reference in New Issue
Block a user