p115client 0.0.5.12__py3-none-any.whl → 0.0.5.12.2__py3-none-any.whl

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.
p115client/const.py CHANGED
@@ -5,8 +5,8 @@ from __future__ import annotations
5
5
 
6
6
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
7
7
  __all__ = [
8
- "AVAILABLE_APPS", "APP_TO_SSOENT", "SSOENT_TO_APP", "CLIENT_API_MAP",
9
- "CLASS_TO_TYPE", "SUFFIX_TO_TYPE", "errno",
8
+ "AVAILABLE_APPS", "APP_TO_SSOENT", "SSOENT_TO_APP", "CLIENT_METHOD_API_MAP",
9
+ "CLIENT_API_METHODS_MAP", "CLASS_TO_TYPE", "SUFFIX_TO_TYPE", "errno",
10
10
  ]
11
11
 
12
12
  from enum import IntEnum
@@ -69,8 +69,11 @@ SSOENT_TO_APP: Final[dict[str, str]] = {
69
69
  "S1": "harmony",
70
70
  }
71
71
 
72
- #: 所有已封装的 115 接口以及对应的方法名
73
- CLIENT_API_MAP: Final[dict[str, str]] = {}
72
+ #: 所有已封装的方法名和对应的 115 接口
73
+ CLIENT_METHOD_API_MAP: Final[dict[str, str]] = {}
74
+
75
+ #: 所有已封装的 115 接口和对应的方法名
76
+ CLIENT_API_METHODS_MAP: Final[dict[str, list[str]]] = {}
74
77
 
75
78
  #: 文件的 class 属性对应的所属类型的整数代码
