p123client 0.0.6.9.3__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: p123client
3
- Version: 0.0.6.9.3
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
@@ -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
@@ -55,46 +56,183 @@ pip install -U p123client
55
56
 
56
57
  ## 1. 导入模块和创建实例
57
58
 
58
- 导入模块
59
+ 首先导入模块
59
60
 
60
61
  ```python
61
62
  from p123client import P123Client
62
63
  ```
63
64
 
65
+ ### 1. 用 token 创建实例
66
+
64
67
  创建客户端对象,需要传入 JWT <kbd>token</kbd>,也就是 `access_token`
65
68
 
66
69
  ```python
70
+ # TODO: 访问令牌
67
71
  token = "..."
72
+
68
73
  client = P123Client(token=token)
69
74
  ```
70
75
 
71
- 你也可以用账户和密码登录
76
+ 或者直接写作
77
+
78
+ ```python
79
+ client = P123Client(token)
80
+ ```
81
+
82
+ 不过我更推荐把 <kbd>token</kbd> 写入一个文件中,例如在 `~/123-token.txt`
83
+
84
+ ```python
85
+ from pathlib import Path
86
+
87
+ client = P123Client(token=Path("~/123-token.txt").expanduser())
88
+ ```
89
+
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
+ 或者直接写作
129
+
130
+ ```python
131
+ client = P123Client(client_id, client_secret)
132
+ ```
133
+
134
+ ### 4. refresh_token 登录
135
+
136
+ ```python
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
72
200
 
73
201
  ```python
74
- passport = "..." # 手机号或者邮箱
75
- password = "..." # 密码
76
- client = P123Client(passport, password)
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"])
77
214
  ```
78
215
 
79
- 它的这个 <kbd>token</kbd>,就包含了有关信息
216
+ ### 8. 关于 token 的结构
217
+
218
+ <kbd>token</kbd> 中包含了一些信息,以 "." 进行分割,可以拆成 3 个部分:
219
+
220
+ - JWT 算法(经过 base64 加密)
221
+ - 用户信息(经过 base64 加密)
222
+ - 签名字符串
80
223
 
81
224
  ```python
82
- from base64 import b64decode
225
+ from base64 import urlsafe_b64decode
83
226
  from json import loads
84
227
 
85
228
  token = client.token
86
229
 
87
230
  method, user_info, sign = token.split(".", 2)
88
- print("JWT 算法:", loads(b64decode(method)))
89
- print("用户信息:", loads(b64decode(user_info+"==")))
231
+ print("JWT 算法:", loads(urlsafe_b64decode(method)))
232
+ print("用户信息:", loads(urlsafe_b64decode(user_info+"==")))
233
+ print("签名:", sign)
90
234
  ```
91
235
 
92
- 当你需要刷新此 <kbd>token</kbd> 时:
93
-
94
- - 要么利用另一个 `refresh_token`
95
- - 要么直接用这个 <kbd>token</kbd> 授权一次扫码,即可获得新的 <kbd>token</kbd>
96
- - 要么重新用账号和密码登录
97
-
98
236
  ## 2. 接口调用
99
237
 
100
238
  所有需要直接或间接执行 HTTP 请求的接口,都有同步和异步的调用方式,且默认是采用 POST 发送 JSON 请求数据
@@ -261,10 +399,10 @@ print(get_downurl(client, "123://torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512
261
399
 
262
400
  ### 4. 直链服务
263
401
 
264
- 需要先安装 [fastapi](https://pypi.org/project/fastapi/)
402
+ 需要先安装 [fastapi](https://pypi.org/project/blacksheep/)
265
403
 
266
404
  ```console
267
- pip install 'fastapi[uvicorn]'
405
+ pip install 'blacksheep[uvicorn]'
268
406
  ```
269
407
 
270
408
  然后启动如下服务,就可以访问以获取直链了
@@ -278,29 +416,30 @@ http://localhost:8123/torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512d9661f6243b
278
416
  http://localhost:8123/torrentgalaxy.db|1976025090|582aa8bfb0ad8e6f512d9661f6243bdd
279
417
 
280
418
  ```python
281
- from fastapi import FastAPI, Request
282
- from fastapi.responses import JSONResponse, RedirectResponse
419
+ from blacksheep import json, redirect, Application, Request
283
420
  from p123client import P123Client
284
421
  from p123client.tool import get_downurl
285
422
 
286
423
  # TODO: 改成你自己的账户和密码
287
- client = P123Client(passport="手机号或邮箱", password="登录密码")
424
+ client = P123Client(passport="15888364500", password="Gchenyang123")
425
+ ## 或者
426
+ # from pathlib import Path
427
+ # client = P123Client(Path("~/123-token.txt").expanduser())
288
428
 
289
- app = FastAPI(debug=True)
429
+ app = Application(show_error_details=__debug__)
290
430
 
291
- @app.get("/{uri:path}")
292
- @app.head("/{uri:path}")
431
+ @app.router.route("/{path:uri}", methods=["GET", "HEAD"])
293
432
  async def index(request: Request, uri: str):
294
433
  try:
295
434
  payload = int(uri)
296
435
  except ValueError:
297
436
  if uri.count("|") < 2:
298
- return JSONResponse({"state": False, "message": f"bad uri: {uri!r}"}, 500)
437
+ return json({"state": False, "message": f"bad uri: {uri!r}"}, 500)
299
438
  payload = uri
300
439
  if s3_key_flag := request.url.query:
301
- payload += "?" + s3_key_flag
440
+ payload += "?" + s3_key_flag.decode("ascii")
302
441
  url = await get_downurl(client, payload, quoted=False, async_=True)
303
- return RedirectResponse(url, 302)
442
+ return redirect(url)
304
443
 
305
444
  if __name__ == "__main__":
306
445
  from uvicorn import run
@@ -371,7 +510,7 @@ def export_share_files_json(
371
510
  print("> 已拉取:", link)
372
511
  print("> 已保存:", path)
373
512
 
374
- export_share_files("https://www.123684.com/s/oec7Vv-CIYWh?提取码:ZY4K")
513
+ export_share_files_json("https://www.123684.com/s/oec7Vv-CIYWh?提取码:ZY4K")
375
514
  ```
376
515
 
377
516
  ### 6. 最新的操作记录
@@ -404,4 +543,9 @@ client.fs_list_new({
404
543
  client.fs_sync_log()
405
544
  ```
406
545
 
546
+ ## 其它资源
547
+
548
+ - 如果你需要更详细的文档,特别是关于各种接口的信息,可以阅读
549
+
550
+ [https://p123client.readthedocs.io/en/latest/](https://p123client.readthedocs.io/en/latest/)
407
551