p123client 0.0.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- p123client-0.0.6/LICENSE +21 -0
- p123client-0.0.6/PKG-INFO +320 -0
- p123client-0.0.6/p123client/__init__.py +10 -0
- p123client-0.0.6/p123client/client.py +7082 -0
- p123client-0.0.6/p123client/const.py +4 -0
- p123client-0.0.6/p123client/exception.py +49 -0
- p123client-0.0.6/p123client/py.typed +0 -0
- p123client-0.0.6/p123client/tool/__init__.py +484 -0
- p123client-0.0.6/p123client/type.py +4 -0
- p123client-0.0.6/pyproject.toml +50 -0
- p123client-0.0.6/readme.md +280 -0
p123client-0.0.6/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 ChenyangGao <https://github.com/ChenyangGao>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
@@ -0,0 +1,320 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: p123client
|
3
|
+
Version: 0.0.6
|
4
|
+
Summary: Python 123 webdisk client.
|
5
|
+
Home-page: https://github.com/ChenyangGao/python-123-client
|
6
|
+
License: MIT
|
7
|
+
Keywords: 123,webdisk,client
|
8
|
+
Author: ChenyangGao
|
9
|
+
Author-email: wosiwujm@gmail.com
|
10
|
+
Requires-Python: >=3.12,<4.0
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
12
|
+
Classifier: Intended Audience :: Developers
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
14
|
+
Classifier: Operating System :: OS Independent
|
15
|
+
Classifier: Programming Language :: Python
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
19
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
20
|
+
Classifier: Topic :: Software Development
|
21
|
+
Classifier: Topic :: Software Development :: Libraries
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
23
|
+
Requires-Dist: aiofile
|
24
|
+
Requires-Dist: httpx
|
25
|
+
Requires-Dist: httpx_request (>=0.1.2)
|
26
|
+
Requires-Dist: multidict
|
27
|
+
Requires-Dist: orjson
|
28
|
+
Requires-Dist: python-asynctools (>=0.0.8.2)
|
29
|
+
Requires-Dist: python-encode_uri (>=0.0.3)
|
30
|
+
Requires-Dist: python-filewrap (>=0.2.6.1)
|
31
|
+
Requires-Dist: python-hashtools (>=0.0.3.3)
|
32
|
+
Requires-Dist: python-http_request (>=0.0.7)
|
33
|
+
Requires-Dist: python-httpfile (>=0.0.5)
|
34
|
+
Requires-Dist: python-iterutils (>=0.2)
|
35
|
+
Requires-Dist: python-property (>=0.0.3)
|
36
|
+
Requires-Dist: yarl
|
37
|
+
Project-URL: Repository, https://github.com/ChenyangGao/python-123-client
|
38
|
+
Description-Content-Type: text/markdown
|
39
|
+
|
40
|
+
# Python 123 网盘客户端
|
41
|
+
|
42
|
+

|
43
|
+

|
44
|
+

|
45
|
+

|
46
|
+

