def get_solutions(sort, order, problem_code, page, language, result, username): url = f'/status/{problem_code.upper()}' resp = request(url=url) if resp.status_code != 200: return [{'code': 503}] params = build_request_params(resp.html, language, result, username, page) resp = request(url=url, params=params) if resp.status_code == 200: if problem_code in resp.url: resp_html = resp.html solution_table = resp_html.find('table')[2] page_info = resp_html.find(PAGE_INFO_CLASS, first=True) data_rows = html_to_list(solution_table) for row in data_rows: # remove view solution column del row[-1] # format result column row[3] = ' '.join(row[3].split('\n')) resp = {'data_type': 'table', 'data': data_rows} if page_info: resp['extra'] = f'\nPage: {page_info.text}' return [resp] else: return [{'code': 404, 'data': INVALID_PROBLEM_CODE_MSG}] return [{'code': 503}]
def get_team(name): if not name: return [] resp = request(url=get_team_url(name)) if resp.status_code == 200: resp_html = resp.html tables = resp_html.find('table') header = tables[1].text.strip() team_info = tables[2].text.strip() team_info = team_info.replace(':\n', ': ') team_info_list = team_info.split('\n') basic_info = "\n".join(team_info_list[:2]) contests_info = "\n".join( [format_contest(item) for item in team_info_list[2:-1]]) problems_solved_table = html_to_list(tables[-1]) team_details = "\n".join([ '', header, '', basic_info, contests_info, '', 'Problems Successfully Solved:', '' ]) return [{ 'data': team_details }, { 'data': problems_solved_table, "data_type": "table", "is_pager": False }] elif resp.status_code == 404: return [{'code': 404, 'data': 'Team not found.'}] return [{'code': 503}]
def test_html_to_list_valid_html(self): """Should convert requests_html.HTML instance to `list`""" html = HTML(html=" \ <tr><th>A</th><th>V</th></tr> \ <tr><td>a1</td><td>v1</td></tr> \ <tr><td>a2</td><td>v2</td></tr> \ ") self.assertEqual(html_to_list(html), [['A', 'V'], ['a1', 'v1'], ['a2', 'v2']])
def get_contests(show_past): resp = request(url='/contests') if resp.status_code == 200: tables = resp.html.find('table') labels = ['Present', 'Future'] if show_past: labels = ['Past'] tables = [tables[0], tables[-1]] resps = [] for idx, label in enumerate(labels): resps += [ {'data': style_text(f'{label} Contests:\n', 'BOLD')}, {'data': html_to_list(tables[idx + 1]), 'data_type': 'table'} ] return resps return [{'code': 503}]
def submit_problem(problem_code, solution_file, language): url = f'/submit/{problem_code}' get_resp = request(url=url) if not is_logged_in(get_resp): return [{"code": 401, "data": "This session has been disconnected. Login again."}] if get_resp.status_code == 200: rhtml = get_resp.html form_token = get_form_token(rhtml) language_code = get_language_code(rhtml, language) csrf_token = get_csrf_token(rhtml, CSRF_TOKEN_INPUT_ID) if language_code is None: return [{'code': 400, 'data': 'Invalid language.'}] else: return [{'code': 503}] try: solution_file_obj = open(solution_file) except IOError: return [{'data': 'Solution file not found.', 'code': 400}] data = { 'language': language_code, 'problem_code': problem_code, 'form_id': PROBLEM_SUB_DATA_FORM_ID, 'form_token': form_token } files = {'files[sourcefile]': solution_file_obj} post_resp = request(method='POST', url=url, data=data, files=files) if post_resp.status_code == 200: print(style_text('Submitting code...\n', 'BLUE')) status_code = post_resp.url.split('/')[-1] url = f'/get_submission_status/{status_code}' print(style_text('Fetching results...\n', 'BLUE')) max_tries = 3 num_tries = 0 while True: resp = request(url=url, token=csrf_token) num_tries += 1 try: status_json = resp.json() except ValueError: if num_tries == max_tries: return [{'code': 503}] continue result_code = status_json['result_code'] if result_code != 'wait': data = '' if result_code == 'compile': error_msg = get_compilation_error(status_code) data = style_text(f'Compilation error.\n{error_msg}', 'FAIL') elif result_code == 'runtime': data = style_text(f"Runtime error. {status_json.get('signal', '')}\n", 'FAIL') elif result_code == 'wrong': data = style_text('Wrong answer\n', 'FAIL') elif result_code == 'accepted': data = 'Correct answer\n' resps = [{'data': data}] status_table = get_status_table(status_code) if status_table: resps.append({'data_type': 'table', 'data': html_to_list(status_table)}) return resps else: print(style_text('Waiting...\n', 'BLUE')) return [{'code': 503}]
def search_problems(sort, order, search_type): url = f'/problems/{search_type.lower()}' resp = request(url=url) if resp.status_code == 200: return [{'data_type': 'table', 'data': html_to_list(resp.html.find('table')[1])}] return [{"code": 503}]
def test_html_to_list_none_html(self): """Should return empty list when no html is provided""" self.assertTrue(len(html_to_list(None)) == 0)