Esempio n. 1
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)
Esempio n. 2
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
Esempio n. 3
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
Esempio n. 4
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
Esempio n. 5
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
Esempio n. 6
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)
Esempio n. 7
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)
 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 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
Esempio n. 10
0
def login(args):
    # get service
    service = onlinejudge.dispatch.service_from_url(args.url)
    if service is None:
        sys.exit(1)

    # configure
    kwargs = {}
    if service.get_name() == 'yukicoder':
        if not args.method:
            args.method = 'github'
        if args.method not in [ 'github', 'twitter' ]:
            log.failure('login for yukicoder: invalid option: --method %s', args.method)
            sys.exit(1)
        kwargs['method'] = args.method
    else:
        if args.method:
            log.failure('login for %s: invalid option: --method %s', service.get_name(), args.method)
            sys.exit(1)

    # login
    def get_credentials():
        if args.username is None:
            args.username = input('Username: ')
        if args.password is None:
            args.password = getpass.getpass()
        return args.username, args.password
    with utils.with_cookiejar(utils.new_default_session(), path=args.cookie) as sess:
        service.login(get_credentials, session=sess, **kwargs)
Esempio n. 11
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
Esempio n. 12
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
 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
Esempio n. 14
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
Esempio n. 15
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)
Esempio n. 16
0
def submission_from_url(s):
    for cls in submissions:
        it = cls.from_url(s)
        if it is not None:
            log.status('submission recognized: %s: %s', str(it), s)
            return it
    log.failure('unknown submission: %s', s)
Esempio n. 17
0
def submission_from_url(s: str) -> Optional['Submission']:
    for cls in submissions:
        submission = cls.from_url(s)
        if submission is not None:
            log.status('submission recognized: %s: %s', str(submission), s)
            return submission
    log.failure('unknown submission: %s', s)
    return None
Esempio n. 18
0
 def _report_messages(cls,
                      msgs: List[str],
                      unexpected: bool = False) -> bool:
     for msg in msgs:
         log.status('message: %s', msg)
     if msgs and unexpected:
         log.failure('unexpected messages found')
     return bool(msgs)
Esempio n. 19
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
Esempio n. 20
0
def problem_from_url(s):
    for cls in problems:
        it = cls.from_url(s)
        if it is not None:
            log.status('problem recognized: %s: %s', str(it), s)
            return it
    it = submission_from_url(s)
    if it is not None:
        return it.get_problem()
    log.failure('unknown problem: %s', s)
Esempio n. 21
0
def problem_from_url(s: str) -> Optional['Problem']:
    for cls in problems:
        problem = cls.from_url(s)
        if problem is not None:
            log.status('problem recognized: %s: %s', str(problem), s)
            return problem
    submission = submission_from_url(s)
    if submission is not None:
        return submission.get_problem()
    log.failure('unknown problem: %s', s)
    return None
Esempio n. 22
0
def submit(args):
    problem = onlinejudge.dispatch.problem_from_url(args.url)
    if problem is None:
        sys.exit(1)
    # code
    with open(args.file) as fh:
        code = fh.buffer.read()
    try:
        s = code.decode()  # for logging
    except UnicodeDecodeError as e:
        log.failure('%s: %s', e.__class__.__name__, str(e))
        s = repr(code)[1:]
    log.info('code:')
    log.emit(log.bold(s))
    # session
    with utils.session(cookiejar=args.cookie) as sess:
        # language
        langs = problem.get_language_dict(session=sess)
        if args.language not in langs:
            log.error('language is unknown')
            log.info('supported languages are:')
            for lang in sorted(langs.keys()):
                log.emit('%s (%s)', lang, langs[lang]['description'])
            sys.exit(1)
        # confirm
        if args.wait:
            log.status('sleep(%.2f)', args.wait)
            time.sleep(args.wait)
        if not args.yes:
            sys.stdout.write('Are you sure? [y/N] ')
            sys.stdout.flush()
            c = sys.stdin.read(1)
            if c != 'y':
                log.info('terminated.')
                return
        # submit
        url = problem.submit(code, language=args.language, session=sess)
        if url and args.open:
            if not isinstance(args.open, str):
                args.open = None
                for browser in default_url_opener:
                    args.open = shutil.which(browser)
                    if args.open:
                        break
            if not args.open:
                log.failure('couldn\'t open the url. please specify a browser')
            else:
                log.info('open the submission page with: %s', args.open)
                subprocess.check_call([args.open, url],
                                      stdin=sys.stdin,
                                      stdout=sys.stdout,
                                      stderr=sys.stderr)
Esempio n. 23
0
def service_from_url(s):
    for cls in services:
        it = cls.from_url(s)
        if it is not None:
            log.status('service recognized: %s: %s', str(it), s)
            return it
    it = submission_from_url(s)
    if it is not None:
        return it.get_service()
    it = problem_from_url(s)
    if it is not None:
        return it.get_service()
    log.failure('unknown service: %s', s)
Esempio n. 24
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
Esempio n. 25
0
def service_from_url(s: str) -> Optional['Service']:
    for cls in services:
        service = cls.from_url(s)
        if service is not None:
            log.status('service recognized: %s: %s', str(service), s)
            return service
    submission = submission_from_url(s)
    if submission is not None:
        return submission.get_service()
    problem = problem_from_url(s)
    if problem is not None:
        return problem.get_service()
    log.failure('unknown service: %s', s)
    return None
Esempio n. 26
0
def login(args: 'argparse.Namespace') -> None:
    # get service
    service = onlinejudge.dispatch.service_from_url(args.url)
    if service is None:
        sys.exit(1)

    # configure
    kwargs = {}
    if service.get_name() == 'yukicoder':
        if not args.method:
            args.method = 'github'
        if args.method not in ['github', 'twitter']:
            log.failure('login for yukicoder: invalid option: --method %s',
                        args.method)
            sys.exit(1)
        kwargs['method'] = args.method
    else:
        if args.method:
            log.failure('login for %s: invalid option: --method %s',
                        service.get_name(), args.method)
            sys.exit(1)

    with utils.with_cookiejar(utils.new_default_session(),
                              path=args.cookie) as sess:

        if args.check:
            if service.is_logged_in(session=sess):
                log.info('You have already signed in.')
            else:
                log.info('You are not signed in.')
                sys.exit(1)

        else:
            # login
            def get_credentials() -> Tuple[str, str]:
                if args.username is None:
                    args.username = input('Username: '******'If you don\'t want to give your password to this program, you can give only your session tokens.'
            )
            log.info(
                'see: https://github.com/kmyk/online-judge-tools/blob/master/LOGIN_WITH_COOKIES.md'
            )

            service.login(get_credentials, session=sess,
                          **kwargs)  # type: ignore
Esempio n. 27
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
Esempio n. 28
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
Esempio n. 29
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)