def send_msg(msg_to_send: str): for sock in user_list: try: server_utils.send_msg(sock, msg_to_send) print('已向用户发送消息') except Exception as e: print(str(e)) logging.error(str(e))
def tcplink(sock: socket.socket, addr): print('Accept new connection from', addr) logging.info('Accept new user') server_utils.send_msg(sock, 'welcome') if last_msg_to_send is not None: server_utils.send_msg(sock, last_msg_to_send) while True: try: data: bytes = sock.recv(1024) print(type(data)) if len(data) == 0: break ch_raw = bytes.decode(data, encoding='utf-8') while len(ch_raw) != 0: try: len_pos = ch_raw.find('~') length = int(ch_raw[:len_pos]) ch = ch_raw[len_pos + 1:len_pos + length + 1] ch_raw = ch_raw[len_pos + length + 1:] print(ch) if not ch.startswith('HELLO-CALL'): logging.info("Receive:" + ch) if (ch.startswith("TODO:")): code = ch[5:9] pos = ch.find("~@$") username = ch[9:pos] password = ch[pos + 3:] submit.add_to_queue(username, password, code, sock) elif (ch.startswith("HELLO-CALL")): server_utils.send_msg(sock, 'FEEDBACK') print('send: FEEDBACK') elif (ch.startswith("INFO:")): username = ch[5:] userinfo.add_to_queue(username, sock) except Exception as err: print(err) print('解析字符串时出错') break except: break server_utils.send_msg(sock, 'toast:服务器即将关闭,请稍后再试') time.sleep(2) sock.close() user_list.remove(sock) print('Connection closed.') logging.warning('Connection closed.')
def get_user_info(username: str, sock: socket.socket): try: # Clear Browser Cookies user_result = [] if user_dict.get(username) is not None: user_result = user_dict.get(username) latest_code = user_latest_submit.get(username) cur_latest_code: str = '' name_user: str = '' print('开始获取' + username + '的用户信息') url = 'https://acm.sjtu.edu.cn/OnlineJudge/status?owner=' + \ username + '&problem=&language=0&verdict=0' html = getHTMLText(url) soup = BeautifulSoup(html, 'html.parser') first_sub = soup.find_all('tr')[1].find_all('td') name_user = name_user.join(first_sub[1].string) cur_latest_code = cur_latest_code.join(first_sub[0].string) # Check if the first fetch. first_fetch: bool = True cnt = 0 while True: try: flag = False allUniv = fillUnivList(soup) for i in range(len(allUniv)): if not first_fetch and i == 0: continue u = allUniv[i] if u[0] == latest_code: flag = True break code = ''.join(u[0]) problem = ''.join(u[2]) result = ''.join(u[3]) user_result.append(info_type(code, problem, result)) cnt += 1 if first_fetch: first_fetch = False if len(allUniv) < 15: break if flag: break # move next_code = allUniv[14][0] url = 'https://acm.sjtu.edu.cn/OnlineJudge/status?owner=' + \ username + '&problem=&language=0&verdict=0&top='+next_code html = getHTMLText(url) soup = BeautifulSoup(html, 'html.parser') except Exception as err: print('结束,信息获取完成') break print('共收集到' + username + '的' + str(cnt) + '次提交,正在发送...') problem_set = set() problem_solved_set = set() submit_cnt = 0 submit_solved_cnt = 0 for item in user_result: if item.problem not in problem_set: problem_set.add(item.problem) if item.result == '正确' and item.problem not in problem_solved_set: problem_solved_set.add(item.problem) submit_cnt += 1 if item.result == '正确': submit_solved_cnt += 1 to_send: str = "" if submit_cnt == 0: to_send = "info_fail" else: pos = name_user.find(' ') user = name_user[:pos] name = name_user[pos:] to_send = "info:" + user + "!#@%!" + name + \ "!#@%!"+"完成题目 "+str(len(problem_solved_set))+'/'+str(len(problem_set))+"!#@%!" + \ "提交记录 "+str(submit_solved_cnt)+"/"+str(submit_cnt) + \ "({:.2f}%)".format(submit_solved_cnt / submit_cnt*100) try: server_utils.send_msg(sock, to_send) print('信息发送成功!') # 存储用户数据 user_dict[username] = user_result np.save('user_dict.npy', user_dict) user_latest_submit[username] = cur_latest_code np.save('user_latest.npy', user_latest_submit) print('保存用户信息成功!') except Exception as err: print(err) print('信息发送异常!') except Exception as err: print(err) print('信息服务出现异常,可能无此用户!') try: server_utils.send_msg(sock, 'info_fail') print('信息发送成功!') except: print('信息发送异常!')
def get_user_info(username: str, sock: socket.socket): try: # Clear Browser Cookies browser.delete_all_cookies() user_result = [] if user_dict.get(username) is not None: user_result = user_dict.get(username) latest_code = user_latest_submit.get(username) cur_latest_code: str = None print('开始获取' + username + '的用户信息') browser.get('https://acm.sjtu.edu.cn/OnlineJudge/status?owner=' + username + '&problem=&language=0&verdict=0') name_user: str = browser.find_element( By.XPATH, '//*[@id="status"]/tbody/tr[1]/td[2]').text # Check if the first fetch. first_fetch = 1 cnt = 0 while True: try: flag = False for i in range(first_fetch, 16): cur_code = browser.find_element( By.XPATH, '//*[@id="status"]/tbody/tr[' + str(i) + ']/td[1]/a').text if first_fetch and i == 1: cur_latest_code = cur_code if cur_code == latest_code: flag = True break cur_pro = browser.find_element( By.XPATH, '//*[@id="status"]/tbody/tr[' + str(i) + ']/td[3]/a[1]').text cur_result = browser.find_element( By.XPATH, '//*[@id="status"]/tbody/tr[' + str(i) + ']/td[4]/span').text user_result.append(info_type(cur_code, cur_pro, cur_result)) cnt += 1 if flag: break if first_fetch == 1: first_fetch = 2 # Move to another page. browser.find_element( By.XPATH, '//*[@id="status"]/tbody/tr[15]/td[1]/a').click() except Exception as err: print('结束,信息获取完成') break print('共收集到' + username + '的' + str(cnt) + '次提交,正在发送...') problem_set = set() problem_solved_set = set() submit_cnt = 0 submit_solved_cnt = 0 for item in user_result: if item.problem not in problem_set: problem_set.add(item.problem) if item.result == '正确' and item.problem not in problem_solved_set: problem_solved_set.add(item.problem) submit_cnt += 1 if item.result == '正确': submit_solved_cnt += 1 to_send: str = "" if submit_cnt == 0: to_send = "info_fail" else: pos = name_user.find(' ') user = name_user[:pos] name = name_user[pos:] to_send = "info:" + user + "!#@%!" + name + \ "!#@%!"+"完成题目 "+str(len(problem_solved_set))+'/'+str(len(problem_set))+"!#@%!" + \ "提交记录 "+str(submit_solved_cnt)+"/"+str(submit_cnt) + \ "({:.2f}%)".format(submit_solved_cnt / submit_cnt*100) try: server_utils.send_msg(sock, to_send) print('信息发送成功!') # 存储用户数据 user_dict[username] = user_result np.save('user_dict.npy', user_dict) user_latest_submit[username] = cur_latest_code np.save('user_latest.npy', user_latest_submit) print('保存用户信息成功!') except: print('信息发送异常!') except Exception as err: print(err) print('信息服务出现异常,可能无此用户!') try: server_utils.send_msg(sock, 'info_fail') print('信息发送成功!') except: print('信息发送异常!')
def submit_code(username: str, password: str, problem_to_solve: str, sock: socket.socket): global browser try: # Clear Browser Cookies browser.delete_all_cookies() print("正在获取代码......") browser.get('https://gitee.com/xz2000/SJTU-OJ/raw/master/Code/' + problem_to_solve + '.cpp') print('代码获取成功!') code_to_input: str = browser.find_element_by_xpath( '/html/body/pre').text print(code_to_input) if (code_to_input.startswith("404:")): print('代码获取失败,正在向用户发送信息...') logging.error('未找到代码') try: server_utils.send_msg(sock, "toast:抱歉,服务器没有当前题目的代码") print('发送信息成功!') except: print('向用户发送信息失败!') return browser.get("https://acm.sjtu.edu.cn/OnlineJudge") input_username = browser.find_element(By.NAME, 'username') input_password = browser.find_element(By.NAME, 'password') btn_login = browser.find_element(By.NAME, 'action') actions = ActionChains(browser) actions.send_keys_to_element(input_username, username) actions.send_keys_to_element(input_password, password) actions.click(btn_login) actions.perform() try: browser.get("https://acm.sjtu.edu.cn/OnlineJudge/submit") input_problem = browser.find_element(By.NAME, 'problem') input_code = browser.find_element(By.NAME, 'code') btn_submit = browser.find_element( By.XPATH, '//*[@id="wrap"]/div/form/fieldset/div[4]/button') except Exception as err: logging.error('提交出现异常(账户密码错误)' + err.__str__()) print('提交出现异常,账户密码错误') try: server_utils.send_msg(sock, "toast:您的密码有误,请检查您的账户密码") print('发送信息成功!') except: print('向用户发送信息失败!') return actions = ActionChains(browser) actions.send_keys_to_element(input_problem, problem_to_solve) actions.send_keys_to_element(input_code, code_to_input) actions.click(btn_submit) actions.perform() print('提交已经成功,正在向用户发送信息') logging.info('代码获取成功') try: server_utils.send_msg(sock, "toast:提交已经成功,请关注评测结果") print('发送信息成功!') except: print('向用户发送信息失败!') except Exception as err: print('抱歉!出现错误') print('正在向用户发送信息...') logging.error('代码服务异常' + err.__str__()) try: server_utils.send_msg(sock, "toast:代码提交或拉取服务出现错误,请稍后再试") print('发送信息成功!') except: print('向用户发送信息失败!') # Restart Chrome # browser.close() option = webdriver.ChromeOptions() option.add_argument('--headless') option.add_argument('--no-sandbox') browser = webdriver.Chrome(options=option) logging.info('提交服务已经重启')