예제 #1
0
    def login(self, get_credentials, session=None):
        driver = session  # rename
        assert isinstance(driver,
                          selenium.webdriver.remote.webdriver.WebDriver)

        # go to the login page
        url = 'https://accounts.topcoder.com/member'
        driver.get(url)
        log.status('wait for redirect...')
        time.sleep(3)
        if driver.current_url != url:
            log.info('You have already logged in.')
            return True

        # type credentials and click login
        username, password = get_credentials()
        driver.find_element_by_xpath(
            '//form[@name="vm.loginForm"]//input[@name="username"]').send_keys(
                username)
        driver.find_element_by_xpath(
            '//form[@name="vm.loginForm"]//input[@id="current-password-input"]'
        ).send_keys(password)
        driver.find_element_by_xpath(
            '//form[@name="vm.loginForm"]//button[@type="submit" and text()="Log In"]'
        ).click()

        # wait a minute
        log.info('Logging in...')
        time.sleep(6)
        if driver.current_url != url:
            log.success('Success')
            return True
        else:
            log.failure('Failure')
            return False
예제 #2
0
 def submit(self, code, language, session=None):
     assert language in self.get_language_dict(session=session)
     session = session or requests.Session()
     url = self.get_url() + '/submit'
     # get
     log.status('GET: %s', url)
     resp = session.get(url)
     log.status(utils.describe_status_code(resp.status_code))
     resp.raise_for_status()
     # parse
     soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser)
     form = soup.find('form', action=re.compile(r'/submit$'))
     if not form:
         log.error('form not found')
         return None
     log.debug('form: %s', str(form))
     # post
     form = utils.FormSender(form, url=resp.url)
     if False:
         form.set('source', code)
     else:
         form.set_file('file', ('source', code))
     form.set('lang', language)
     resp = form.request(session=session)
     resp.raise_for_status()
     # result
     if '/submissions/' in resp.url:
         log.success('success: result: %s', resp.url)
         return resp.url
     else:
         log.failure('failure')
         return None
예제 #3
0
 def submit(self, code, language, session=None):
     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)
     csrftoken = soup.find("meta", attrs={
         "name": "csrf-token"
     }).attrs["content"]
     # post
     url = "https://www.hackerrank.com/rest/contests/{}/challenges/{}/submissions".format(
         self.contest_slug, self.challenge_slug)
     payload = {"code": code, "language": language}
     log.debug("payload: %s", payload)
     resp = utils.request(
         "POST",
         url,
         session=session,
         json=payload,
         headers={"X-CSRF-Token": csrftoken},
     )
     # parse
     it = json.loads(resp.content.decode())
     log.debug("json: %s", it)
     if not it["status"]:
         log.failure("Submit Code: failed")
         return None
     model_id = it["model"]["id"]
     url = self.get_url().rstrip("/") + "/submissions/code/{}".format(
         model_id)
     log.success("success: result: %s", url)
     return onlinejudge.submission.CompatibilitySubmission(url,
                                                           problem=self)
예제 #4
0
def generate_scanner(args):
    if not args.silent:
        log.warning("This feature is " + log.red("experimental") + ".")
    if args.silent:
        for handler in log.logger.handlers:
            log.removeHandler(handler)
    problem = onlinejudge.dispatch.problem_from_url(args.url)
    if problem is None:
        sys.exit(1)
    with utils.with_cookiejar(utils.new_default_session(), path=args.cookie) as sess:
        it = problem.get_input_format(session=sess)
    if not it:
        log.error("input format not found")
        sys.exit(1)
    try:
        log.debug("original data: %s", repr(it))
        it = list(tokenize(it))
        log.debug("tokenized: %s", str(it))
        it = list(parse(it))
        log.debug("parsed: %s", str(it))
        it = postprocess(it)
        log.debug("postprocessed: %s", str(it))
        it = export(it, use_scanf=args.scanf, repeat_macro=args.repeat_macro)
        log.debug("result: %s", repr(it))
    except:
        log.error("something wrong")
        raise
    log.success("success:")
    print(log.bold(it.rstrip()))  # to stdout