76
79
  CLASS_TO_TYPE: Final[dict[str, int]] = {
p115client/tool/attr.py CHANGED
@@ -72,7 +72,7 @@ def get_attr(
72
72
  resp = yield client.fs_file(id, async_=async_, **request_kwargs)
73
73
  check_response(resp)
74
74
  return normalize_attr_web(resp["data"][0], dict_cls=AttrDict)
75
- return run_gen_step(gen_step, may_call=False, async_=async_)
75
+ return run_gen_step(gen_step, async_)
76
76
 
77
77
 
78
78
  def type_of_attr(attr: Mapping, /) -> int:
p115client/tool/auth.py CHANGED
@@ -58,5 +58,5 @@ def deauth_open(
58
58
  async_=async_,
59
59
  **request_kwargs,
60
60
  )
61
- return run_gen_step(gen_step, async_=async_)
61
+ return run_gen_step(gen_step, async_)
62
62
 
@@ -145,7 +145,7 @@ def batch_get_url(
145
145
  for id, info in resp["data"].items()
146
146
  if info["url"]
147
147
  }
148
- return run_gen_step(gen_step, may_call=False, async_=async_)
148
+ return run_gen_step(gen_step, async_)
149
149
 
150
150
 
151
151
  @overload
@@ -228,7 +228,7 @@ def iter_url_batches(
228
228
  is_directory=False,
229
229
  headers=headers,
230
230
  ))
231
- return run_gen_step_iter(gen_step, may_call=False, async_=async_)
231
+ return run_gen_step_iter(gen_step, async_)
232
232
 
233
233
 
234
234
  @overload
@@ -403,7 +403,7 @@ def iter_files_with_url(
403
403
  **request_kwargs,
404
404
  )
405
405
  yield Yield(attr)
406
- return run_gen_step_iter(gen_step, may_call=False, async_=async_)
406
+ return run_gen_step_iter(gen_step, async_)
407
407
 
408
408
 
409
409
  @overload
@@ -561,7 +561,7 @@ def iter_images_with_url(
561
561
  **request_kwargs,
562
562
  )
563
563
  yield Yield(attr)
564
- return run_gen_step_iter(gen_step, may_call=False, async_=async_)
564
+ return run_gen_step_iter(gen_step, async_)
565
565
 
566
566
 
567
567
  @overload
@@ -746,7 +746,7 @@ def iter_subtitles_with_url(
746
746
  **request_kwargs,
747
747
  )
748
748
  yield Yield(attr)
749
- return run_gen_step_iter(gen_step, may_call=False, async_=async_)
749
+ return run_gen_step_iter(gen_step, async_)
750
750
 
751
751
 
752
752
  @overload
@@ -836,7 +836,7 @@ def iter_subtitle_batches(
836
836
  pass
837
837
  finally:
838
838
  yield client.fs_delete(scid, async_=async_, **request_kwargs)
839
- return run_gen_step_iter(gen_step, may_call=False, async_=async_)
839
+ return run_gen_step_iter(gen_step, async_)
840
840
 
841
841
 
842
842
  @overload
@@ -1077,7 +1077,7 @@ def make_strm(
1077
1077
  if use_abspath is not None:
1078
1078
  params["path_already"] = path_already
1079
1079
  yield (async_batch if async_ else thread_batch)(
1080
- lambda attr: run_gen_step(save(attr), may_call=False, async_=async_),
1080
+ lambda attr: run_gen_step(save(attr), async_),
1081
1081
  (iter_files if use_abspath is None else iter_files_with_path)(
1082
1082
  client,
1083
1083
  cid,
@@ -1114,7 +1114,7 @@ def make_strm(
1114
1114
  "ignore": ignored,
1115
1115
  "remove": removed,
1116
1116
  }
1117
- return run_gen_step(gen_step, may_call=False, async_=async_)
1117
+ return run_gen_step(gen_step, async_)
1118
1118
 
1119
1119
 
1120
1120
  @overload
@@ -1265,7 +1265,7 @@ def iter_download_nodes(
1265
1265
  finally:
1266
1266
  yield shutdown()
1267
1267
  if pickcode:
1268
- return run_gen_step_iter(gen_step(pickcode), may_call=False, async_=async_)
1268
+ return run_gen_step_iter(gen_step(pickcode), async_)
1269
1269
  else:
1270
1270
  def chain():
1271
1271
  with with_iter_next(iterdir(
@@ -1283,12 +1283,8 @@ def iter_download_nodes(
1283
1283
  yield Yield(
1284
1284
  {"fid": str(attr["id"]), "pid": "0", "fn": attr["name"]}
1285
1285
  )
1286
- yield YieldFrom(run_gen_step_iter(
1287
- gen_step(attr["pickcode"]),
1288
- may_call=False,
1289
- async_=async_,
1290
- ))
1291
- return run_gen_step_iter(chain, may_call=False, async_=async_)
1286
+ yield YieldFrom(run_gen_step_iter(gen_step(attr["pickcode"]), async_))
1287
+ return run_gen_step_iter(chain, async_)
1292
1288
 
1293
1289
 
1294
1290
  @overload
@@ -1435,11 +1431,7 @@ def iter_download_files(
1435
1431
  **defaults,
1436
1432
  })
1437
1433
  for pickcode in pickcodes:
1438
- yield YieldFrom(run_gen_step_iter(
1439
- gen_step(pickcode),
1440
- may_call=False,
1441
- async_=async_,
1442
- ))
1434
+ yield YieldFrom(run_gen_step_iter(gen_step(pickcode), async_))
1443
1435
  return
1444
1436
  if not pickcode:
1445
1437
  resp = yield client.fs_file_skim(cid, async_=async_, **request_kwargs)
@@ -1476,7 +1468,7 @@ def iter_download_files(
1476
1468
  finally:
1477
1469
  ancestors_loaded = True
1478
1470
  if async_:
1479
- task: Any = create_task(run_gen_step(load_ancestors, may_call=False, async_=True))
1471
+ task: Any = create_task(run_gen_step(load_ancestors, True))
1480
1472
  else:
1481
1473
  task = run_as_thread(run_gen_step, load_ancestors)
1482
1474
  cache: list[dict] = []
@@ -1510,7 +1502,7 @@ def iter_download_files(
1510
1502
  else:
1511
1503
  task.result()
1512
1504
  yield YieldFrom(map(norm_attr, cache))
1513
- return run_gen_step_iter(gen_step, may_call=False, async_=async_)
1505
+ return run_gen_step_iter(gen_step, async_)
1514
1506
 
1515
1507
 
1516
1508
  @overload
@@ -1586,5 +1578,5 @@ def get_remaining_open_count(
1586
1578
  for f in cache:
1587
1579
  f.close()
1588
1580
  return len(cache)
1589
- return run_gen_step(gen_step, may_call=False, async_=async_)
1581
+ return run_gen_step(gen_step, async_)
1590
1582
 
p115client/tool/edit.py CHANGED
@@ -91,7 +91,7 @@ def update_abstract(
91
91
  chunked(ids, batch_size),
92
92
  max_workers=max_workers
93
93
  ))
94
- return run_gen_step(gen_step, may_call=False, async_=async_)
94
+ return run_gen_step(gen_step, async_)
95
95
 
96
96
 
97
97
  @overload
@@ -634,5 +634,5 @@ def batch_unstar(
634
634
  async_=async_, # type: ignore
635
635
  **request_kwargs,
636
636
  )
637
- return run_gen_step(gen_step, may_call=False, async_=async_)
637
+ return run_gen_step(gen_step, async_)
638
638
 
@@ -120,7 +120,7 @@ def parse_export_dir_as_dict_iter(
120
120
  yield ensure_async(close, threaded=True)
121
121
  elif callable(close := getattr(file, "close", None)):
122
122
  close()
123
- return run_gen_step_iter(gen_step, may_call=False, async_=async_)
123
+ return run_gen_step_iter(gen_step, async_)
124
124
 
125
125
 
126
126
  @overload
@@ -230,7 +230,7 @@ def parse_export_dir_as_path_iter(
230
230
  yield ensure_async(close, threaded=True)
231
231
  elif callable(close := getattr(file, "close", None)):
232
232
  close()
233
- return run_gen_step_iter(gen_step, may_call=False, async_=async_)
233
+ return run_gen_step_iter(gen_step, async_)
234
234
 
235
235
 
236
236
  @overload
@@ -317,7 +317,7 @@ def parse_export_dir_as_patht_iter(
317
317
  yield ensure_async(close, threaded=True)
318
318
  elif callable(close := getattr(file, "close", None)):
319
319
  close()
320
- return run_gen_step_iter(gen_step, may_call=False, async_=async_)
320
+ return run_gen_step_iter(gen_step, async_)
321
321
 
322
322
 
323
323
  @overload
@@ -406,7 +406,7 @@ def export_dir(
406
406
  payload["layer_limit"] = layer_limit
407
407
  resp = yield client.fs_export_dir(payload, async_=async_, **request_kwargs)
408
408
  return check_response(resp)["data"]["export_id"]
409
- return run_gen_step(gen_step, may_call=False, async_=async_)
409
+ return run_gen_step(gen_step, async_)
410
410
 
411
411
 
412
412
  @overload
@@ -486,7 +486,7 @@ def export_dir_result(
486
486
  raise TimeoutError(export_id)
487
487
  if check_interval:
488
488
  yield do_sleep(min(check_interval, remaining_seconds))
489
- return run_gen_step(gen_step, may_call=False, async_=async_)
489
+ return run_gen_step(gen_step, async_)
490
490
 
491
491
 
492
492
  @overload
@@ -645,5 +645,5 @@ def export_dir_parse_iter(
645
645
  async_=async_, # type: ignore
646
646
  **request_kwargs,
647
647
  )
648
- return run_gen_step_iter(gen_step, may_call=False, async_=async_)
648
+ return run_gen_step_iter(gen_step, async_)
649
649
 
@@ -7,18 +7,19 @@ __doc__ = "这个模块利用 P115Client.fs_files 方法做了一些封装"
7
7
 
8
8
  from asyncio import shield, wait_for, Task, TaskGroup
9
9
  from collections import deque
10
- from collections.abc import AsyncIterator, Callable, Iterator
10
+ from collections.abc import AsyncIterator, Awaitable, Callable, Iterator
11
11
  from concurrent.futures import Future, ThreadPoolExecutor
12
12
  from copy import copy
13
13
  from errno import EBUSY, ENOENT, ENOTDIR
14
14
  from functools import partial
15
15
  from inspect import isawaitable
16
16
  from itertools import cycle
17
+ from sys import exc_info
17
18
  from time import time
18
- from typing import overload, Any, Final, Literal
19
+ from typing import cast, overload, Any, Final, Literal
19
20
  from warnings import warn
20
21
 
21
- from iterutils import run_gen_step, run_gen_step_iter, Yield
22
+ from iterutils import as_gen_step, run_gen_step_iter, Yield
22
23
  from p115client import check_response, P115Client, P115OpenClient
23
24
  from p115client.client import get_status_code
24
25
  from p115client.exception import BusyOSError, DataError, P115Warning
@@ -95,6 +96,16 @@ def iter_fs_files(
95
96
  client = P115Client(client, check_for_relogin=True)
96
97
  if page_size <= 0:
97
98
  page_size = 10_000
99
+ if not isinstance(client, P115Client) or app == "open":
100
+ page_size = min(page_size, 1150)
101
+ fs_files = partial(client.fs_files_open, **request_kwargs)
102
+ elif app in ("", "web", "desktop", "harmony"):
103
+ page_size = min(page_size, 1150)
104
+ request_kwargs.setdefault("base_url", get_webapi_origin)
105
+ fs_files = partial(client.fs_files, **request_kwargs)
106
+ else:
107
+ request_kwargs.setdefault("base_url", get_proapi_origin)
108
+ fs_files = partial(client.fs_files_app, app=app, **request_kwargs)
98
109
  if first_page_size <= 0:
99
110
  first_page_size = page_size
100
111
  if isinstance(payload, (int, str)):
@@ -104,14 +115,7 @@ def iter_fs_files(
104
115
  "limit": first_page_size, "show_dir": 1, **payload,
105
116
  }
106
117
  cid = int(payload["cid"])
107
- if not isinstance(client, P115Client) or app == "open":
108
- fs_files = partial(client.fs_files_open, **request_kwargs)
109
- elif app in ("", "web", "desktop", "harmony"):
110
- request_kwargs.setdefault("base_url", get_webapi_origin)
111
- fs_files = partial(client.fs_files, **request_kwargs)
112
- else:
113
- request_kwargs.setdefault("base_url", get_proapi_origin)
114
- fs_files = partial(client.fs_files_app, app=app, **request_kwargs)
118
+ @as_gen_step
115
119
  def get_files(payload: dict, /):
116
120
  nonlocal count
117
121
  while True:
@@ -145,13 +149,13 @@ def iter_fs_files(
145
149
  return resp
146
150
  def gen_step():
147
151
  while True:
148
- resp = yield run_gen_step(get_files(payload), may_call=False, async_=async_)
152
+ resp = yield get_files(payload)
149
153
  payload["limit"] = page_size
150
154
  yield Yield(resp)
151
155
  payload["offset"] += len(resp["data"])
152
156
  if payload["offset"] >= count:
153
157
  break
154
- return run_gen_step_iter(gen_step, may_call=False, async_=async_)
158
+ return run_gen_step_iter(gen_step, async_)
155
159
 
156
160
 
157
161
  def iter_fs_files_threaded(
@@ -187,14 +191,7 @@ def iter_fs_files_threaded(
187
191
  if isinstance(client, str):
188
192
  client = P115Client(client, check_for_relogin=True)
189
193
  if page_size <= 0:
190
- page_size = 7_000
191
- if isinstance(payload, (int, str)):
192
- payload = {"cid": payload}
193
- payload = {
194
- "asc": 1, "cid": 0, "fc_mix": 1, "o": "user_ptime", "offset": 0,
195
- "limit": page_size, "show_dir": 1, **payload,
196
- }
197
- cid = int(payload["cid"])
194
+ page_size = 10_000
198
195
  if not isinstance(client, P115Client) or app == "open":
199
196
  page_size = min(page_size, 1150)
200
197
  fs_files = partial(client.fs_files_open, **request_kwargs)
@@ -205,6 +202,13 @@ def iter_fs_files_threaded(
205
202
  else:
206
203
  request_kwargs.setdefault("base_url", get_proapi_origin)
207
204
  fs_files = partial(client.fs_files_app, app=app, **request_kwargs)
205
+ if isinstance(payload, (int, str)):
206
+ payload = {"cid": payload}
207
+ payload = {
208
+ "asc": 1, "cid": 0, "fc_mix": 1, "o": "user_ptime", "offset": 0,
209
+ "limit": page_size, "show_dir": 1, **payload,
210
+ }
211
+ cid = int(payload["cid"])
208
212
  def get_files(payload: dict, /):
209
213
  nonlocal count
210
214
  resp = fs_files(payload)
@@ -302,27 +306,41 @@ async def iter_fs_files_asynchronized(
302
306
  if isinstance(client, str):
303
307
  client = P115Client(client, check_for_relogin=True)
304
308
  if page_size <= 0:
305
- page_size = 7_000
306
- if isinstance(payload, (int, str)):
307
- payload = {"cid": payload}
308
- payload = {
309
- "asc": 1, "cid": 0, "fc_mix": 1, "o": "user_ptime", "offset": 0,
310
- "limit": page_size, "show_dir": 1, **payload,
311
- }
312
- cid = int(payload["cid"])
309
+ page_size = 10_000
310
+ fs_files: Callable[..., Awaitable[dict]]
313
311
  if not isinstance(client, P115Client) or app == "open":
314
312
  page_size = min(page_size, 1150)
315
- fs_files = partial(client.fs_files_open, **request_kwargs)
313
+ fs_files = cast(Callable, partial(
314
+ client.fs_files_open,
315
+ async_=True,
316
+ **request_kwargs,
317
+ ))
316
318
  elif app in ("", "web", "desktop", "harmony"):
317
319
  page_size = min(page_size, 1150)
318
320
  request_kwargs.setdefault("base_url", get_webapi_origin)
319
- fs_files = partial(client.fs_files, **request_kwargs)
321
+ fs_files = cast(Callable, partial(
322
+ client.fs_files,
323
+ async_=True,
324
+ **request_kwargs,
325
+ ))
320
326
  else:
321
327
  request_kwargs.setdefault("base_url", get_proapi_origin)
322
- fs_files = partial(client.fs_files_app, app=app, **request_kwargs)
328
+ fs_files = cast(Callable, partial(
329
+ client.fs_files_app,
330
+ app=app,
331
+ async_=True,
332
+ **request_kwargs,
333
+ ))
334
+ if isinstance(payload, (int, str)):
335
+ payload = {"cid": payload}
336
+ payload = {
337
+ "asc": 1, "cid": 0, "fc_mix": 1, "o": "user_ptime", "offset": 0,
338
+ "limit": page_size, "show_dir": 1, **payload,
339
+ }
340
+ cid = int(payload["cid"])
323
341
  async def get_files(payload: dict, /):
324
342
  nonlocal count
325
- resp = await fs_files(payload, async_=True) # type: ignore
343
+ resp = await fs_files(payload)
326
344
  check_response(resp)
327
345
  if cid and int(resp["path"][-1]["cid"]) != cid:
328
346
  if count < 0:
@@ -390,7 +408,5 @@ async def iter_fs_files_asynchronized(
390
408
  finally:
391
409
  for t in tuple(tg._tasks):
392
410
  t.cancel()
393
- await tg.__aexit__(None, None, None)
394
-
411
+ await tg.__aexit__(*exc_info())
395
412
 
396
- # TODO: 以上的数据获取方式某种程度上应该是通用的,只要是涉及到 offset 和 count,因此可以总结出一个更抽象的函数
@@ -119,7 +119,7 @@ def iter_history_list_once(
119
119
  ts_last_call = time()
120
120
  resp = yield history_list(payload, async_=async_)
121
121
  events = check_response(resp)["data"]["list"]
122
- return run_gen_step_iter(gen_step, may_call=False, async_=async_)
122
+ return run_gen_step_iter(gen_step, async_)
123
123
 
124
124
 
125
125
  @overload
@@ -220,5 +220,5 @@ def iter_history_list(
220
220
  from_time = int(event["update_time"])
221
221
  sub_first_loop = False
222
222
  yield Yield(event)
223
- return run_gen_step_iter(gen_step, may_call=False, async_=async_)
223
+ return run_gen_step_iter(gen_step, async_)
224
224