|
| 1 | +# Python Version: 3.x |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +the module for Algo-Method (https://algo-method.com/) |
| 5 | +""" |
| 6 | + |
| 7 | +import urllib.parse |
| 8 | +from typing import * |
| 9 | +import json |
| 10 | +import re |
| 11 | + |
| 12 | +import bs4 |
| 13 | +import requests |
| 14 | + |
| 15 | +import onlinejudge._implementation.testcase_zipper |
| 16 | +import onlinejudge._implementation.utils as utils |
| 17 | +import onlinejudge.dispatch |
| 18 | +import onlinejudge.type |
| 19 | + |
| 20 | +import posixpath |
| 21 | + |
| 22 | +class AlgoMethodService(onlinejudge.type.Service): |
| 23 | + def get_url(self) -> str: |
| 24 | + return 'https://algo-method.com/' |
| 25 | + |
| 26 | + def get_name(self) -> str: |
| 27 | + return 'algo-method' |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def from_url(cls, url: str) -> Optional['AlgoMethodService']: |
| 31 | + # example: https://algo-method.com/ |
| 32 | + result = urllib.parse.urlparse(url) |
| 33 | + if result.scheme in ('', 'http', 'https') \ |
| 34 | + and result.netloc == 'algo-method.com': |
| 35 | + return cls() |
| 36 | + return None |
| 37 | + |
| 38 | + |
| 39 | +class AlgoMethodProblem(onlinejudge.type.Problem): |
| 40 | + def __init__(self, *, problem_id: str): |
| 41 | + self.problem_id = problem_id |
| 42 | + |
| 43 | + def download_sample_cases(self, *, session: Optional[requests.Session] = None) -> List[onlinejudge.type.TestCase]: |
| 44 | + session = session or utils.get_default_session() |
| 45 | + # get |
| 46 | + resp = utils.request('GET', self.get_url(), session=session) |
| 47 | + # parse |
| 48 | + soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.HTML_PARSER) |
| 49 | + samples = onlinejudge._implementation.testcase_zipper.SampleZipper() |
| 50 | + for case, name in self._parse_sample_cases(soup): |
| 51 | + samples.add(case.encode(), name) |
| 52 | + return samples.get() |
| 53 | + |
| 54 | + def _parse_sample_cases(self, soup: bs4.BeautifulSoup) -> List[Tuple[str, str]]: |
| 55 | + bodyMD = json.loads(soup.find(id='__NEXT_DATA__').get_text())['props']['pageProps']['tasks']['body'] |
| 56 | + pattern = r'#### ([入出]力例 \d ?)\r\n```IOExample\r\n([\s\S]+?)```' |
| 57 | + cases = re.findall(pattern, bodyMD) |
| 58 | + for name, case in cases: |
| 59 | + yield (case, name) |
| 60 | + |
| 61 | + def get_url(self) -> str: |
| 62 | + return 'https://algo-method.com/tasks/{}'.format(self.problem_id) |
| 63 | + |
| 64 | + def get_service(self) -> AlgoMethodService: |
| 65 | + return AlgoMethodService() |
| 66 | + |
| 67 | + @classmethod |
| 68 | + def from_url(cls, url: str) -> Optional['AlgoMethodProblem']: |
| 69 | + # example: https://algo-method.com/tasks/15 |
| 70 | + result = urllib.parse.urlparse(url) |
| 71 | + dirname, basename = posixpath.split(utils.normpath(result.path)) |
| 72 | + if result.scheme in ('', 'http', 'https') \ |
| 73 | + and result.netloc == 'algo-method.com' \ |
| 74 | + and dirname == '/tasks' \ |
| 75 | + and basename: |
| 76 | + return cls(problem_id=basename) |
| 77 | + return None |
| 78 | + |
| 79 | + |
| 80 | +onlinejudge.dispatch.services += [AlgoMethodService] |
| 81 | +onlinejudge.dispatch.problems += [AlgoMethodProblem] |
0 commit comments