예제 #5
0
 def login_with_github(
         self,
         get_credentials: onlinejudge.type.CredentialsProvider,
         session: Optional[requests.Session] = None) -> bool:
     session = session or utils.new_default_session()
     url = 'https://yukicoder.me/auth/github'
     # get
     resp = utils.request('GET', url, session=session)
     if urllib.parse.urlparse(resp.url).hostname == 'yukicoder.me':
         log.info('You have already signed in.')
         return True
     # redirect to github.com
     # parse
     soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                              utils.html_parser)
     form = soup.find('form')
     if not form:
         log.error('form not found')
         log.info('Did you logged in?')
         return False
     log.debug('form: %s', str(form))
     # post
     username, password = get_credentials()
     form = utils.FormSender(form, url=resp.url)
     form.set('login', username)
     form.set('password', password)
     resp = form.request(session)
     resp.raise_for_status()
     if urllib.parse.urlparse(resp.url).hostname == 'yukicoder.me':
         log.success('You signed in.')
         return True
     else:
         log.failure('You failed to sign in. Wrong user ID or password.')
         return False
예제 #6
0
def generate_output(args: 'argparse.Namespace') -> None:
    if not args.test:
        args.test = cutils.glob_with_format(args.directory, args.format) # by default
    if args.ignore_backup:
        args.test = cutils.drop_backup_or_hidden_files(args.test)
    tests = cutils.construct_relationship_of_files(args.test, args.directory, args.format)
    for name, it in sorted(tests.items()):
        log.emit('')
        log.info('%s', name)
        if 'out' in it:
            log.info('output file already exists.')
            log.info('skipped.')
            continue
        with it['in'].open() as inf:
            begin = time.perf_counter()
            answer, proc = utils.exec_command(args.command, shell=True, stdin=inf)
            end = time.perf_counter()
            log.status('time: %f sec', end - begin)
        if proc.returncode != 0:
            log.failure(log.red('RE') + ': return code %d', proc.returncode)
            log.info('skipped.')
            continue
        log.emit(log.bold(answer.decode().rstrip()))
        match_result = cutils.match_with_format(args.directory, args.format, it['in'])  # type: Optional[Match[Any]]
        if match_result is not None:
            matched_name = match_result.groupdict()['name']  # type: str
        else:
            assert False
        path = cutils.path_from_format(args.directory, args.format, name=matched_name, ext='out')
        if not path.parent.is_dir():
            os.makedirs(str(path.parent), exist_ok=True)
        with path.open('wb') as fh:
            fh.write(answer)
        log.success('saved to: %s', path)
예제 #7
0
 def login(self, get_credentials, session=None):
     session = session or requests.Session()
     url = 'http://codeforces.com/enter'
     # get
     log.status('GET: %s', url)
     resp = session.get(url)
     log.status(utils.describe_status_code(resp.status_code))
     resp.raise_for_status()
     if resp.url != url:  # redirected
         log.info('You have already signed in.')
         return True
     # parse
     soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                              utils.html_parser)
     form = soup.find('form', id='enterForm')
     log.debug('form: %s', str(form))
     username, password = get_credentials()
     form = utils.FormSender(form, url=resp.url)
     form.set('handle', username)
     form.set('password', password)
     form.set('remember', 'on')
     # post
     resp = form.request(session)
     resp.raise_for_status()
     if resp.url != url:  # redirected
         log.success('Welcome, %s.', username)
         return True
     else:
         log.failure('Invalid handle or password.')
         return False
def generate_scanner(args: 'argparse.Namespace') -> None:
    if not args.silent:
        log.warning('This feature is ' + log.red('experimental') + '.')
    if args.silent:
        for handler in log.logger.handlers:
            log.removeHandler(handler)
    problem = onlinejudge.dispatch.problem_from_url(args.url)
    if problem is None:
        sys.exit(1)
    with utils.with_cookiejar(utils.new_default_session(),
                              path=args.cookie) as sess:
        it = problem.get_input_format(session=sess)  # type: Any
    if not it:
        log.error('input format not found')
        sys.exit(1)
    try:
        log.debug('original data: %s', repr(it))
        it = list(tokenize(it))
        log.debug('tokenized: %s', str(it))
        it = list(parse(it))
        log.debug('parsed: %s', str(it))
        it = postprocess(it)
        log.debug('postprocessed: %s', str(it))
        it = export(it, use_scanf=args.scanf, repeat_macro=args.repeat_macro)
        log.debug('result: %s', repr(it))
    except:
        log.error('something wrong')
        raise
    log.success('success:')
    print(log.bold(it.rstrip()))  # to stdout
