Open vaakian opened 3 years ago
import threading import time import requests class EducoderFucker(object): login_url = 'https://www.educoder.net/api/accounts/login.json' username = '' password = '' session = requests.session() session.headers['Content-Type'] = 'application/json; charset=utf-8' def __init__(self, username, password): self.username = username self.password = password self.login() def login(self): data = '{"login":"%s","password":"%s","autologin":true}' % (self.username, self.password) res = self.session.post(self.login_url, data=data) if eval(res.text).get('status'): raise Exception("fail to login\n" + res.text) def start(self): res = self.create() if res['status'] == 0: if self.publish(res['identifier']): sub_res = self.submit(res['identifier']) print(sub_res) def create(self): create_form = '''{"hack":{"difficult":3,"sub_discipline_id":5,"time_limit":3,"name":"output 1","description":"asd","test_set_score":true,"score":300},"hack_codes":{"language":"Python","code":"cHJpbnQoMSk="},"hack_sets":[{"input":"","output":"1","score":100,"position":1}]}''' create_url = 'https://www.educoder.net/api/problems.json' '''POST {"status":0,"message":"success","identifier":"sg4h5pi7"}''' res = self.session.post(create_url, data=create_form) print('create ', res.json()) return res.json() def publish(self, idt): publish_form = '{}' publish_url = f'https://www.educoder.net/api/problems/{idt}/publish.json' '''POST {"status":0,"message":"success"}''' res = self.session.post(publish_url, data=publish_form) print('publish', res.text) return res.json()['status'] == 0 def submit(self, idt): real_idt = self.get_identifier(idt) submit_url = f'https://www.educoder.net/api/myproblems/{real_idt}/code_submit.json?' submit_res = self.session.get(submit_url) print('submit', submit_res.text) is_submited = submit_res.json()['status'] == 0 if is_submited: t = threading.Thread(target=lambda: self.await_delete(real_idt)) t.start() return is_submited def get_identifier(self, idt): start_url = f'https://www.educoder.net/api/problems/{idt}/start.json?' '''GET {"status":0,"message":"success","identifier":"zhyps4lbj2vf"}''' res = self.session.get(start_url) print('real_identifier', res.text) real_itd = res.json()['identifier'] return real_itd def await_delete(self, real_idt): url = f'https://www.educoder.net/api/myproblems/{real_idt}/result.json?mode=submit' max_time = 20 while max_time > 0: max_time = max_time - 1 time.sleep(0.5) res = self.session.get(url) print(f'删除第{20 - max_time}次', res.json()) if res.json()['status'] == 0: print('已删除', real_idt) return print('超时!', real_idt) if name == 'main': fucker = EducoderFucker('user', 'pass') i = 5 while i < 0: fucker.start() i = i - 1