p123client 0.0.6.9.4__tar.gz → 0.0.6.10__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.4 → p123client-0.0.6.10}/PKG-INFO +162 -27
- {p123client-0.0.6.9.4 → p123client-0.0.6.10}/p123client/client.py +911 -325
- {p123client-0.0.6.9.4 → p123client-0.0.6.10}/p123client/tool/__init__.py +4 -3
- {p123client-0.0.6.9.4 → p123client-0.0.6.10}/pyproject.toml +1 -1
- {p123client-0.0.6.9.4 → p123client-0.0.6.10}/readme.md +161 -26
- {p123client-0.0.6.9.4 → p123client-0.0.6.10}/LICENSE +0 -0
- {p123client-0.0.6.9.4 → p123client-0.0.6.10}/p123client/__init__.py +0 -0
- {p123client-0.0.6.9.4 → p123client-0.0.6.10}/p123client/const.py +0 -0
- {p123client-0.0.6.9.4 → p123client-0.0.6.10}/p123client/exception.py +0 -0
- {p123client-0.0.6.9.4 → p123client-0.0.6.10}/p123client/py.typed +0 -0
- {p123client-0.0.6.9.4 → p123client-0.0.6.10}/p123client/type.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: p123client
|
3
|
-
Version: 0.0.6.
|
3
|
+
Version: 0.0.6.10
|
4
4
|
Summary: Python 123 webdisk client.
|
5
5
|
Home-page: https://github.com/ChenyangGao/p123client
|
6
6
|
License: MIT
|
@@ -56,19 +56,29 @@ pip install -U p123client
|
|
56
56
|
|
57
57
|
## 1. 导入模块和创建实例
|
58
58
|
|
59
|
-
|
59
|
+
首先导入模块
|
60
60
|
|
61
61
|
```python
|
62
62
|
from p123client import P123Client
|
63
63
|
```
|
64
64
|
|
65
|
+
### 1. 用 token 创建实例
|
66
|
+
|
65
67
|
创建客户端对象,需要传入 JWT <kbd>token</kbd>,也就是 `access_token`
|
66
68
|
|
67
69
|
```python
|
70
|
+
# TODO: 访问令牌
|
68
71
|
token = "..."
|
72
|
+
|
69
73
|
client = P123Client(token=token)
|
70
74
|
```
|
71
75
|
|
76
|
+
或者直接写作
|
77
|
+
|
78
|
+
```python
|
79
|
+
client = P123Client(token)
|
80
|
+
```
|
81
|
+
|
72
82
|
不过我更推荐把 <kbd>token</kbd> 写入一个文件中,例如在 `~/123-token.txt`
|
73
83
|
|
74
84
|
```python
|
@@ -77,33 +87,152 @@ from pathlib import Path
|
|
77
87
|
client = P123Client(token=Path("~/123-token.txt").expanduser())
|
78
88
|
```
|
79
89
|
|
80
|
-
|
90
|
+
或者直接写作
|
91
|
+
|
92
|
+
```python
|
93
|
+
client = P123Client(Path("~/123-token.txt").expanduser())
|
94
|
+
```
|
95
|
+
|
96
|
+
### 2. 账号和密码登录
|
97
|
+
|
98
|
+
```python
|
99
|
+
# TODO: 手机号或者邮箱
|
100
|
+
passport = "..."
|
101
|
+
# TODO: 密码
|
102
|
+
password = "..."
|
103
|
+
|
104
|
+
client = P123Client(passport=passport, password=passport)
|
105
|
+
```
|
106
|
+
|
107
|
+
或者直接写作
|
108
|
+
|
109
|
+
```python
|
110
|
+
client = P123Client(passport, passport)
|
111
|
+
```
|
112
|
+
|
113
|
+
### 3. client_id 和 client_secret 登录
|
114
|
+
|
115
|
+
需要先申请成为开发者
|
116
|
+
|
117
|
+
https://123yunpan.yuque.com/org-wiki-123yunpan-muaork/cr6ced/hpengmyg32blkbg8
|
118
|
+
|
119
|
+
```python
|
120
|
+
# TODO: 应用标识,创建应用时分配的 appId
|
121
|
+
client_id = "..."
|
122
|
+
# TODO: 应用密钥,创建应用时分配的 secretId
|
123
|
+
client_secret = "..."
|
124
|
+
|
125
|
+
client = P123Client(client_id=client_id, client_secret=client_secret)
|
126
|
+
```
|
127
|
+
|
128
|
+
或者直接写作
|
81
129
|
|
82
130
|
```python
|
83
|
-
|
84
|
-
password = "..." # 密码
|
85
|
-
client = P123Client(passport, password)
|
131
|
+
client = P123Client(client_id, client_secret)
|
86
132
|
```
|
87
133
|
|
88
|
-
|
134
|
+
### 4. refresh_token 登录
|
89
135
|
|
90
136
|
```python
|
91
|
-
|
137
|
+
# TODO: 刷新令牌
|
138
|
+
refresh_token = "..."
|
139
|
+
|
140
|
+
client = P123Client(refresh_token=refresh_token)
|
141
|
+
```
|
142
|
+
|
143
|
+
或者直接写作
|
144
|
+
|
145
|
+
```python
|
146
|
+
client = P123Client(refresh_token)
|
147
|
+
```
|
148
|
+
|
149
|
+
**注意**:使用 refresh_token(或者说 oauth 登录),只允许访问 open 接口
|
150
|
+
|
151
|
+
### 5. oauth 登录
|
152
|
+
|
153
|
+
需要先去开发者后台设置一下回调链接,审核通过后才可用
|
154
|
+
|
155
|
+
```python
|
156
|
+
# TODO: 应用标识,创建应用时分配的 appId
|
157
|
+
client_id = "..."
|
158
|
+
# TODO: 应用密钥,创建应用时分配的 secretId
|
159
|
+
client_secret = "..."
|
160
|
+
# TODO: 回调链接
|
161
|
+
redirect_uri = "..."
|
162
|
+
# TODO: 访问令牌
|
163
|
+
token = "..."
|
164
|
+
|
165
|
+
resp = P123Client.login_with_oauth(
|
166
|
+
client_id=client_id,
|
167
|
+
client_secret=client_secret,
|
168
|
+
redirect_uri=redirect_uri,
|
169
|
+
token=token,
|
170
|
+
)
|
171
|
+
client = P123Client(
|
172
|
+
client_id=client_id,
|
173
|
+
client_secret=client_secret,
|
174
|
+
token=resp["access_token"],
|
175
|
+
refresh_token=resp["refresh_token"],
|
176
|
+
)
|
177
|
+
```
|
178
|
+
|
179
|
+
### 6. 扫码登录
|
180
|
+
|
181
|
+
当你什么都不传时,就会要求你扫码登录
|
182
|
+
|
183
|
+
```python
|
184
|
+
client = P123Client()
|
185
|
+
```
|
186
|
+
|
187
|
+
### 7. 用 token 来扫码新的 token
|
188
|
+
|
189
|
+
你可以用一个 token 去扫码获得另一个 token(仅当不是 oauth 登录时)
|
190
|
+
|
191
|
+
```python
|
192
|
+
client_new = client.login_another()
|
193
|
+
```
|
194
|
+
|
195
|
+
另外你也可以用 oauth 登录,虽然仅能访问 open 接口,但是 QPS 上放得更宽
|
196
|
+
|
197
|
+
下面我用 clouddrive 在 123 网盘上用 oauth 登录举例说明第三方挂载应用登录:
|
198
|
+
|
199
|
+
https://123yunpan.yuque.com/org-wiki-123yunpan-muaork/cr6ced/kf05anzt1r0qnudd
|
200
|
+
|
201
|
+
```python
|
202
|
+
import requests
|
203
|
+
from urllib.parse import parse_qsl, urlsplit
|
204
|
+
|
205
|
+
resp = client.login_oauth_authorize({
|
206
|
+
"client_id": "7c278c60da9e43848c45ff8e6fa9da0a",
|
207
|
+
"redirect_uri": "https://redirect123pan.zhenyunpan.com/redirect_pan123",
|
208
|
+
"accessToken": client.token,
|
209
|
+
"state": "http://localhost:19798",
|
210
|
+
})
|
211
|
+
with requests.get(resp["url"], allow_redirects=False, stream=True) as response:
|
212
|
+
resp = dict(parse_qsl(urlsplit(response.headers["location"]).query))
|
213
|
+
client_new = P123Client(token=resp["access_token"], refresh_token=resp["refresh_token"])
|
214
|
+
```
|
215
|
+
|
216
|
+
### 8. 关于 token 的结构
|
217
|
+
|
218
|
+
<kbd>token</kbd> 中包含了一些信息,以 "." 进行分割,可以拆成 3 个部分:
|
219
|
+
|
220
|
+
- JWT 算法(经过 base64 加密)
|
221
|
+
- 用户信息(经过 base64 加密)
|
222
|
+
- 签名字符串
|
223
|
+
|
224
|
+
```python
|
225
|
+
from base64 import urlsafe_b64decode
|
92
226
|
from json import loads
|
93
227
|
|
94
228
|
token = client.token
|
95
229
|
|
96
230
|
method, user_info, sign = token.split(".", 2)
|
97
|
-
print("JWT 算法:", loads(
|
98
|
-
print("用户信息:", loads(
|
231
|
+
print("JWT 算法:", loads(urlsafe_b64decode(method)))
|
232
|
+
print("用户信息:", loads(urlsafe_b64decode(user_info+"==")))
|
233
|
+
print("签名:", sign)
|
99
234
|
```
|
100
235
|
|
101
|
-
当你需要刷新此 <kbd>token</kbd> 时:
|
102
|
-
|
103
|
-
- 要么利用另一个 `refresh_token`
|
104
|
-
- 要么直接用这个 <kbd>token</kbd> 授权一次扫码,即可获得新的 <kbd>token</kbd>
|
105
|
-
- 要么重新用账号和密码登录
|
106
|
-
|
107
236
|
## 2. 接口调用
|
108
237
|
|
109
238
|
所有需要直接或间接执行 HTTP 请求的接口,都有同步和异步的调用方式,且默认是采用 POST 发送 JSON 请求数据
|
@@ -270,10 +399,10 @@ print(get_downurl(client, "123://torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512
|
|
270
399
|
|
271
400
|
### 4. 直链服务
|
272
401
|
|
273
|
-
需要先安装 [fastapi](https://pypi.org/project/
|
402
|
+
需要先安装 [fastapi](https://pypi.org/project/blacksheep/)
|
274
403
|
|
275
404
|
```console
|
276
|
-
pip install '
|
405
|
+
pip install 'blacksheep[uvicorn]'
|
277
406
|
```
|
278
407
|
|
279
408
|
然后启动如下服务,就可以访问以获取直链了
|
@@ -287,29 +416,30 @@ http://localhost:8123/torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512d9661f6243b
|
|
287
416
|
http://localhost:8123/torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512d9661f6243bdd
|
288
417
|
|
289
418
|
```python
|
290
|
-
from
|
291
|
-
from fastapi.responses import JSONResponse, RedirectResponse
|
419
|
+
from blacksheep import json, redirect, Application, Request
|
292
420
|
from p123client import P123Client
|
293
421
|
from p123client.tool import get_downurl
|
294
422
|
|
295
423
|
# TODO: 改成你自己的账户和密码
|
296
|
-
client = P123Client(passport="
|
424
|
+
client = P123Client(passport="15888364500", password="Gchenyang123")
|
425
|
+
## 或者
|
426
|
+
# from pathlib import Path
|
427
|
+
# client = P123Client(Path("~/123-token.txt").expanduser())
|
297
428
|
|
298
|
-
app =
|
429
|
+
app = Application(show_error_details=__debug__)
|
299
430
|
|
300
|
-
@app.
|
301
|
-
@app.head("/{uri:path}")
|
431
|
+
@app.router.route("/{path:uri}", methods=["GET", "HEAD"])
|
302
432
|
async def index(request: Request, uri: str):
|
303
433
|
try:
|
304
434
|
payload = int(uri)
|
305
435
|
except ValueError:
|
306
436
|
if uri.count("|") < 2:
|
307
|
-
return
|
437
|
+
return json({"state": False, "message": f"bad uri: {uri!r}"}, 500)
|
308
438
|
payload = uri
|
309
439
|
if s3_key_flag := request.url.query:
|
310
|
-
payload += "?" + s3_key_flag
|
440
|
+
payload += "?" + s3_key_flag.decode("ascii")
|
311
441
|
url = await get_downurl(client, payload, quoted=False, async_=True)
|
312
|
-
return
|
442
|
+
return redirect(url)
|
313
443
|
|
314
444
|
if __name__ == "__main__":
|
315
445
|
from uvicorn import run
|
@@ -413,4 +543,9 @@ client.fs_list_new({
|
|
413
543
|
client.fs_sync_log()
|
414
544
|
```
|
415
545
|
|
546
|
+
## 其它资源
|
547
|
+
|
548
|
+
- 如果你需要更详细的文档,特别是关于各种接口的信息,可以阅读
|
549
|
+
|
550
|
+
[https://p123client.readthedocs.io/en/latest/](https://p123client.readthedocs.io/en/latest/)
|
416
551
|
|