예제 #9
0
def generate_output(args):
    if not args.test:
        args.test = glob_with_format(args.format) # by default
    if args.ignore_backup:
        args.test = drop_backup_or_hidden_files(args.test)
    tests = construct_relationship_of_files(args.test, args.format)
    for name, it in sorted(tests.items()):
        log.emit('')
        log.info('%s', name)
        if 'out' in it:
            log.info('output file already exists.')
            log.info('skipped.')
            continue
        with open(it['in']) as inf:
            begin = time.perf_counter()
            answer, proc = utils.exec_command(args.command, shell=args.shell, stdin=inf)
            end = time.perf_counter()
            log.status('time: %f sec', end - begin)
        if proc.returncode != 0:
            log.failure(log.red('RE') + ': return code %d', proc.returncode)
            log.info('skipped.')
            continue
        log.emit(log.bold(answer.decode().rstrip()))
        path = path_from_format(args.format, match_with_format(args.format, it['in']).groupdict()['name'], 'out')
        with open(path, 'w') as fh:
            fh.buffer.write(answer)
        log.success('saved to: %s', path)
예제 #10
0
def split_input(args):
    with open(args.input) as fh:
        inf = fh.read()
    if args.footer == split_input_auto_footer:
        args.footer = inf.splitlines(keepends=True)[-1]
    with subprocess.Popen(args.command,
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=sys.stderr) as proc:
        index = 0
        acc = ''
        for line in inf.splitlines(keepends=True):
            if args.ignore:
                args.ignore -= 1
            else:
                acc += line
            proc.stdin.write(line.encode())
            proc.stdin.flush()
            time.sleep(args.time)
            if non_block_read(proc.stdout):  # if output exists
                index += 1
                path = utils.parcentformat(args.output, {'i': str(index)})
                log.info('case found: %d', index)
                if args.header:
                    acc = args.header + acc
                if args.footer:
                    acc = acc + args.footer
                log.emit(log.bold(acc))
                with open(path, 'w') as fh:
                    fh.write(acc)
                log.success('saved to: %s', path)
                acc = ''
                while non_block_read(proc.stdout):  # consume all
                    pass
예제 #11
0
 def login_with_github(self, get_credentials, session=None):
     session = session or utils.new_default_session()
     url = "https://yukicoder.me/auth/github"
     # get
     resp = utils.request("GET", url, session=session)
     if urllib.parse.urlparse(resp.url).hostname == "yukicoder.me":
         log.info("You have already signed in.")
         return True
     # redirect to github.com
     # parse
     soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser)
     form = soup.find("form")
     if not form:
         log.error("form not found")
         log.info("Did you logged in?")
         return False
     log.debug("form: %s", str(form))
     # post
     username, password = get_credentials()
     form = utils.FormSender(form, url=resp.url)
     form.set("login", username)
     form.set("password", password)
     resp = form.request(session)
     resp.raise_for_status()
     if urllib.parse.urlparse(resp.url).hostname == "yukicoder.me":
         log.success("You signed in.")
         return True
     else:
         log.failure("You failed to sign in. Wrong user ID or password.")
         return False
예제 #12
0
 def login(self,
           get_credentials: onlinejudge.service.CredentialsProvider,
           session: Optional[requests.Session] = None) -> bool:
     session = session or utils.new_default_session()
     url = 'https://codeforces.com/enter'
     # get
     resp = utils.request('GET', url, session=session)
     if resp.url != url:  # redirected
         log.info('You have already signed in.')
         return True
     # parse
     soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                              utils.html_parser)
     form = soup.find('form', id='enterForm')
     log.debug('form: %s', str(form))
     username, password = get_credentials()
     form = utils.FormSender(form, url=resp.url)
     form.set('handleOrEmail', username)
     form.set('password', password)
     form.set('remember', 'on')
     # post
     resp = form.request(session)
     resp.raise_for_status()
     if resp.url != url:  # redirected
         log.success('Welcome, %s.', username)
         return True
     else:
         log.failure('Invalid handle or password.')
         return False
예제 #13
0
 def login(self, get_credentials: onlinejudge.type.CredentialsProvider, session: Optional[requests.Session] = None) -> bool:
     session = session or utils.new_default_session()
     url = 'https://www.hackerrank.com/auth/login'
     # get
     resp = utils.request('GET', url, session=session)
     if resp.url != url:
         log.debug('redirected: %s', resp.url)
         log.info('You have already signed in.')
         return True
     # parse
     soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser)
     csrftoken = soup.find('meta', attrs={'name': 'csrf-token'}).attrs['content']
     tag = soup.find('input', attrs={'name': 'username'})
     while tag.name != 'form':
         tag = tag.parent
     form = tag
     # post
     username, password = get_credentials()
     form = utils.FormSender(form, url=resp.url)
     form.set('login', username)
     form.set('password', password)
     form.set('remember_me', 'true')
     form.set('fallback', 'true')
     resp = form.request(session, method='POST', action='/rest/auth/login', headers={'X-CSRF-Token': csrftoken})
     resp.raise_for_status()
     log.debug('redirected: %s', resp.url)
     # result
     if '/auth' not in resp.url:
         log.success('You signed in.')
         return True
     else:
         log.failure('You failed to sign in. Wrong user ID or password.')
         return False
