Ejemplo n.º 1
0
 def download_sample_cases(
     self,
     session: Optional[requests.Session] = None
 ) -> List[onlinejudge.type.TestCase]:
     session = session or utils.new_default_session()
     # get
     resp = _request('GET', self.get_url(), session=session)
     msgs = AtCoderService._get_messages_from_cookie(resp.cookies)
     if AtCoderService._report_messages(msgs, unexpected=True):
         # example message: "message: You cannot see this page."
         log.warning('are you logged in?')
         return []
     # parse
     soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                              utils.html_parser)
     samples = onlinejudge._implementation.testcase_zipper.SampleZipper()
     lang = None
     for pre, h3 in self._find_sample_tags(soup):
         s = utils.textfile(utils.dos2unix(pre.string.lstrip()))
         name = h3.string
         l = self._get_tag_lang(pre)
         if lang is None:
             lang = l
         elif lang != l:
             log.info(
                 'skipped due to language: current one is %s, not %s: %s ',
                 lang, l, name)
             continue
         samples.add(s.encode(), name)
     return samples.get()
Ejemplo n.º 2
0
 def _parse_sample_tag(self, tag: bs4.Tag) -> Optional[Tuple[str, str]]:
     assert isinstance(tag, bs4.Tag)
     assert tag.name == 'h2'
     name = tag.contents[0]
     if ':' in name:
         name = name[:name.find(':')]
     if name in ['Sample input', 'Sample output']:
         nxt = tag.next_sibling
         while nxt and nxt.string.strip() == '':
             nxt = nxt.next_sibling
         if nxt.name == 'pre':
             s = utils.textfile(utils.dos2unix(nxt.string.lstrip()))
         else:
             s = ''
         return s, name
     return None
Ejemplo n.º 3
0
def _AtCoderProblemContent_parse_sample_cases(
        soup: bs4.BeautifulSoup) -> List[onlinejudge.type.TestCase]:
    samples = onlinejudge._implementation.testcase_zipper.SampleZipper()
    lang = None
    for pre, h3 in _AtCoderProblemContent_find_sample_tags(soup):
        s = utils.textfile(utils.dos2unix(pre.string.lstrip()))
        name = h3.string
        l = _AtCoderProblemContent_get_tag_lang(pre)
        if lang is None:
            lang = l
        elif lang != l:
            log.info('skipped due to language: current one is %s, not %s: %s ',
                     lang, l, name)
            continue
        samples.add(s.encode(), name)
    return samples.get()
Ejemplo n.º 4
0
    def _parse_sample_tag(self, tag: bs4.Tag) -> Optional[Tuple[str, str]]:
        assert isinstance(tag, bs4.Tag)
        assert tag.name == 'h2'
        name = tag.contents[0]
        if ':' in name:
            name = name[:name.find(':')]
        if name in ['Sample input', 'Sample output']:
            nxt = tag.next_sibling
            while nxt and nxt.string.strip() == '':
                nxt = nxt.next_sibling

            # This implementation is discussed in https://github.com/kmyk/online-judge-tools/pull/599
            if nxt.name == 'pre':
                s = utils.dos2unix(nxt.string[1:])
            elif nxt.name == 'p':
                s = ''  # *NOTHING* means that the empty string "" is input, not "\n".

            return s, name
        return None
Ejemplo n.º 5
0
    def download_sample_cases(
            self,
            *,
            session: Optional[requests.Session] = None) -> List[TestCase]:
        """
        :raises SampleParseError:
        """
        session = session or utils.get_default_session()

        url = 'https://www.spoj.com/problems/{}'.format(self.problem_id)
        resp = utils.request('GET', url, session=session)
        soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                                 utils.html_parser)
        samples = onlinejudge._implementation.testcase_zipper.SampleZipper()
        for pre, h3 in self._find_sample_tags(soup):
            s = utils.textfile(
                utils.dos2unix(utils.parse_content(pre).lstrip()))
            name = h3.string
            samples.add(s.encode(), name)
        return samples.get()
Ejemplo n.º 6
0
    def download_sample_cases(
        self,
        session: Optional[requests.Session] = None
    ) -> List[onlinejudge.type.TestCase]:
        """
        :raises Exception: if no such problem exists
        """

        session = session or utils.new_default_session()

        # get
        resp = _request('GET',
                        self.get_url(type='beta'),
                        raise_for_status=False,
                        session=session)
        if _list_alert(resp):
            log.warning('are you logged in?')
        resp.raise_for_status()

        # parse
        soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                                 utils.html_parser)
        samples = onlinejudge._implementation.testcase_zipper.SampleZipper()
        lang = None
        for pre, h3 in self._find_sample_tags(soup):
            s = utils.textfile(utils.dos2unix(pre.string.lstrip()))
            name = h3.string
            l = self._get_tag_lang(pre)
            if lang is None:
                lang = l
            elif lang != l:
                log.info(
                    'skipped due to language: current one is %s, not %s: %s ',
                    lang, l, name)
                continue
            samples.add(s.encode(), name)
        return samples.get()