Beispiel #1
0
class TestAtCoderClientReal(unittest.TestCase):

    def setUp(self):
        self.temp_dir = tempfile.mkdtemp()
        self.client = AtCoderClient()

    def test_submit_source_code(self):
        problem_list = self.client.download_problem_list(Contest("arc002"))
        self.assertEqual(
            ['arc002_1',
             'arc002_2',
             'arc002_3',
             'arc002_4'],
            [p.problem_id for p in problem_list])

    def test_download_problem_content(self):
        content = self.client.download_problem_content(
            Problem(Contest("arc002"), "C", "arc002_3"))
        self.assertEqual("N\nc_{1}c_{2}...c_{N}\n", content.input_format_text)
        self.assertEqual(3, len(content.samples))

    def test_login_failed(self):
        def fake_supplier():
            return "@@@ invalid user name @@@", "@@@ password @@@"

        try:
            self.client.login(credential_supplier=fake_supplier,
                              use_local_session_cache=False)
            self.fail("Unexpectedly, this test succeeded to login.")
        except LoginError:
            pass

    def test_download_all_contests(self):
        contests = self.client.download_all_contests()
        # Check if the number of contests is more than the number when I wrote
        # this unit test.
        self.assertGreaterEqual(len(contests), 523)

        # Make sure there is no duplication
        self.assertEqual(
            len(set([c.get_id() for c in contests])),
            len(contests))

    def test_check_logging_in_is_false(self):
        self.assertFalse(self.client.check_logging_in())

    def test_cookie_save_and_load(self):
        cookie_path = os.path.join(self.temp_dir, "cookie.txt")

        session = requests.Session()

        loaded = load_cookie_to(session, cookie_path)
        self.assertFalse(loaded)

        save_cookie(session, cookie_path)

        new_session = requests.Session()
        loaded = load_cookie_to(new_session, cookie_path)
        self.assertTrue(loaded)
Beispiel #2
0
def prepare_contest(atcoder_client: AtCoderClient,
                    contest_id: str,
                    config: Config,
                    retry_delay_secs: float = 1.5,
                    retry_max_delay_secs: float = 60,
                    retry_max_tries: int = 10):
    attempt_count = 1
    while True:
        problem_list = atcoder_client.download_problem_list(
            Contest(contest_id=contest_id))
        if problem_list:
            break
        if 0 < retry_max_tries < attempt_count:
            raise EnvironmentInitializationError
        logger.warning(
            "Failed to fetch. Will retry in {} seconds. (Attempt {})".format(
                retry_delay_secs, attempt_count))
        time.sleep(retry_delay_secs)
        retry_delay_secs = min(retry_delay_secs * 2, retry_max_delay_secs)
        attempt_count += 1

    atcoder_client.download_contest_languages(problem_list)

    tasks = [(atcoder_client, problem, config) for problem in problem_list]

    output_splitter()

    if config.etc_config.parallel_download:
        thread_pool = Pool(processes=cpu_count())
        thread_pool.map(func, tasks)
    else:
        for argv in tasks:
            try:
                func(argv)
            except Exception:
                # Prevent the script from stopping
                print(traceback.format_exc(), file=sys.stderr)
                pass

    if config.postprocess_config.exec_cmd_on_contest_dir is not None:
        contest_dir_path = os.path.join(config.code_style_config.workspace_dir,
                                        contest_id)
        logger.info(
            _message_on_execution(
                contest_dir_path,
                config.postprocess_config.exec_cmd_on_contest_dir))
        config.postprocess_config.execute_on_contest_dir(contest_dir_path)
Beispiel #3
0
def main(lang='ja'):

    alphabet = str(Path('.').absolute().name)
    contest_id = str(Path('.').absolute().parent.name)

    client = AtCoderClient()
    client.login()
    ic(client.check_logging_in())

    contest = Contest(ic(contest_id))
    ic(contest.get_url())
    problem_list = client.download_problem_list(ic(contest))
    problem = problem_list[['A', 'B', 'C', 'D', 'E', 'F'].index(ic(alphabet))]

    html_doc = client.download_problem_content(problem).original_html
    soup = BeautifulSoup(html_doc, 'html.parser')

    title = soup.find(class_="h2").get_text()
    task_statement = soup.find(id="task-statement")

    if lang == 'ja':
        task_statement = task_statement.find(class_='lang-ja')

    def sanitize_html_for_ipynb(html_doc):
        replace_dict = {
            '<var>': '$',
            '</var>': '$',
            '<pre>': '<pre><code>',
            '</pre>': '</code></pre>'
        }
        for old_word, new_word in replace_dict.items():
            html_doc = html_doc.replace(old_word, new_word)
        return ic(html_doc)

    title = str(sanitize_html_for_ipynb(str(title)))
    title = title.lstrip().split('\n')[0]

    task_statement = Tomd(sanitize_html_for_ipynb(str(task_statement)))
    with open('problem.md', 'w+') as f:
        f.write(f"## {ic(title)}\n")
        f.write('---\n')
        f.write(task_statement.markdown)