예제 #14
0
 def login(self, get_credentials, session=None):
     session = session or utils.new_default_session()
     url = "http://codeforces.com/enter"
     # get
     resp = utils.request("GET", url, session=session)
     if resp.url != url:  # redirected
         log.info("You have already signed in.")
         return True
     # parse
     soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                              utils.html_parser)
     form = soup.find("form", id="enterForm")
     log.debug("form: %s", str(form))
     username, password = get_credentials()
     form = utils.FormSender(form, url=resp.url)
     form.set("handleOrEmail", username)
     form.set("password", password)
     form.set("remember", "on")
     # post
     resp = form.request(session)
     resp.raise_for_status()
     if resp.url != url:  # redirected
         log.success("Welcome, %s.", username)
         return True
     else:
         log.failure("Invalid handle or password.")
         return False
예제 #15
0
 def submit(self, code, language, session=None):
     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)
     csrftoken = soup.find('meta', attrs={
         'name': 'csrf-token'
     }).attrs['content']
     # post
     url = 'https://www.hackerrank.com/rest/contests/{}/challenges/{}/submissions'.format(
         self.contest_slug, self.challenge_slug)
     payload = {'code': code, 'language': language}
     log.debug('payload: %s', payload)
     resp = utils.request('POST',
                          url,
                          session=session,
                          json=payload,
                          headers={'X-CSRF-Token': csrftoken})
     # parse
     it = json.loads(resp.content.decode())
     log.debug('json: %s', it)
     if not it['status']:
         log.failure('Submit Code: failed')
         return None
     model_id = it['model']['id']
     url = self.get_url().rstrip('/') + '/submissions/code/{}'.format(
         model_id)
     log.success('success: result: %s', url)
     return onlinejudge.submission.CompatibilitySubmission(url,
                                                           problem=self)
예제 #16
0
 def submit_code(self, code: bytes, language: str, session: Optional['requests.Session'] = None) -> onlinejudge.type.Submission:  # or SubmissionError
     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)
     form = soup.find('form', class_='submitForm')
     if form is None:
         log.error('not logged in')
         raise SubmissionError
     log.debug('form: %s', str(form))
     # make data
     form = utils.FormSender(form, url=resp.url)
     form.set('programTypeId', language)
     form.set_file('sourceFile', 'code', code)
     resp = form.request(session=session)
     resp.raise_for_status()
     # result
     if resp.url.endswith('/my'):
         # example: https://codeforces.com/contest/598/my
         log.success('success: result: %s', resp.url)
         return onlinejudge.type.DummySubmission(resp.url)
     else:
         log.failure('failure')
         log.debug('redirected to %s', resp.url)
         # parse error messages
         soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser)
         for span in soup.findAll('span', class_='error'):
             log.warning('Codeforces says: "%s"', span.string)
         raise SubmissionError
예제 #17
0
    def login(self,
              get_credentials: onlinejudge.type.CredentialsProvider,
              session: Optional[requests.Session] = None) -> bool:
        session = session or utils.new_default_session()
        url = 'https://toph.co/login'
        # get
        resp = utils.request('GET', url, session=session)
        if resp.url != url:  # redirected
            log.info('You are already logged in.')
            return True
        # parse
        soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                                 utils.html_parser)
        form = soup.find('form', class_='login-form')
        log.debug('form: %s', str(form))
        username, password = get_credentials()
        form[
            'action'] = '/login'  # to avoid KeyError inside form.request method as Toph does not have any defined action
        form = utils.FormSender(form, url=resp.url)
        form.set('handle', username)
        form.set('password', password)
        # post
        resp = form.request(session)
        resp.raise_for_status()

        resp = utils.request(
            'GET', url, session=session
        )  # Toph's Location header is not getting the expected value
        if resp.url != url:
            log.success('Welcome, %s.', username)
            return True
        else:
            log.failure('Invalid handle/email or password.')
            return False
