def download_sample_cases( self, session: Optional[requests.Session] = None ) -> List[onlinejudge.type.TestCase]: session = session or utils.new_default_session() # get url = self.get_url(contests=False) + '/file/statement/samples.zip' resp = utils.request('GET', url, session=session, raise_for_status=False) if resp.status_code == 404: log.warning('samples.zip not found') log.info( 'this 404 happens in both cases: 1. no sample cases as intended; 2. just an error' ) return [] resp.raise_for_status() # parse with zipfile.ZipFile(io.BytesIO(resp.content)) as fh: samples = [] # type: List[TestCase] for filename in sorted(fh.namelist()): log.debug('filename: %s', filename) if filename.endswith('.in'): inpath = filename outpath = filename[:-3] + '.ans' indata = fh.read(inpath).decode() outdata = fh.read(outpath).decode() samples += [ TestCase(LabeledString(inpath, indata), LabeledString(outpath, outdata)) ] return samples
def download_system_cases(self, session: Optional[requests.Session] = None) -> List[TestCase]: session = session or utils.new_default_session() # get # example: https://www.hackerrank.com/rest/contests/hourrank-1/challenges/beautiful-array/download_testcases url = 'https://www.hackerrank.com/rest/contests/{}/challenges/{}/download_testcases'.format(self.contest_slug, self.challenge_slug) resp = utils.request('GET', url, session=session, raise_for_status=False) if resp.status_code != 200: log.error('response: %s', resp.content.decode()) return [] # parse with zipfile.ZipFile(io.BytesIO(resp.content)) as fh: # list names names = [] # type: List[str] pattern = re.compile(r'(in|out)put/\1put(\d+).txt') for filename in sorted(fh.namelist()): # "input" < "output" if filename.endswith('/'): continue log.debug('filename: %s', filename) m = pattern.match(filename) assert m if m.group(1) == 'in': names += [m.group(2)] # zip samples samples = [] # type: List[TestCase] for name in names: inpath = 'input/input{}.txt'.format(name) outpath = 'output/output{}.txt'.format(name) indata = fh.read(inpath).decode() outdata = fh.read(outpath).decode() samples += [TestCase(LabeledString(inpath, indata), LabeledString(outpath, outdata))] return samples
def add(self, s: str, name: str = '') -> None: if self.dangling is None: if re.search('output', name, re.IGNORECASE) or re.search( '出力', name): log.warning('strange name for input string: %s', name) self.dangling = LabeledString(name, s) else: if re.search('input', name, re.IGNORECASE) or re.search( '入力', name): if not (re.search('output', name, re.IGNORECASE) or re.search('出力', name) ): # to ignore titles like "Output for Sample Input 1" log.warning('strange name for output string: %s', name) self.data += [TestCase(self.dangling, LabeledString(name, s))] self.dangling = None
def download_system_cases( self, session: Optional[requests.Session] = None) -> List[TestCase]: session = session or utils.new_default_session() # get url = 'https://yukicoder.me/problems/no/{}/testcase.zip'.format( self.problem_no) resp = utils.request('GET', url, session=session) # parse basenames = collections.defaultdict( dict) # type: Dict[str, Dict[str, LabeledString]] with zipfile.ZipFile(io.BytesIO(resp.content)) as fh: for filename in sorted(fh.namelist()): # "test_in" < "test_out" dirname = os.path.dirname(filename) basename = os.path.basename(filename) kind = {'test_in': 'input', 'test_out': 'output'}[dirname] content = fh.read(filename).decode() name = basename if os.path.splitext( name)[1] == '.in': # ".in" extension is confusing name = os.path.splitext(name)[0] basenames[basename][kind] = LabeledString(name, content) samples = [] # type: List[TestCase] for basename in sorted(basenames.keys()): data = basenames[basename] if 'input' not in data or 'output' not in data or len(data) != 2: log.error('dangling sample found: %s', str(data)) else: samples += [TestCase(data['input'], data['output'])] return samples
def download_sample_cases( self, session: Optional[requests.Session] = None) -> List[TestCase]: session = session or utils.new_default_session() # get samples via the official API # reference: http://developers.u-aizu.ac.jp/api?key=judgedat%2Ftestcases%2Fsamples%2F%7BproblemId%7D_GET url = 'https://judgedat.u-aizu.ac.jp/testcases/samples/{}'.format( self.problem_id) resp = utils.request('GET', url, session=session) samples = [] # type: List[TestCase] for sample in json.loads(resp.content.decode(resp.encoding)): samples += [ TestCase( LabeledString(str(sample['serial']), sample['in']), LabeledString(str(sample['serial']), sample['out']), ) ] return samples
def download_sample_cases( self, session: Optional[requests.Session] = None ) -> List[onlinejudge.type.TestCase]: session = session or utils.new_default_session() # get resp = utils.request('GET', self.get_url(), session=session) # parse soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser) in_pre, out_pre = soup.find_all('pre', class_='sio') in_p = in_pre.find_previous_sibling('p', class_='pst') out_p = out_pre.find_previous_sibling('p', class_='pst') log.debug('pre (in): %s', in_pre.contents) log.debug('pre (out): %s', out_pre.contents) assert in_p.text.strip() == 'Sample Input' assert out_p.text.strip() == 'Sample Output' assert len(in_pre.contents) == len(out_pre.contents) samples = [] # type: List[TestCase] if len(in_pre.contents) == 1: assert isinstance(in_pre.contents[0], bs4.NavigableString) assert isinstance(out_pre.contents[0], bs4.NavigableString) samples += [ TestCase( LabeledString(in_p.text.strip(), in_pre.text + '\r\n'), LabeledString(out_p.text.strip(), out_pre.text + '\r\n')) ] else: assert len(in_pre.contents) % 2 == 0 for i in range(len(in_pre.contents) // 2): in_name = in_pre.contents[2 * i] in_data = in_pre.contents[2 * i + 1] out_name = out_pre.contents[2 * i] out_data = out_pre.contents[2 * i + 1] assert in_name.name == 'b' assert isinstance(in_data, bs4.NavigableString) assert out_name.name == 'b' assert isinstance(out_data, bs4.NavigableString) indata = LabeledString(in_name.text.strip(), str(in_data).strip() + '\r\n') outdata = LabeledString(out_name.text.strip(), str(out_data).strip() + '\r\n') samples += [TestCase(indata, outdata)] return samples
def download_system_cases( self, session: Optional[requests.Session] = None) -> List[TestCase]: session = session or utils.new_default_session() # get header # reference: http://developers.u-aizu.ac.jp/api?key=judgedat%2Ftestcases%2F%7BproblemId%7D%2Fheader_GET url = 'https://judgedat.u-aizu.ac.jp/testcases/{}/header'.format( self.problem_id) resp = utils.request('GET', url, session=session) header = json.loads(resp.content.decode(resp.encoding)) # get testcases via the official API testcases = [] # type: List[TestCase] for header in header['headers']: # reference: http://developers.u-aizu.ac.jp/api?key=judgedat%2Ftestcases%2F%7BproblemId%7D%2F%7Bserial%7D_GET url = 'https://judgedat.u-aizu.ac.jp/testcases/{}/{}'.format( self.problem_id, header['serial']) resp = utils.request('GET', url, session=session) testcase = json.loads(resp.content.decode(resp.encoding)) skipped = False for type in ('in', 'out'): if testcase[type].endswith( '..... (terminated because of the limitation)\n'): log.error( 'AOJ API says: terminated because of the limitation') skipped = True if skipped: log.warning("skipped due to the limitation of AOJ API") continue testcases += [ TestCase( LabeledString(header['name'], testcase['in']), LabeledString(header['name'], testcase['out']), ) ] return testcases
def download_sample_cases( self, session: Optional[requests.Session] = None) -> List[TestCase]: session = session or utils.new_default_session() base_url = self.get_url() # get csrftoken resp = utils.request('GET', base_url, session=session) csrftoken = None for cookie in session.cookies: if cookie.name == 'csrftoken' and cookie.domain == 'csacademy.com': # type: ignore csrftoken = cookie.value # type: ignore if csrftoken is None: log.error('csrftoken is not found') return [] # get config headers = { 'x-csrftoken': csrftoken, 'x-requested-with': 'XMLHttpRequest', } contest_url = 'https://csacademy.com/contest/{}/'.format( self.contest_name) resp = utils.request('GET', contest_url, session=session, headers=headers) # parse config assert resp.encoding is None config = json.loads( resp.content.decode() ) # NOTE: Should I memoize this? Is the CSAcademyRound class required? task_config = None for it in config['state']['contesttask']: if it['name'] == self.task_name: task_config = it if task_config is None: log.error('no such task: %s', self.task_name) return [] # get get_contest_task_url = 'https://csacademy.com/contest/get_contest_task/' payload = {'contestTaskId': (None, str(task_config['id']))} headers = { 'x-csrftoken': csrftoken, 'x-requested-with': 'XMLHttpRequest', 'Referer': base_url, } resp = utils.request('POST', get_contest_task_url, session=session, files=payload, headers=headers) # parse assert resp.encoding is None contest_task = json.loads( resp.content.decode()) # NOTE: Should I memoize this? if contest_task.get('title') == 'Page not found': log.error('something wrong') return [] samples = [] for test_number, example_test in enumerate( contest_task['state']['EvalTask'][0]['exampleTests']): inname = 'Input {}'.format(test_number) outname = 'Output {}'.format(test_number) samples += [ TestCase( LabeledString(inname, example_test['input']), LabeledString(outname, example_test['output']), ) ] return samples