Beispiel #4
0
def prepare_contest(atcoder_client: AtCoderClient,
                    contest_id: str,
                    config: Config):
    retry_duration = 1.5
    while True:
        problem_list = atcoder_client.download_problem_list(
            Contest(contest_id=contest_id))
        if problem_list:
            break
        sleep(retry_duration)
        logging.warning(
            "Failed to fetch. Will retry in {} seconds".format(retry_duration))

    tasks = [(atcoder_client,
              problem,
              config) for
             problem in problem_list]

    output_splitter()

    if config.etc_config.parallel_download:
        thread_pool = Pool(processes=cpu_count())
        thread_pool.map(func, tasks)
    else:
        for argv in tasks:
            try:
                func(argv)
            except Exception:
                # Prevent the script from stopping
                print(traceback.format_exc(), file=sys.stderr)
                pass

    if config.postprocess_config.exec_cmd_on_contest_dir is not None:
        contest_dir_path = os.path.join(
            config.code_style_config.workspace_dir, contest_id)
        logging.info(_message_on_execution(contest_dir_path,
                                           config.postprocess_config.exec_cmd_on_contest_dir))
        config.postprocess_config.execute_on_contest_dir(
            contest_dir_path)
def prepare_contest(atcoder_client: AtCoderClient,
                    contest_id: str,
                    config: Config):
    retry_duration = 1.5
    while True:
        problem_list = atcoder_client.download_problem_list(
            Contest(contest_id=contest_id))
        if problem_list:
            break
        sleep(retry_duration)
        logging.warning(
            "Failed to fetch. Will retry in {} seconds".format(retry_duration))

    tasks = [(atcoder_client,
              problem,
              config) for
             problem in problem_list]

    output_splitter()

    if config.etc_config.parallel_download:
        thread_pool = Pool(processes=cpu_count())
        thread_pool.map(func, tasks)
    else:
        for argv in tasks:
            try:
                func(argv)
            except Exception:
                # Prevent the script from stopping
                print(traceback.format_exc(), file=sys.stderr)
                pass

    if config.postprocess_config.exec_cmd_on_contest_dir is not None:
        contest_dir_path = os.path.join(
            config.code_style_config.workspace_dir, contest_id)
        logging.info(_message_on_execution(contest_dir_path,
                                           config.postprocess_config.exec_cmd_on_contest_dir))
        config.postprocess_config.execute_on_contest_dir(
            contest_dir_path)
Beispiel #6
0
#!/usr/bin/env python3
from atcodertools.client.models.contest import Contest
from atcodertools.client.atcoder import AtCoderClient
import sys

# write contest id here
cid = sys.argv[1]

print('Contest ID = {}'.format(cid))

try:
    at = AtCoderClient()
    at.login()
    cn = Contest(cid)
    pls = at.download_problem_list(cn)

except Exception as e:
    print('ログイン/問題取得でエラー Exception = {}'.format(e))

else:
    from subprocess import run
    from glob import glob

    run(['rm', '-r', 'tests'])
    run(['mkdir', 'tests'])

    for pl in pls:
        k = pl.alphabet
        v = pl.get_url()

        try:
atcoder = AtCoderClient()


def mkdirs(path):
    try:
        os.makedirs(path)
    except OSError as exc:
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass


if __name__ == "__main__":
    htmls_dir = "./problem_htmls/"
    mkdirs(htmls_dir)
    for contest in atcoder.download_all_contests():
        for problem in atcoder.download_problem_list(contest):
            html_path = os.path.join(
                htmls_dir, "{contest}-{problem_id}.html".format(
                    contest=contest.get_id(),
                    problem_id=problem.get_alphabet()))

            if os.path.exists(html_path):
                print(
                    "{} already exists -- skipping download".format(html_path))
                continue

            print("Downloading {}".format(html_path))
            try:
                html = atcoder.download_problem_content(problem).original_html
                with open(html_path, 'w') as f:
                    f.write(html)
atcoder = AtCoderClient()


def mkdirs(path):
    try:
        os.makedirs(path)
    except OSError as exc:
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass


if __name__ == "__main__":
    htmls_dir = "./problem_htmls/"
    mkdirs(htmls_dir)
    for contest in atcoder.download_all_contests():
        for problem in atcoder.download_problem_list(contest):
            html_path = os.path.join(htmls_dir, "{contest}-{problem_id}.html".format(
                contest=contest.get_id(), problem_id=problem.get_alphabet()))

            if os.path.exists(html_path):
                print(
                    "{} already exists -- skipping download".format(html_path))
                continue

            print("Downloading {}".format(html_path))
            try:
                html = atcoder.download_problem_content(problem).original_html
                with open(html_path, 'w') as f:
                    f.write(html)
            except Exception as e:
                print("Failed to download {}. {}".format(html_path, e))