| 276 | |
| 277 | |
| 278 | class Contest: |
| 279 | def __init__(self, contest_seq: int, contest_type: int = 1): |
| 280 | double = contest_type % 2 == 0 |
| 281 | url_pattern = BIWEEKLY_URL if double else WEEKLY_URL |
| 282 | slug_pattern = BIWEEKLY_SLUG if double else WEEKLY_SLUG |
| 283 | self.contest_type = contest_type |
| 284 | self.contest_url = url_pattern.format(contest_seq) |
| 285 | self.contest_title_slug = slug_pattern.format(contest_seq) |
| 286 | self.contest_title = ( |
| 287 | f"第 {contest_seq} 场双周赛" if double else f"第 {contest_seq} 场周赛" |
| 288 | ) |
| 289 | self.contest_title_en = ( |
| 290 | f"Biweekly Contest {contest_seq}" |
| 291 | if double |
| 292 | else f"Weekly Contest {contest_seq}" |
| 293 | ) |
| 294 | |
| 295 | @staticmethod |
| 296 | def format_time(timestamp: int) -> str: |
| 297 | tz = timezone(timedelta(hours=+8)) |
| 298 | return datetime.fromtimestamp(timestamp, tz).strftime("%Y-%m-%d %H:%M") |
| 299 | |
| 300 | def get_data(self, retry: int = 3): |
| 301 | try: |
| 302 | print(self.contest_url) |
| 303 | headers = { |
| 304 | 'User-Agent': user_agent, |
| 305 | 'Host': 'leetcode.cn', |
| 306 | 'content-type': 'application/json', |
| 307 | 'Accept': 'application/json, text/javascript, */*; q=0.01', |
| 308 | } |
| 309 | res = requests.get( |
| 310 | self.contest_url, timeout=6, verify=False, headers=headers |
| 311 | ) |
| 312 | res = res.json() |
| 313 | if not res or "error" in res or not res["questions"]: |
| 314 | return {} |
| 315 | questions = res["questions"] |
| 316 | question_slugs = [q["title_slug"] for q in questions] |
| 317 | return { |
| 318 | "contest_title": self.contest_title, |
| 319 | "contest_title_en": self.contest_title_en, |
| 320 | "contest_title_slug": res["contest"]["title_slug"], |
| 321 | "contest_id": res["contest"]["id"], |
| 322 | "contest_start_time": res["contest"]["origin_start_time"], |
| 323 | "contest_duration": res["contest"]["duration"], |
| 324 | "user_num": res["user_num"], |
| 325 | "question_slugs": question_slugs, |
| 326 | } |
| 327 | except Exception as e: |
| 328 | print(e) |
| 329 | time.sleep(2) |
| 330 | return self.get_data(retry - 1) if retry > 0 else {} |
| 331 | |
| 332 | @staticmethod |
| 333 | def format(data: dict) -> List: |
| 334 | if not data: |
| 335 | return [] |