|
47
|
+
|
48
|
+
## 安装
|
49
|
+
|
50
|
+
你可以从 [pypi](https://pypi.org/project/p123client/) 安装最新版本
|
51
|
+
|
52
|
+
```console
|
53
|
+
pip install -U p123client
|
54
|
+
```
|
55
|
+
|
56
|
+
## 入门介绍
|
57
|
+
|
58
|
+
### 1. 导入模块和创建实例
|
59
|
+
|
60
|
+
导入模块
|
61
|
+
|
62
|
+
```python
|
63
|
+
from p123client import P123Client
|
64
|
+
```
|
65
|
+
|
66
|
+
创建客户端对象,需要传入 JWT <kbd>token</kbd>
|
67
|
+
|
68
|
+
```python
|
69
|
+
token = "..."
|
70
|
+
client = P123Client(token=token)
|
71
|
+
```
|
72
|
+
|
73
|
+
你也可以用账户和密码登录
|
74
|
+
|
75
|
+
```python
|
76
|
+
passport = "..." # 手机号或者邮箱
|
77
|
+
password = "..." # 密码
|
78
|
+
client = P123Client(passport, password)
|
79
|
+
```
|
80
|
+
|
81
|
+
### 2. 接口调用
|
82
|
+
|
83
|
+
所有需要直接或间接执行 HTTP 请求的接口,都有同步和异步的调用方式,且默认是采用 POST 发送 JSON 请求数据
|
84
|
+
|
85
|
+
```python
|
86
|
+
# 同步调用
|
87
|
+
client.method(payload)
|
88
|
+
client.method(payload, async_=False)
|
89
|
+
|
90
|
+
# 异步调用
|
91
|
+
await client.method(payload, async_=True)
|
92
|
+
```
|
93
|
+
|
94
|
+
从根本上讲,除了几个 `staticmethod`,它们都会调用 `P123Client.request`
|
95
|
+
|
96
|
+
```python
|
97
|
+
url = "https://www.123pan.com/api/someapi"
|
98
|
+
response = client.request(url=url, json={...})
|
99
|
+
```
|
100
|
+
|
101
|
+
当你需要构建自己的扩展模块,以增加一些新的 123 web 接口时,就需要用到此方法了
|
102
|
+
|
103
|
+
```python
|
104
|
+
from collections.abc import Coroutine
|
105
|
+
from typing import overload, Any, Literal
|
106
|
+
|
107
|
+
from p123client import P123Client
|
108
|
+
|
109
|
+
class MyCustom123Client(P123Client):
|
110
|
+
|
111
|
+
@overload
|
112
|
+
def foo(
|
113
|
+
self,
|
114
|
+
payload: dict,
|
115
|
+
/,
|
116
|
+
async_: Literal[False] = False,
|
117
|
+
**request_kwargs,
|
118
|
+
) -> dict:
|
119
|
+
...
|
120
|
+
@overload
|
121
|
+
def foo(
|
122
|
+
self,
|
123
|
+
payload: dict,
|
124
|
+
/,
|
125
|
+
async_: Literal[True],
|
126
|
+
**request_kwargs,
|
127
|
+
) -> Coroutine[Any, Any, dict]:
|
128
|
+
...
|
129
|
+
def foo(
|
130
|
+
self,
|
131
|
+
payload: dict,
|
132
|
+
/,
|
133
|
+
async_: bool = False,
|
134
|
+
**request_kwargs,
|
135
|
+
) -> dict | Coroutine[Any, Any, dict]:
|
136
|
+
api = "https://www.123pan.com/api/foo"
|
137
|
+
return self.request(
|
138
|
+
api,
|
139
|
+
method="GET",
|
140
|
+
params=payload,
|
141
|
+
async_=async_,
|
142
|
+
**request_kwargs,
|
143
|
+
)
|
144
|
+
|
145
|
+
@overload
|
146
|
+
def bar(
|
147
|
+
self,
|
148
|
+
payload: dict,
|
149
|
+
/,
|
150
|
+
async_: Literal[False] = False,
|
151
|
+
**request_kwargs,
|
152
|
+
) -> dict:
|
153
|
+
...
|
154
|
+
@overload
|
155
|
+
def bar(
|
156
|
+
self,
|
157
|
+
payload: dict,
|
158
|
+
/,
|
159
|
+
async_: Literal[True],
|
160
|
+
**request_kwargs,
|
161
|
+
) -> Coroutine[Any, Any, dict]:
|
162
|
+
...
|
163
|
+
def bar(
|
164
|
+
self,
|
165
|
+
payload: dict,
|
166
|
+
/,
|
167
|
+
async_: bool = False,
|
168
|
+
**request_kwargs,
|
169
|
+
) -> dict | Coroutine[Any, Any, dict]:
|
170
|
+
api = "https://www.123pan.com/api/bar"
|
171
|
+
return self.request(
|
172
|
+
api,
|
173
|
+
method="POST",
|
174
|
+
json=payload,
|
175
|
+
async_=async_,
|
176
|
+
**request_kwargs,
|
177
|
+
)
|
178
|
+
```
|
179
|
+
|
180
|
+
### 3. 检查响应
|
181
|
+
|
182
|
+
接口被调用后,如果返回的是 dict 类型的数据(说明原本是 JSON),则可以用 `p123client.check_response` 执行检查。首先会查看其中名为 "code" 的键的对应值,如果为 0 或 200 或者不存在,则原样返回被检查的数据;否则,抛出一个 `p123client.P123OSError` 的实例。
|
183
|
+
|
184
|
+
```python
|
185
|
+
from p123client import check_response
|
186
|
+
|
187
|
+
# 检查同步调用
|
188
|
+
data = check_response(client.method(payload))
|
189
|
+
# 检查异步调用
|
190
|
+
data = check_response(await client.method(payload, async_=True))
|
191
|
+
```
|
192
|
+
|
193
|
+
### 4. 辅助工具
|
194
|
+
|
195
|
+
一些简单的封装工具可能是必要的,特别是那种实现起来代码量比较少,可以封装成单个函数的。我把平常使用过程中,积累的一些经验具体化为一组工具函数。这些工具函数分别有着不同的功能,如果组合起来使用,或许能解决很多问题。
|
196
|
+
|
197
|
+
```python
|
198
|
+
from p123client import tool
|
199
|
+
```
|
200
|
+
|
201
|
+
#### 1. 创建自定义 uri
|
202
|
+
|
203
|
+
```python
|
204
|
+
from p123client import P123Client
|
205
|
+
from p123client.tool import make_uri
|
206
|
+
|
207
|
+
# TODO: 改成你自己的账户和密码
|
208
|
+
client = P123Client(passport="手机号或邮箱", password="登录密码")
|
209
|
+
|
210
|
+
# TODO: 请改成你要处理的文件 id
|
211
|
+
file_id = 15688945
|
212
|
+
print(make_uri(client, file_id))
|
213
|
+
```
|
214
|
+
|
215
|
+
#### 2. 由自定义 uri 转存文件到你的网盘
|
216
|
+
|
217
|
+
```python
|
218
|
+
from p123client import P123Client
|
219
|
+
from p123client.tool import upload_uri
|
220
|
+
|
221
|
+
# TODO: 改成你自己的账户和密码
|
222
|
+
client = P123Client(passport="手机号或邮箱", password="登录密码")
|
223
|
+
|
224
|
+
uri = "123://torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512d9661f6243bdd"
|
225
|
+
print(upload_uri(client, uri, duplicate=1))
|
226
|
+
```
|
227
|
+
|
228
|
+
#### 3. 由自定义 uri 获取下载直链
|
229
|
+
|
230
|
+
```python
|
231
|
+
from p123client import P123Client
|
232
|
+
from p123client.tool import get_downurl
|
233
|
+
|
234
|
+
# TODO: 改成你自己的账户和密码
|
235
|
+
client = P123Client(passport="手机号或邮箱", password="登录密码")
|
236
|
+
|
237
|
+
# 带 s3_key_flag
|
238
|
+
print(get_downurl(client, "123://torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512d9661f6243bdd?1812602326-0"))
|
239
|
+
# 不带 s3_key_flag(会转存)
|
240
|
+
print(get_downurl(client, "123://torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512d9661f6243bdd"))
|
241
|
+
```
|
242
|
+
|
243
|
+
#### 4. 直链服务
|
244
|
+
|
245
|
+
需要先安装 [fastapi](https://pypi.org/project/fastapi/)
|
246
|
+
|
247
|
+
```console
|
248
|
+
pip install 'fastapi[uvicorn]'
|
249
|
+
```
|
250
|
+
|
251
|
+
然后启动如下服务,就可以访问以获取直链了
|
252
|
+
|
253
|
+
**带 s3_key_flag**
|
254
|
+
|
255
|
+
http://localhost:8123/torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512d9661f6243bdd?1812602326-0
|
256
|
+
|
257
|
+
**不带 s3_key_flag(会转存)**
|
258
|
+
|
259
|
+
http://localhost:8123/torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512d9661f6243bdd
|
260
|
+
|
261
|
+
```python
|
262
|
+
from fastapi import FastAPI, Request
|
263
|
+
from fastapi.responses import JSONResponse, RedirectResponse
|
264
|
+
from p123client import P123Client
|
265
|
+
from p123client.tool import get_downurl
|
266
|
+
|
267
|
+
# TODO: 改成你自己的账户和密码
|
268
|
+
client = P123Client(passport="手机号或邮箱", password="登录密码")
|
269
|
+
|
270
|
+
app = FastAPI(debug=True)
|
271
|
+
|
272
|
+
@app.get("/{uri:path}")
|
273
|
+
@app.head("/{uri:path}")
|
274
|
+
async def index(request: Request, uri: str):
|
275
|
+
try:
|
276
|
+
payload = int(uri)
|
277
|
+
except ValueError:
|
278
|
+
if uri.count("|") < 2:
|
279
|
+
return JSONResponse({"state": False, "message": f"bad uri: {uri!r}"}, 500)
|
280
|
+
payload = uri
|
281
|
+
if s3_key_flag := request.url.query:
|
282
|
+
payload += "?" + s3_key_flag
|
283
|
+
url = await get_downurl(client, payload, quoted=False, async_=True)
|
284
|
+
return RedirectResponse(url, 302)
|
285
|
+
|
286
|
+
if __name__ == "__main__":
|
287
|
+
from uvicorn import run
|
288
|
+
|
289
|
+
run(app, host="0.0.0.0", port=8123)
|
290
|
+
```
|
291
|
+
|
292
|
+
#### 5. 遍历文件列表
|
293
|
+
|
294
|
+
**遍历网盘中的文件列表**
|
295
|
+
|
296
|
+
```python
|
297
|
+
from p123client import P123Client
|
298
|
+
from p123client.tool import iterdir
|
299
|
+
|
300
|
+
# TODO: 改成你自己的账户和密码
|
301
|
+
client = P123Client(passport="手机号或邮箱", password="登录密码")
|
302
|
+
|
303
|
+
for info in iterdir(client, parent_id=0, max_depth=-1, predicate=lambda a: not a["is_dir"]):
|
304
|
+
print(info)
|
305
|
+
```
|
306
|
+
|
307
|
+
**遍历分享中的文件列表(无需登录)**
|
308
|
+
|
309
|
+
```python
|
310
|
+
from p123client.tool import share_iterdir
|
311
|
+
|
312
|
+
# TODO: 分享码
|
313
|
+
share_key = "g0n0Vv-2sbI"
|
314
|
+
# TODO: 密码
|
315
|
+
share_pwd = ""
|
316
|
+
|
317
|
+
for info in share_iterdir(share_key, share_pwd, parent_id=0, max_depth=-1, predicate=lambda a: not a["is_dir"]):
|
318
|
+
print(info)
|
319
|
+
```
|
320
|
+
|