|
3 | 3 | Using a wrapper simplifies accessing the API, mostly due to handling OAuth. |
4 | 4 | """ |
5 | 5 |
|
6 | | -from enum import Enum, auto |
| 6 | +from enum import StrEnum |
7 | 7 | from logging import getLogger |
8 | 8 | from time import time_ns |
9 | 9 | from typing import Any |
|
13 | 13 | Article = dict[str, Any] |
14 | 14 |
|
15 | 15 |
|
16 | | -class ArticlesSortType(Enum): |
| 16 | +class ArticlesSortType(StrEnum): |
17 | 17 | """Enum with all viable sorting types""" |
18 | 18 |
|
19 | | - HOT = auto() |
20 | | - NEW = auto() |
21 | | - RISING = auto() |
22 | | - TOP = auto() |
23 | | - CONTROVERSIAL = auto() |
| 19 | + HOT = "hot" |
| 20 | + NEW = "new" |
| 21 | + RISING = "rising" |
| 22 | + TOP = "top" |
| 23 | + CONTROVERSIAL = "controversial" |
24 | 24 |
|
25 | 25 |
|
26 | | -class ArticlesSortTime(Enum): |
| 26 | +class ArticlesSortTime(StrEnum): |
27 | 27 | """Enum with all viable sort times""" |
28 | 28 |
|
29 | | - HOUR = auto() |
30 | | - DAY = auto() |
31 | | - WEEK = auto() |
32 | | - MONTH = auto() |
33 | | - YEAR = auto() |
34 | | - ALL = auto() |
| 29 | + HOUR = "hour" |
| 30 | + DAY = "day" |
| 31 | + WEEK = "week" |
| 32 | + MONTH = "month" |
| 33 | + YEAR = "year" |
| 34 | + ALL = "all" |
35 | 35 |
|
36 | 36 |
|
37 | 37 | class Reddit: |
@@ -139,14 +139,11 @@ def _prepare_params( |
139 | 139 | time: ArticlesSortTime | None = None, |
140 | 140 | limit: int | None = None, |
141 | 141 | ): |
142 | | - params = dict() |
143 | | - if sort is not None: |
144 | | - params["sort"] = sort.name.lower() |
145 | | - if time is not None: |
146 | | - params["t"] = time.name.lower() |
147 | | - if limit is not None: |
148 | | - params["limit"] = limit |
149 | | - return params |
| 142 | + return { |
| 143 | + **({"sort": sort.value} if sort is not None else {}), |
| 144 | + **({"t": time.value} if time is not None else {}), |
| 145 | + **({"limit": limit} if limit is not None else {}), |
| 146 | + } |
150 | 147 |
|
151 | 148 | async def _get_articles(self, url: str, params: dict[str, Any]) -> list[Article]: |
152 | 149 | if self._access_token_expires_in <= time_ns(): |
|
0 commit comments