예제 #18
0
    def submit_code(self, code: bytes, language: str, session: Optional[requests.Session] = None, kind: str = 'example') -> onlinejudge.type.Submission:
        assert kind in [ 'example', 'full' ]
        session = session or utils.new_default_session()

        # module=MatchDetails
        url = 'https://community.topcoder.com/tc?module=MatchDetails&rd=%d' % self.rd
        resp = utils.request('GET', url, session=session)
        soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser)
        path = soup.find('a', text='Register/Submit').attrs['href']
        assert path.startswith('/') and 'module=ViewReg' in path

        # module=ViewActiveContests
        url = 'https://community.topcoder.com' + path
        resp = utils.request('GET', url, session=session)
        soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser)
        path = [ tag.attrs['href'] for tag in soup.find_all('a', text='Submit') if ('rd=%d' % self.rd) in tag.attrs['href'] ]
        if len(path) == 0:
            log.error('link to submit not found:  Are you logged in?  Are you registered?  Is the contest running?')
            raise onlinejudge.type.SubmissionError
        assert len(path) == 1
        path = path[0]
        assert path.startswith('/') and 'module=Submit' in path
        query = dict(urllib.parse.parse_qsl(urllib.parse.urlparse(path).query))
        self.cd     = query['cd']
        self.compid = query['compid']

        # module=Submit
        submit_url = 'https://community.topcoder.com' + path
        resp = utils.request('GET', submit_url, session=session)
        soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser)

        # post
        url = 'https://community.topcoder.com/longcontest/'
        language_id = self.get_language_dict(session=session)[language]['value']
        data = {
            'module': 'Submit',
            'rd': self.rd,
            'cd': self.cd,
            'compid': self.compid,
            'Action': 'submit',
            'exOn': { 'example': 'true', 'full': 'false' }[kind],
            'lid': language_id,
            'code': code,
        }
        resp = utils.request('POST', url, session=session, data=data)

        # check if module=SubmitSuccess
        if 'module=SubmitSuccess' in resp.content.decode(resp.encoding):
            url = 'http://community.topcoder.com/longcontest/?module=SubmitSuccess&rd={}&cd={}&compid={}'.format(self.rd, self.cd, self.compid)
            log.success('success: result: %s', url)
            return onlinejudge.type.CompatibilitySubmission(url, self)
        else:
            # module=Submit to get error messages
            resp = utils.request('GET', submit_url, session=session)
            soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser)
            messages = soup.find('textarea', { 'name': 'messages' }).text
            log.failure('%s', messages)
            raise onlinejudge.type.SubmissionError
예제 #19
0
def download(args: 'argparse.Namespace') -> None:
    # prepare values
    problem = onlinejudge.dispatch.problem_from_url(args.url)
    if problem is None:
        sys.exit(1)
    kwargs = {}
    if args.system:
        supported_service_names = ['aoj', 'yukicoder']
        if problem.get_service().get_name() not in supported_service_names:
            log.error('--system for %s is not supported',
                      problem.get_service().get_name())
            sys.exit(1)
        kwargs['is_system'] = True
    if args.format is None:
        if kwargs.get('is_system'):
            if problem.get_service().get_name() == 'yukicoder':
                args.format = '%b.%e'
            else:
                args.format = '%i.%e'
        else:
            args.format = 'sample-%i.%e'

    # get samples from the server
    with utils.with_cookiejar(utils.new_default_session(),
                              path=args.cookie) as sess:
        samples = problem.download(session=sess, **kwargs)  # type: ignore

    # write samples to files
    for i, sample in enumerate(samples):
        log.emit('')
        log.info('sample %d', i)
        for kind in ['input', 'output']:
            ext = kind[:-3]
            data = getattr(sample, kind).data
            name = getattr(sample, kind).name
            table = {}
            table['i'] = str(i + 1)
            table['e'] = ext
            table['n'] = name
            table['b'] = os.path.basename(name)
            table['d'] = os.path.dirname(name)
            path = os.path.join(args.directory,
                                utils.parcentformat(args.format, table))
            log.status('%sput: %s', ext, name)
            log.emit(colorama.Style.BRIGHT + data.rstrip() +
                     colorama.Style.RESET_ALL)
            if args.dry_run:
                continue
            if os.path.exists(path):
                log.warning('file already exists: %s', path)
                if not args.overwrite:
                    log.warning('skipped')
                    continue
            os.makedirs(os.path.dirname(path), exist_ok=True)
            with open(path, 'w') as fh:
                fh.write(data)
            log.success('saved to: %s', path)
