p123client 0.0.6.9.2__tar.gz → 0.0.6.9.4__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.9.2 → p123client-0.0.6.9.4}/PKG-INFO +119 -23
- {p123client-0.0.6.9.2 → p123client-0.0.6.9.4}/p123client/client.py +2451 -726
- p123client-0.0.6.9.4/p123client/const.py +13 -0
- p123client-0.0.6.9.4/p123client/exception.py +117 -0
- {p123client-0.0.6.9.2 → p123client-0.0.6.9.4}/pyproject.toml +2 -1
- {p123client-0.0.6.9.2 → p123client-0.0.6.9.4}/readme.md +117 -22
- p123client-0.0.6.9.2/p123client/const.py +0 -4
- p123client-0.0.6.9.2/p123client/exception.py +0 -60
- {p123client-0.0.6.9.2 → p123client-0.0.6.9.4}/LICENSE +0 -0
- {p123client-0.0.6.9.2 → p123client-0.0.6.9.4}/p123client/__init__.py +0 -0
- {p123client-0.0.6.9.2 → p123client-0.0.6.9.4}/p123client/py.typed +0 -0
- {p123client-0.0.6.9.2 → p123client-0.0.6.9.4}/p123client/tool/__init__.py +0 -0
- {p123client-0.0.6.9.2 → p123client-0.0.6.9.4}/p123client/type.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: p123client
|
3
|
-
Version: 0.0.6.9.
|
3
|
+
Version: 0.0.6.9.4
|
4
4
|
Summary: Python 123 webdisk client.
|
5
5
|
Home-page: https://github.com/ChenyangGao/p123client
|
6
6
|
License: MIT
|
@@ -33,6 +33,7 @@ Requires-Dist: python-http_request (>=0.0.7)
|
|
33
33
|
Requires-Dist: python-httpfile (>=0.0.5)
|
34
34
|
Requires-Dist: python-iterutils (>=0.2.4.1)
|
35
35
|
Requires-Dist: python-property (>=0.0.3)
|
36
|
+
Requires-Dist: qrcode
|
36
37
|
Requires-Dist: yarl
|
37
38
|
Project-URL: Repository, https://github.com/ChenyangGao/p123client
|
38
39
|
Description-Content-Type: text/markdown
|
@@ -45,7 +46,7 @@ Description-Content-Type: text/markdown
|
|
45
46
|

|
46
47
|

