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