Skip to content

Commit 789c547

Browse files
committed
Split model files
1 parent f5bc3a9 commit 789c547

File tree

6 files changed

+144
-129
lines changed

6 files changed

+144
-129
lines changed

src/luogu/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,16 @@
88
"""
99

1010
from .exceptions import AccessDeniedHttpException, HttpException, NotFoundHttpException
11-
from .models import Paste, Problem, User
11+
from .models.main import Problem, User
12+
from .models.paste import Paste
1213
from .session import Session
14+
15+
__all__ = (
16+
"AccessDeniedHttpException",
17+
"HttpException",
18+
"NotFoundHttpException",
19+
"Paste",
20+
"Problem",
21+
"Session",
22+
"User",
23+
)

src/luogu/models/__init__.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import requests
2+
3+
from ..constants import USER_AGENT
4+
from ..exceptions import AccessDeniedHttpException, HttpException, NotFoundHttpException
5+
from ..utils import 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)

src/luogu/models.py renamed to src/luogu/models/main.py

Lines changed: 2 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,5 @@
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
543

554

565
class User(Model):
@@ -269,75 +218,3 @@ def id(self):
269218
@cached_method
270219
def provider(self):
271220
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"])

src/luogu/models/paste.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from ..utils import cached_method
2+
from . import Model
3+
from .main import User
4+
5+
6+
class Paste(Model):
7+
"""剪贴板
8+
9+
:param str id: 剪贴板 ID
10+
11+
:var str data: 内容
12+
:var str id: 剪贴板 ID
13+
:var User user: 用户
14+
:var int time: 时间
15+
:var bool public: 是否公开
16+
"""
17+
18+
def __init__(self, id: str) -> None:
19+
self._current_data: dict[str] = self._get(
20+
f"https://www.luogu.com.cn/paste/{id}"
21+
)["currentData"]
22+
23+
paste: dict[str] = self._current_data["paste"]
24+
self.data: str = paste["data"]
25+
self.id: str = paste["id"]
26+
self._user: dict[str] = paste["user"]
27+
self.time: int = paste["time"]
28+
self.public: bool = paste["public"]
29+
30+
@property
31+
@cached_method
32+
def user(self) -> User:
33+
return User(self._user["uid"])
34+
35+
def delete(self) -> str:
36+
"""删除剪贴板
37+
38+
:returns: 剪贴板 ID
39+
:rtype: str
40+
"""
41+
return self._post(f"https://www.luogu.com.cn/paste/delete/{self.id}")["id"]
42+
43+
def edit(self, data: str = None, public: bool = None):
44+
"""编辑剪贴板
45+
46+
:param str data: 剪贴板内容
47+
:param bool public: 是否公开
48+
49+
:returns: 剪贴板 ID
50+
:rtype: str
51+
"""
52+
r = self._post(
53+
f"https://www.luogu.com.cn/paste/edit/{self.id}",
54+
{"data": data, "public": public},
55+
)
56+
if data is not None:
57+
self.data = data
58+
if public is not None:
59+
self.public = public
60+
return r["id"]
61+
62+
@classmethod
63+
def new(cls, data: str, public: bool = None) -> "Paste":
64+
"""新建剪贴板
65+
66+
:param str data: 剪贴板内容
67+
:param bool public: 值为真时表示公开剪贴板,否则表示私有剪贴板
68+
69+
:rtype: Session.Paste
70+
"""
71+
r = cls._post(
72+
"https://www.luogu.com.cn/paste/new",
73+
{"data": data, "public": public},
74+
)
75+
return cls(r["id"])

src/luogu/session.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import requests
55

66
from .constants import USER_AGENT
7-
from .models import Paste, Problem, User
7+
from .models.main import Problem, User
8+
from .models.paste import Paste
89
from .utils import get_csrf_token
910

1011

tox.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,3 @@ commands =
1919
[flake8]
2020
max-line-length = 88
2121
extend-ignore = E203
22-
per-file-ignores =
23-
*/luogu/__init__.py: F401

0 commit comments

Comments
 (0)