|
47
48
|
|
48
|
-
## 安装
|
49
|
+
## 0. 安装
|
49
50
|
|
50
51
|
你可以从 [pypi](https://pypi.org/project/p123client/) 安装最新版本
|
51
52
|
|
@@ -53,9 +54,7 @@ Description-Content-Type: text/markdown
|
|
53
54
|
pip install -U p123client
|
54
55
|
```
|
55
56
|
|
56
|
-
##
|
57
|
-
|
58
|
-
### 1. 导入模块和创建实例
|
57
|
+
## 1. 导入模块和创建实例
|
59
58
|
|
60
59
|
导入模块
|
61
60
|
|
@@ -63,13 +62,21 @@ pip install -U p123client
|
|
63
62
|
from p123client import P123Client
|
64
63
|
```
|
65
64
|
|
66
|
-
创建客户端对象,需要传入 JWT <kbd>token</kbd
|
65
|
+
创建客户端对象,需要传入 JWT <kbd>token</kbd>,也就是 `access_token`
|
67
66
|
|
68
67
|
```python
|
69
68
|
token = "..."
|
70
69
|
client = P123Client(token=token)
|
71
70
|
```
|
72
71
|
|
72
|
+
不过我更推荐把 <kbd>token</kbd> 写入一个文件中,例如在 `~/123-token.txt`
|
73
|
+
|
74
|
+
```python
|
75
|
+
from pathlib import Path
|
76
|
+
|
77
|
+
client = P123Client(token=Path("~/123-token.txt").expanduser())
|
78
|
+
```
|
79
|
+
|
73
80
|
你也可以用账户和密码登录
|
74
81
|
|
75
82
|
```python
|
@@ -78,7 +85,26 @@ password = "..." # 密码
|
|
78
85
|
client = P123Client(passport, password)
|
79
86
|
```
|
80
87
|
|
81
|
-
|
88
|
+
它的这个 <kbd>token</kbd>,就包含了有关信息
|
89
|
+
|
90
|
+
```python
|
91
|
+
from base64 import b64decode
|
92
|
+
from json import loads
|
93
|
+
|
94
|
+
token = client.token
|
95
|
+
|
96
|
+
method, user_info, sign = token.split(".", 2)
|
97
|
+
print("JWT 算法:", loads(b64decode(method)))
|
98
|
+
print("用户信息:", loads(b64decode(user_info+"==")))
|
99
|
+
```
|
100
|
+
|
101
|
+
当你需要刷新此 <kbd>token</kbd> 时:
|
102
|
+
|
103
|
+
- 要么利用另一个 `refresh_token`
|
104
|
+
- 要么直接用这个 <kbd>token</kbd> 授权一次扫码,即可获得新的 <kbd>token</kbd>
|
105
|
+
- 要么重新用账号和密码登录
|
106
|
+
|
107
|
+
## 2. 接口调用
|
82
108
|
|
83
109
|
所有需要直接或间接执行 HTTP 请求的接口,都有同步和异步的调用方式,且默认是采用 POST 发送 JSON 请求数据
|
84
110
|
|
@@ -177,7 +203,7 @@ class MyCustom123Client(P123Client):
|
|
177
203
|
)
|
178
204
|
```
|
179
205
|
|
180
|
-
|
206
|
+
## 3. 检查响应
|
181
207
|
|
182
208
|
接口被调用后,如果返回的是 dict 类型的数据(说明原本是 JSON),则可以用 `p123client.check_response` 执行检查。首先会查看其中名为 "code" 的键的对应值,如果为 0 或 200 或者不存在,则原样返回被检查的数据;否则,抛出一个 `p123client.P123OSError` 的实例。
|
183
209
|
|
@@ -190,7 +216,7 @@ data = check_response(client.method(payload))
|
|
190
216
|
data = check_response(await client.method(payload, async_=True))
|
191
217
|
```
|
192
218
|
|
193
|
-
|
219
|
+
## 4. 辅助工具
|
194
220
|
|
195
221
|
一些简单的封装工具可能是必要的,特别是那种实现起来代码量比较少,可以封装成单个函数的。我把平常使用过程中,积累的一些经验具体化为一组工具函数。这些工具函数分别有着不同的功能,如果组合起来使用,或许能解决很多问题。
|
196
222
|
|
@@ -198,7 +224,9 @@ data = check_response(await client.method(payload, async_=True))
|
|
198
224
|
from p123client import tool
|
199
225
|
```
|
200
226
|
|
201
|
-
|
227
|
+
## 5. 学习案例
|
228
|
+
|
229
|
+
### 1. 创建自定义 uri
|
202
230
|
|
203
231
|
```python
|
204
232
|
from p123client import P123Client
|
@@ -212,7 +240,7 @@ file_id = 15688945
|
|
212
240
|
print(make_uri(client, file_id))
|
213
241
|
```
|
214
242
|
|
215
|
-
|
243
|
+
### 2. 由自定义 uri 转存文件到你的网盘
|
216
244
|
|
217
245
|
```python
|
218
246
|
from p123client import P123Client
|
@@ -225,7 +253,7 @@ uri = "123://torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512d9661f6243bdd"
|
|
225
253
|
print(upload_uri(client, uri, duplicate=1))
|
226
254
|
```
|
227
255
|
|
228
|
-
|
256
|
+
### 3. 由自定义 uri 获取下载直链
|
229
257
|
|
230
258
|
```python
|
231
259
|
from p123client import P123Client
|
@@ -240,7 +268,7 @@ print(get_downurl(client, "123://torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512
|
|
240
268
|
print(get_downurl(client, "123://torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512d9661f6243bdd"))
|
241
269
|
```
|
242
270
|
|
243
|
-
|
271
|
+
### 4. 直链服务
|
244
272
|
|
245
273
|
需要先安装 [fastapi](https://pypi.org/project/fastapi/)
|
246
274
|
|
@@ -289,9 +317,11 @@ if __name__ == "__main__":
|
|
289
317
|
run(app, host="0.0.0.0", port=8123)
|
290
318
|
```
|
291
319
|
|
292
|
-
|
320
|
+
### 5. 遍历文件列表
|
293
321
|
|
294
|
-
|
322
|
+
导出的文件信息中,有个 `"uri"`,表示文件的自定义链接,前面以 `123://` 开头,你可以替换成 302 服务的地址,例如 `http://localhost:8123/`
|
323
|
+
|
324
|
+
#### 1. 遍历网盘中的文件列表
|
295
325
|
|
296
326
|
```python
|
297
327
|
from p123client import P123Client
|
@@ -300,21 +330,87 @@ from p123client.tool import iterdir
|
|
300
330
|
# TODO: 改成你自己的账户和密码
|
301
331
|
client = P123Client(passport="手机号或邮箱", password="登录密码")
|
302
332
|
|
303
|
-
for info in iterdir(client, parent_id=0, max_depth=-1
|
333
|
+
for info in iterdir(client, parent_id=0, max_depth=-1):
|
304
334
|
print(info)
|
305
335
|
```
|
306
336
|
|
307
|
-
|
337
|
+
#### 2. 遍历分享中的文件列表,不含目录
|
308
338
|
|
309
339
|
```python
|
310
340
|
from p123client.tool import share_iterdir
|
311
341
|
|
312
|
-
#
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
342
|
+
# NOTE: 无需登录
|
343
|
+
for info in share_iterdir(
|
344
|
+
"https://www.123684.com/s/oec7Vv-CIYWh?提取码:ZY4K",
|
345
|
+
max_depth=-1,
|
346
|
+
predicate=lambda a: not a["is_dir"],
|
347
|
+
):
|
318
348
|
print(info)
|
319
349
|
```
|
320
350
|
|
351
|
+
#### 3. 导出分享中的文件列表到本地 .json 文件
|
352
|
+
|
353
|
+
```python
|
354
|
+
from orjson import dumps
|
355
|
+
from p123client.tool import share_iterdir
|
356
|
+
|
357
|
+
def export_share_files_json(
|
358
|
+
link: str,
|
359
|
+
path: str = "",
|
360
|
+
cooldown: float = 0,
|
361
|
+
):
|
362
|
+
"""把分享链接中的文件列表导出到 json 文件
|
363
|
+
|
364
|
+
:param link: 分享链接,可以包含提取码
|
365
|
+
:param path: 保存的路径,如果不提供则自动确定
|
366
|
+
:param cooldown: 两次调用之间,冷却的时间(用两次调用开始时的时间差,而不是一次完成到下一次开始的时间差)
|
367
|
+
"""
|
368
|
+
print("< 将拉取:", link)
|
369
|
+
ls: list[dict] = []
|
370
|
+
for i, a in enumerate(share_iterdir(link, max_depth=-1, cooldown=cooldown), 1):
|
371
|
+
ls.append(a)
|
372
|
+
print(i, a)
|
373
|
+
if ls:
|
374
|
+
info = ls[0]
|
375
|
+
if not path:
|
376
|
+
suffix = f"@{info['ShareKey']},{info['SharePwd']}.json"
|
377
|
+
path = f"{info['name'][:255-len(suffix)]}{suffix}"
|
378
|
+
open(path, "wb").write(dumps(ls))
|
379
|
+
print()
|
380
|
+
print("> 已拉取:", link)
|
381
|
+
print("> 已保存:", path)
|
382
|
+
|
383
|
+
export_share_files_json("https://www.123684.com/s/oec7Vv-CIYWh?提取码:ZY4K")
|
384
|
+
```
|
385
|
+
|
386
|
+
### 6. 最新的操作记录
|
387
|
+
|
388
|
+
在网盘中,你可以按更新时间逆序查询,即可得到最新上传的文件列表
|
389
|
+
|
390
|
+
```python
|
391
|
+
client.fs_list_new({
|
392
|
+
"orderBy": "update_time",
|
393
|
+
"orderDirection": "desc",
|
394
|
+
"SearchData": ".",
|
395
|
+
})
|
396
|
+
```
|
397
|
+
|
398
|
+
更一般的,你可以在[同步空间](https://www.123pan.com/SynchronousSpace/main)中执行文件操作。
|
399
|
+
|
400
|
+
而在拉取文件列表时,需要指定
|
401
|
+
|
402
|
+
```python
|
403
|
+
client.fs_list_new({
|
404
|
+
"operateType": "SyncSpacePage",
|
405
|
+
"event": "syncFileList",
|
406
|
+
"RequestSource": 1,
|
407
|
+
})
|
408
|
+
```
|
409
|
+
|
410
|
+
同步空间中的增删改操作都有[操作记录](https://www.123pan.com/SynchronousSpace/record),你可以用接口
|
411
|
+
|
412
|
+
```python
|
413
|
+
client.fs_sync_log()
|
414
|
+
```
|
415
|
+
|
416
|
+
|