예제 #20
0
def download(args):
    # prepare values
    problem = onlinejudge.dispatch.problem_from_url(args.url)
    if problem is None:
        sys.exit(1)
    kwargs = {}
    if args.system:
        supported_service_names = ["aoj", "yukicoder"]
        if problem.get_service().get_name() not in supported_service_names:
            log.error("--system for %s is not supported",
                      problem.get_service().get_name())
            sys.exit(1)
        kwargs["is_system"] = True
    if args.format is None:
        if kwargs.get("is_system"):
            if problem.get_service().get_name() == "yukicoder":
                args.format = "%b.%e"
            else:
                args.format = "%i.%e"
        else:
            args.format = "sample-%i.%e"

    # get samples from the server
    with utils.with_cookiejar(utils.new_default_session(),
                              path=args.cookie) as sess:
        samples = problem.download(session=sess, **kwargs)

    # write samples to files
    for i, sample in enumerate(samples):
        log.emit("")
        log.info("sample %d", i)
        for kind in ["input", "output"]:
            ext = kind[:-3]
            data = sample[kind]["data"]
            name = sample[kind]["name"]
            table = {}
            table["i"] = str(i + 1)
            table["e"] = ext
            table["n"] = name
            table["b"] = os.path.basename(name)
            table["d"] = os.path.dirname(name)
            path = os.path.join(args.directory,
                                utils.parcentformat(args.format, table))
            log.status("%sput: %s", ext, name)
            log.emit(colorama.Style.BRIGHT + data.rstrip() +
                     colorama.Style.RESET_ALL)
            if args.dry_run:
                continue
            if os.path.exists(path):
                log.warning("file already exists: %s", path)
                if not args.overwrite:
                    log.warning("skipped")
                    continue
            os.makedirs(os.path.dirname(path), exist_ok=True)
            with open(path, "w", encoding="utf-8") as fh:
                fh.write(data)
            log.success("saved to: %s", path)
예제 #21
0
 def submit_code(
     self,
     code: bytes,
     language: str,
     session: Optional[requests.Session] = None
 ) -> onlinejudge.type.DummySubmission:
     assert language in self.get_language_dict(session=session)
     session = session or utils.new_default_session()
     # get
     url = 'http://{}.contest.atcoder.jp/submit'.format(
         self.contest_id)  # TODO: use beta.atcoder.jp
     resp = _request('GET', url, session=session)
     msgs = AtCoderService._get_messages_from_cookie(resp.cookies)
     if AtCoderService._report_messages(msgs, unexpected=True):
         raise SubmissionError
     # check whether logged in
     path = utils.normpath(urllib.parse.urlparse(resp.url).path)
     if path.startswith('/login'):
         log.error('not logged in')
         raise SubmissionError
     # parse
     soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                              utils.html_parser)
     form = soup.find('form', action=re.compile(r'^/submit\?task_id='))
     if not form:
         log.error('form not found')
         raise SubmissionError
     log.debug('form: %s', str(form))
     # post
     task_id = self._get_task_id(session=session)
     form = utils.FormSender(form, url=resp.url)
     form.set('task_id', str(task_id))
     form.set('source_code', code)
     form.set('language_id_{}'.format(task_id), language)
     resp = form.request(session=session)
     resp.raise_for_status()
     # result
     msgs = AtCoderService._get_messages_from_cookie(resp.cookies)
     AtCoderService._report_messages(msgs)
     if '/submissions/me' in resp.url:
         # example: https://practice.contest.atcoder.jp/submissions/me#32174
         # CAUTION: this URL is not a URL of the submission
         log.success('success: result: %s', resp.url)
         # NOTE: ignore the returned legacy URL and use beta.atcoder.jp's one
         url = 'https://beta.atcoder.jp/contests/{}/submissions/me'.format(
             self.contest_id)
         return onlinejudge.type.DummySubmission(url)
     else:
         log.failure('failure')
         log.debug('redirected to %s', resp.url)
         raise SubmissionError
예제 #22
0
 def submit(self, code, language, session=None):
     assert language in self.get_language_dict(session=session)
     session = session or utils.new_default_session()
     # get
     url = "http://{}.contest.atcoder.jp/submit".format(
         self.contest_id)  # TODO: use beta.atcoder.jp
     resp = utils.request("GET", url, session=session)
     msgs = AtCoderService._get_messages_from_cookie(resp.cookies)
     if AtCoderService._report_messages(msgs, unexpected=True):
         return None
     # check whether logged in
     path = utils.normpath(urllib.parse.urlparse(resp.url).path)
     if path.startswith("/login"):
         log.error("not logged in")
         return None
     # parse
     soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                              utils.html_parser)
     form = soup.find("form", action=re.compile(r"^/submit\?task_id="))
     if not form:
         log.error("form not found")
         return None
     log.debug("form: %s", str(form))
     # post
     task_id = self._get_task_id(session=session)
     form = utils.FormSender(form, url=resp.url)
     form.set("task_id", str(task_id))
     form.set("source_code", code)
     form.set("language_id_{}".format(task_id), language)
     resp = form.request(session=session)
     resp.raise_for_status()
     # result
     msgs = AtCoderService._get_messages_from_cookie(resp.cookies)
     AtCoderService._report_messages(msgs)
     if "/submissions/me" in resp.url:
         # example: https://practice.contest.atcoder.jp/submissions/me#32174
         # CAUTION: this URL is not a URL of the submission
         log.success("success: result: %s", resp.url)
         # NOTE: ignore the returned legacy URL and use beta.atcoder.jp's one
         url = "https://beta.atcoder.jp/contests/{}/submissions/me".format(
             self.contest_id)
         return onlinejudge.submission.CompatibilitySubmission(url)
     else:
         log.failure("failure")
         return None
