|
1 | | -import requests |
2 | | - |
3 | | -from .constants import USER_AGENT |
4 | | -from .exceptions import AccessDeniedHttpException, HttpException, NotFoundHttpException |
5 | | -from .utils import LazyList, cached_method, dict_without_underscores, get_csrf_token |
6 | | - |
7 | | - |
8 | | -class Model: |
9 | | - _session = requests.Session() |
10 | | - _session.headers["User-Agent"] = USER_AGENT |
11 | | - |
12 | | - @classmethod |
13 | | - def _get(cls, url: str, params: dict = None, check: bool = True) -> "dict[str]": |
14 | | - r = cls._session.get( |
15 | | - url, |
16 | | - params=params, |
17 | | - headers={"X-Luogu-Type": "content-only"}, |
18 | | - ) |
19 | | - r.raise_for_status() |
20 | | - data = r.json() |
21 | | - if check: |
22 | | - if data["code"] == 404: |
23 | | - raise NotFoundHttpException(data["currentData"]["errorMessage"]) |
24 | | - elif data["code"] == 403: |
25 | | - raise AccessDeniedHttpException(data["currentData"]["errorMessage"]) |
26 | | - elif data["code"] >= 400: |
27 | | - raise HttpException(data["currentData"]["errorMessage"]) |
28 | | - return data |
29 | | - |
30 | | - @classmethod |
31 | | - def _post(cls, url: str, data: dict = None) -> "dict[str]": |
32 | | - r = cls._session.post( |
33 | | - url, |
34 | | - json=data, |
35 | | - headers={ |
36 | | - "x-csrf-token": get_csrf_token(cls._session), |
37 | | - }, |
38 | | - ) |
39 | | - r.raise_for_status() |
40 | | - return r.json() |
41 | | - |
42 | | - def __repr__(self): |
43 | | - return f"{self.__class__.__name__}({self.id})" |
44 | | - |
45 | | - def __eq__(self, other): |
46 | | - if hasattr(self, "_current_data") and hasattr(other, "_current_data"): |
47 | | - return self._current_data == other._current_data |
48 | | - if type(other) is type(self): |
49 | | - return dict_without_underscores(self.__dict__) == dict_without_underscores( |
50 | | - other.__dict__ |
51 | | - ) |
52 | | - else: |
53 | | - super().__eq__(other) |
| 1 | +from ..utils import LazyList, cached_method |
| 2 | +from . import Model |
54 | 3 |
|
55 | 4 |
|
56 | 5 | class User(Model): |
@@ -269,75 +218,3 @@ def id(self): |
269 | 218 | @cached_method |
270 | 219 | def provider(self): |
271 | 220 | return User(self._provider["uid"]) |
272 | | - |
273 | | - |
274 | | -class Paste(Model): |
275 | | - """剪贴板 |
276 | | -
|
277 | | - :param str id: 剪贴板 ID |
278 | | -
|
279 | | - :var str data: 内容 |
280 | | - :var str id: 剪贴板 ID |
281 | | - :var User user: 用户 |
282 | | - :var int time: 时间 |
283 | | - :var bool public: 是否公开 |
284 | | - """ |
285 | | - |
286 | | - def __init__(self, id: str) -> None: |
287 | | - self._current_data: dict[str] = self._get( |
288 | | - f"https://www.luogu.com.cn/paste/{id}" |
289 | | - )["currentData"] |
290 | | - |
291 | | - paste: dict[str] = self._current_data["paste"] |
292 | | - self.data: str = paste["data"] |
293 | | - self.id: str = paste["id"] |
294 | | - self._user: dict[str] = paste["user"] |
295 | | - self.time: int = paste["time"] |
296 | | - self.public: bool = paste["public"] |
297 | | - |
298 | | - @property |
299 | | - @cached_method |
300 | | - def user(self) -> User: |
301 | | - return User(self._user["uid"]) |
302 | | - |
303 | | - def delete(self) -> str: |
304 | | - """删除剪贴板 |
305 | | -
|
306 | | - :returns: 剪贴板 ID |
307 | | - :rtype: str |
308 | | - """ |
309 | | - return self._post(f"https://www.luogu.com.cn/paste/delete/{self.id}")["id"] |
310 | | - |
311 | | - def edit(self, data: str = None, public: bool = None): |
312 | | - """编辑剪贴板 |
313 | | -
|
314 | | - :param str data: 剪贴板内容 |
315 | | - :param bool public: 是否公开 |
316 | | -
|
317 | | - :returns: 剪贴板 ID |
318 | | - :rtype: str |
319 | | - """ |
320 | | - r = self._post( |
321 | | - f"https://www.luogu.com.cn/paste/edit/{self.id}", |
322 | | - {"data": data, "public": public}, |
323 | | - ) |
324 | | - if data is not None: |
325 | | - self.data = data |
326 | | - if public is not None: |
327 | | - self.public = public |
328 | | - return r["id"] |
329 | | - |
330 | | - @classmethod |
331 | | - def new(cls, data: str, public: bool = None) -> "Paste": |
332 | | - """新建剪贴板 |
333 | | -
|
334 | | - :param str data: 剪贴板内容 |
335 | | - :param bool public: 值为真时表示公开剪贴板,否则表示私有剪贴板 |
336 | | -
|
337 | | - :rtype: Session.Paste |
338 | | - """ |
339 | | - r = cls._post( |
340 | | - "https://www.luogu.com.cn/paste/new", |
341 | | - {"data": data, "public": public}, |
342 | | - ) |
343 | | - return cls(r["id"]) |
0 commit comments