|
| 1 | +import os |
| 2 | +import urllib.request |
| 3 | +import urllib.error |
| 4 | +from bs4 import BeautifulSoup |
| 5 | + |
| 6 | +__all__ = ['get_info'] |
| 7 | + |
| 8 | +CODEFORCES_URL = "https://codeforces.com" |
| 9 | + |
| 10 | + |
| 11 | +def get_info(contest_id, index, lang='en'): |
| 12 | + """ |
| 13 | + Get info for a contest problem. |
| 14 | +
|
| 15 | + Parameters |
| 16 | + ---------- |
| 17 | + contest_id: int |
| 18 | + Id of the contest. It is not the round number. It can be seen in |
| 19 | + contest URL. For example: /contest/**566**/status |
| 20 | + index: str |
| 21 | + Usually a letter of a letter, followed by a digit, that |
| 22 | + represent a problem index in a contest. It can be seen in |
| 23 | + problem URL. For example: /contest/566/**A** |
| 24 | +
|
| 25 | + Returns |
| 26 | + ------- |
| 27 | + title: str |
| 28 | + Title of the problem. |
| 29 | + time_limit: str |
| 30 | + Time limit specification for the problem. |
| 31 | + memory_limit: str |
| 32 | + Memory limit specification for the problem. |
| 33 | + sample_tests: zip |
| 34 | + Sample tests given for the problem. |
| 35 | + """ |
| 36 | + |
| 37 | + problem_url = os.path.join( |
| 38 | + CODEFORCES_URL, |
| 39 | + "contest/%d/problem/%s?lang=%s" % (contest_id, index, lang) |
| 40 | + ) |
| 41 | + |
| 42 | + with urllib.request.urlopen(problem_url) as res: |
| 43 | + soup = BeautifulSoup(res.read(), 'html.parser') |
| 44 | + |
| 45 | + title = soup.find_all("div", class_="title")[0].text |
| 46 | + |
| 47 | + time_limit = soup.find_all("div", class_="time-limit")[0].text[19:] |
| 48 | + memory_limit = soup.find_all("div", class_="memory-limit")[0].text[21:] |
| 49 | + |
| 50 | + i = [i.pre.string[1:] for i in soup.find_all("div", class_="input")] |
| 51 | + o = [i.pre.string[1:] for i in soup.find_all("div", class_="output")] |
| 52 | + |
| 53 | + sample_tests = zip(i, o) |
| 54 | + |
| 55 | + return (title, time_limit, memory_limit, sample_tests) |
0 commit comments