예제 #23
0
def download(args):
    problem = onlinejudge.dispatch.problem_from_url(args.url)
    if problem is None:
        sys.exit(1)
    kwargs = {}
    if problem.get_service().get_name() == 'yukicoder':
        for x in args.extra_option:
            if x == 'all':
                kwargs['is_all'] = True
    if args.format is None:
        if problem.get_service().get_name() == 'yukicoder' and kwargs.get(
                'is_all'):
            args.format = 'test/%b.%e'
        else:
            args.format = 'test/sample-%i.%e'
    with utils.session(cookiejar=args.cookie) as sess:
        samples = problem.download(session=sess, **kwargs)
    for i, sample in enumerate(samples):
        log.emit('')
        log.info('sample %d', i)
        for ext, (s, name) in zip(['in', 'out'], sample):
            table = {}
            table['i'] = str(i + 1)
            table['e'] = ext
            table['n'] = name
            table['b'] = os.path.basename(name)
            table['d'] = os.path.dirname(name)
            path = utils.parcentformat(args.format, table)
            log.status('%sput: %s', ext, name)
            log.emit(colorama.Style.BRIGHT + s.rstrip() +
                     colorama.Style.RESET_ALL)
            if args.dry_run:
                continue
            if os.path.exists(path):
                log.warning('file already exists: %s', path)
                if not args.overwrite:
                    log.warning('skipped')
                    continue
            os.makedirs(os.path.dirname(path), exist_ok=True)
            with open(path, 'w') as fh:
                fh.write(s)
            log.success('saved to: %s', path)
예제 #24
0
    def login(self, get_credentials: onlinejudge.type.CredentialsProvider, session: Optional[requests.Session] = None) -> bool:
        session = session or utils.new_default_session()

        # NOTE: you can see this login page with https://community.topcoder.com/longcontest/?module=Submit
        url = 'https://community.topcoder.com/longcontest/'
        username, password = get_credentials()
        data = {
            'nextpage': 'https://www.topcoder.com/',
            'module': 'Login',
            'ha': username,
            'pass': password,
            'rem': 'on',
        }
        resp = utils.request('POST', url, session=session, data=data)

        if 'longcontest' not in resp.url:
            log.success('Success')
            return True
        else:
            log.failure('Failure')
            return False
예제 #25
0
    def login(self, get_credentials, session=None):
        session = session or utils.new_default_session()

        # NOTE: you can see this login page with https://community.topcoder.com/longcontest/?module=Submit
        url = "https://community.topcoder.com/longcontest/"
        username, password = get_credentials()
        data = {
            "nextpage": "https://www.topcoder.com/",
            "module": "Login",
            "ha": username,
            "pass": password,
            "rem": "on",
        }
        resp = utils.request("POST", url, session=session, data=data)

        if "longcontest" not in resp.url:
            log.success("Success")
            return True
        else:
            log.failure("Failure")
            return False
def test_reactive(args):
    with fifo() as (fhr1, fhw1):
        with fifo() as (fhr2, fhw2):
            with subprocess.Popen(args.command,
                                  shell=args.shell,
                                  stdin=fhr2,
                                  stdout=fhw1,
                                  stderr=sys.stderr) as proc1:
                with subprocess.Popen(args.judge,
                                      shell=args.shell,
                                      stdin=fhr1,
                                      stdout=fhw2,
                                      stderr=sys.stderr) as proc2:
                    proc1.communicate()
                    proc2.communicate()
                    if proc1.returncode != 0:
                        log.failure('RE: solution returns %d',
                                    proc1.returncode)
                    if proc2.returncode == 0:
                        log.success('AC')
                    else:
                        log.failure('WA: judge returns %d', proc2.returncode)
