|
1 | | -from . import * |
| 1 | +from leetcode.models import * |
| 2 | +from leetcode.configuration import Configuration |
| 3 | +from leetcode.leet_api import LeetAPI |
2 | 4 |
|
3 | | -class problemByIDSlug(QueryTemplate): |
| 5 | +class problemInfo(): |
| 6 | + API_URL = "https://leetcode.com/api/problems/all/" |
| 7 | + configuration = Configuration() |
| 8 | + leet_api = LeetAPI(configuration) |
| 9 | + |
4 | 10 | def __init__(self): |
5 | | - super().__init__() |
6 | | - self.api_url = "https://leetcode.com/api/problems/all/" |
7 | 11 | self.title_slug: str = None |
8 | 12 |
|
| 13 | + @classmethod |
| 14 | + def get_title_slug(cls, question_id: int) -> str: |
| 15 | + response = cls.leet_api.get_request(cls.API_URL) |
| 16 | + for item in response.get('stat_status_pairs', []): |
| 17 | + if item['stat'].get('question_id') == question_id: |
| 18 | + return item['stat'].get('question__title_slug', '') |
| 19 | + return None |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def get_id(cls, title_slug: str) -> int: |
| 23 | + response = cls.leet_api.get_request(cls.API_URL) |
| 24 | + for item in response.get('stat_status_pairs', []): |
| 25 | + if item['stat'].get('question__title_slug') == title_slug: |
| 26 | + return item['stat'].get('question_id', 0) |
| 27 | + return None |
| 28 | + |
9 | 29 | def execute(self, args): |
10 | | - RESULT = self.leet_API.get_request(self.api_url) |
| 30 | + result = self.leet_api.get_request(self.API_URL) |
11 | 31 | if getattr(args, 'id'): |
12 | | - for x in RESULT['stat_status_pairs']: |
13 | | - if x['stat']['question_id'] == args.id: |
14 | | - self.title_slug = x['stat']['question__title_slug'] |
| 32 | + for item in result.get('stat_status_pairs', []): |
| 33 | + if item['stat'].get('question_id') == args.id: |
| 34 | + self.title_slug = item['stat'].get('question__title_slug', '') |
15 | 35 | break |
16 | | - if self.title_slug is None: |
17 | | - console.print("Invalid ID have been provided. Please try again.", style=ALERT) |
| 36 | + if not self.title_slug: |
| 37 | + console.print("Invalid ID has been provided. Please try again.", style=ALERT) |
18 | 38 | sys.exit(1) |
19 | 39 | elif getattr(args, 'slug'): |
20 | 40 | self.title_slug = args.slug |
|
0 commit comments