예제 #27
0
 def login(self,
           get_credentials: onlinejudge.service.CredentialsProvider,
           session: Optional[requests.Session] = None) -> bool:
     session = session or utils.new_default_session()
     url = 'https://www.hackerrank.com/login'
     # get
     resp = utils.request('GET', url, session=session)
     if resp.url != url:
         log.info('You have already signed in.')
         return True
     # parse
     soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                              utils.html_parser)
     form = soup.find('form', id='legacy-login')
     if not form:
         log.error('form not found')
         return False
     csrftoken = soup.find('meta', attrs={
         'name': 'csrf-token'
     }).attrs['content']
     # post
     username, password = get_credentials()
     form = utils.FormSender(form, url=resp.url)
     form.set('login', username)
     form.set('password', password)
     form.set('remember_me', 'true')
     form.set('fallback', 'true')
     resp = form.request(session,
                         action='/auth/login',
                         headers={'X-CSRF-Token': csrftoken})
     resp.raise_for_status()
     # result
     if resp.url != url:
         log.success('You signed in.')
         return True
     else:
         log.failure('You failed to sign in. Wrong user ID or password.')
         return False
def generate_scanner(args):
    if not args.silent:
        log.warning('This feature is ' + log.red('experimental') + '.')
    if args.silent:
        for handler in log.logger.handlers:
            log.removeHandler(handler)
    problem = onlinejudge.dispatch.problem_from_url(args.url)
    if problem is None:
        sys.exit(1)
    with utils.session(cookiejar=args.cookie) as sess:
        it = problem.get_input_format(session=sess)
    if not it:
        log.error('input format not found')
        sys.exit(1)
    try:
        it = tokenize(it)
        it = parse(it)
        it = export(it, use_scanf=args.scanf, repeat_macro=args.repeat_macro)
    except:
        log.error('somethin wrong')
        raise
    log.success('success:')
    print(log.bold(it.rstrip()))  # to stdout
예제 #29
0
 def submit(self, code, language, session=None):
     assert language in self.get_language_dict(session=session)
     session = session or requests.Session()
     url = 'http://{}.contest.atcoder.jp/submit'.format(self.contest_id)
     # get
     log.status('GET: %s', url)
     resp = session.get(url)
     log.status(utils.describe_status_code(resp.status_code))
     resp.raise_for_status()
     msgs = AtCoderService._get_messages_from_cookie(resp.cookies)
     if AtCoderService._report_messages(msgs, unexpected=True):
         return None
     # parse
     soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                              utils.html_parser)
     form = soup.find('form', action=re.compile(r'^/submit\?task_id='))
     if not form:
         log.error('form not found')
         return None
     log.debug('form: %s', str(form))
     # post
     task_id = self._get_task_id(session=session)
     form = utils.FormSender(form, url=resp.url)
     form.set('task_id', str(task_id))
     form.set('source_code', code)
     form.set('language_id_{}'.format(task_id), language)
     resp = form.request(session=session)
     resp.raise_for_status()
     # result
     msgs = AtCoderService._get_messages_from_cookie(resp.cookies)
     AtCoderService._report_messages(msgs)
     if '/submissions/me' in resp.url:
         log.success('success: result: %s', resp.url)
         return resp.url
     else:
         log.failure('failure')
         return None
예제 #30
0
 def submit_code(
     self,
     code: bytes,
     language: str,
     session: Optional[requests.Session] = None
 ) -> onlinejudge.type.Submission:  # or SubmissionError
     session = session or utils.new_default_session()
     # get
     url = self.get_url() + '/submit'
     resp = utils.request('GET', url, session=session)
     # parse
     soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                              utils.html_parser)
     form = soup.find('form', id='submit_form')
     if not form:
         log.error('form not found')
         raise onlinejudge.type.SubmissionError
     # post
     form = utils.FormSender(form, url=resp.url)
     form.set('lang', language)
     form.set_file('file', 'code', code)
     form.unset('custom_test')
     resp = form.request(session=session)
     resp.raise_for_status()
     # result
     if 'submissions' in resp.url:
         # example: https://yukicoder.me/submissions/314087
         log.success('success: result: %s', resp.url)
         return onlinejudge.type.DummySubmission(resp.url)
     else:
         log.failure('failure')
         log.debug('redirected to %s', resp.url)
         soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                                  utils.html_parser)
         for div in soup.findAll('div', attrs={'role': 'alert'}):
             log.warning('yukicoder says: "%s"', div.string)
         raise onlinejudge.type.SubmissionError