download¶
下载
这个模块提供了一些和下载有关的函数
- p115client.tool.download.batch_get_url(client: str | PathLike | P115Client | P115OpenClient, pickcode: int | str | Iterable[int | str], user_agent: str = '', app: str = 'android', *, async_: Literal[False] = False, **request_kwargs) dict[int, P115URL][source]¶
- p115client.tool.download.batch_get_url(client: str | PathLike | P115Client | P115OpenClient, pickcode: int | str | Iterable[int | str], user_agent: str = '', app: str = 'android', *, async_: Literal[True], **request_kwargs) Coroutine[Any, Any, dict[int, P115URL]]
批量获取下载链接
- Parameters:
client – 115 客户端或 cookies
pickcode – pickcode 或 id
user_agent – “user-agent” 请求头的值
app – 使用指定 app(设备)的接口
async – 是否异步
request_kwargs – 其它请求参数
- Returns:
字典,key 是文件 id,value 是下载链接,自动忽略所有无效项目
- p115client.tool.download.download_file(client: str | PathLike | P115Client | P115OpenClient, fid: int | str | Mapping, path: str = '', resume: bool = True, reporthook: None | Callable[[int], Any] = None, *, async_: Literal[False, True] = False, **request_kwargs) TaskResultTuple[source]¶
从 115 网盘下载一个文件到本地
- Parameters:
client – 115 客户端或 cookies
fid – 待下载文件的 id、pickcode 或者信息字典
path – 下载到本地路径,如果不提供或者以 “/” 结尾,则用网盘上的名字
resume – 是否断点续传
reporthook – 用于更新进度条
async – 是否异步
request_kwargs – 其它请求参数
- Returns:
下载结果信息
- p115client.tool.download.get_pic_url(client: str | PathLike | P115Client, sha1: str, *, async_: Literal[False] = False, **request_kwargs) str[source]¶
- p115client.tool.download.get_pic_url(client: str | PathLike | P115Client, sha1: Iterable[str], *, async_: Literal[False] = False, **request_kwargs) list[str]
- p115client.tool.download.get_pic_url(client: str | PathLike | P115Client, sha1: str, *, async_: Literal[True], **request_kwargs) Coroutine[Any, Any, str]
- p115client.tool.download.get_pic_url(client: str | PathLike | P115Client, sha1: Iterable[str], *, async_: Literal[True], **request_kwargs) Coroutine[Any, Any, list[str]]
单个或批量获取图片链接
Note
不仅限于图片,每个文件必须限制在 50 MB 以内(含)
- Parameters:
client – 115 客户端或 cookies
sha1 – 图片的 sha1 或 f”{bucket}_{object}”(bucket 是所在存储桶, `object`是对象 id)
async – 是否异步
request_kwargs – 其它请求参数
- Returns:
图片链接的单个或列表
- p115client.tool.download.get_remaining_open_count(client: str | PathLike | P115Client | P115OpenClient, app: str = 'android', *, async_: Literal[False] = False, **request_kwargs) int[source]¶
- p115client.tool.download.get_remaining_open_count(client: str | PathLike | P115Client | P115OpenClient, app: str = 'android', *, async_: Literal[True], **request_kwargs) Coroutine[Any, Any, int]
获取剩余的可打开下载链接数
Note
假设总数是 n,通常总数是 10,偶尔会调整,如果已经有 m 个被打开的链接,则返回的数字是 n-m
- Parameters:
client – 115 客户端或 cookies
app – 使用指定 app(设备)的接口
async – 是否异步
request_kwargs – 其它请求参数
- Returns:
个数
- p115client.tool.download.iter_download_files(client: str | PathLike | P115Client, cid: int | str | Mapping = 0, ensure_name: bool = False, escape: None | bool | Callable[[str], str] = True, with_ancestors: bool = False, id_to_dirnode: None | EllipsisType | MutableMapping[int, tuple[str, int]] = None, path_already: bool = False, max_workers: None | int = 0, max_files: int | None = 0, max_dirs: int | None = 0, app: str = 'android', *, async_: Literal[False] = False, **request_kwargs) Iterator[dict][source]¶
- p115client.tool.download.iter_download_files(client: str | PathLike | P115Client, cid: int | str | Mapping = 0, ensure_name: bool = False, escape: None | bool | Callable[[str], str] = True, with_ancestors: bool = False, id_to_dirnode: None | EllipsisType | MutableMapping[int, tuple[str, int]] = None, path_already: bool = False, max_workers: None | int = 0, max_files: int | None = 0, max_dirs: int | None = 0, app: str = 'android', *, async_: Literal[True], **request_kwargs) AsyncIterator[dict]
获取一个目录内所有的文件信息,不包括目录
- Parameters:
client – 115 客户端或 cookies
cid – 目录 id 或 pickcode
ensure_name – 确保返回数据中有 “name” 字段
escape –
对文件名进行转义
如果为 None,则不处理;否则,这个函数用来对文件名中某些符号进行转义,例如 “/” 等
如果为 True,则使用 posixpatht.escape,会对文件名中 “/”,或单独出现的 “.” 和 “..” 用 “" 进行转义
如果为 False,则使用 posix_escape_name 函数对名字进行转义,会把文件名中的 “/” 转换为 “|”
如果为 Callable,则用你所提供的调用,以或者转义后的名字
with_ancestors – 文件信息中是否要包含 “ancestors”
id_to_dirnode – 字典,保存 id 到对应文件的
(name, parent_id)元组的字典path_already – 如果为 True,则说明 id_to_dirnode 中已经具备构建路径所需要的目录节点,所以不会再去拉取目录节点的信息
max_workers – 最大并发数,如果为 None 或 < 0 则自动确定,如果为 0 则单工作者惰性执行
max_files – 估计最大存在的文件数,<= 0 时则无限
max_dirs – 估计最大存在的目录数,<= 0 时则无限
app – 使用指定 app(设备)的接口
async – 是否异步
request_kwargs – 其它请求参数
- Returns:
迭代器,产生文件的简略信息
- p115client.tool.download.iter_download_nodes(client: str | PathLike | P115Client, pickcode: str | int | Mapping = '', files: bool = True, ensure_name: bool = False, id_to_dirnode: None | EllipsisType | MutableMapping[int, tuple[str, int]] = Ellipsis, max_workers: None | int = 0, max_page: int | None = 0, app: str = 'android', *, async_: Literal[False] = False, **request_kwargs) Iterator[dict][source]¶
- p115client.tool.download.iter_download_nodes(client: str | PathLike | P115Client, pickcode: str | int | Mapping = '', files: bool = True, ensure_name: bool = False, id_to_dirnode: None | EllipsisType | MutableMapping[int, tuple[str, int]] = Ellipsis, max_workers: None | int = 0, max_page: int | None = 0, app: str = 'android', *, async_: Literal[True], **request_kwargs) AsyncIterator[dict]
获取一个目录内所有的文件或者目录的信息(简略)
Caution
不要在不确定的情况下,给
max_page设置一个特别大的值,这会大致大量无用的请求,导致服务器繁忙,响应效率低下对于
x个文件或目录,相应的d, r = divmod(x, 5000); max_page += (r > 0),或者max_page = -(-x // 5000)- Parameters:
client – 115 客户端或 cookies
pickcode – 目录的 pickcode 或 id
files – 如果为 True,则只获取文件,否则只获取目录
ensure_name – 确保返回数据中有 “name” 字段
id_to_dirnode – 字典,保存 id 到对应文件的
(name, parent_id)元组的字典max_workers – 最大并发数,如果为 None 或 < 0 则自动确定,如果为 0 则单工作者惰性执行
max_page –
要拉取的最大页码(页码从 1 开始计数)
如果为 None,则页码从小到大拉取,并会尝试获取总文件数,当获取到后且还在运行中,则从后往前拉取
如果 > 0,则页码从大到小拉取
如果 <= 0,则页码从小到大拉取
app – 使用指定 app(设备)的接口
async – 是否异步
request_kwargs – 其它请求参数
- Returns:
迭代器,产生文件或者目录的简略信息
- p115client.tool.download.iter_files_with_url(client: str | PathLike | P115Client | P115OpenClient, cid: int | str | Mapping = 0, suffixes: None | str | Iterable[str] = None, type: Literal[1, 2, 3, 4, 5, 6, 7, 99] = 99, cur: Literal[0, 1] = 0, with_ancestors: bool = False, with_path: bool = False, use_star: None | bool = False, escape: None | bool | Callable[[str], str] = True, normalize_attr: Callable[[dict], dict] = normalize_attr, id_to_dirnode: None | EllipsisType | MutableMapping[int, tuple[str, int]] = None, app: str = 'web', raise_for_changed_count: bool = False, user_agent: str = '', *, async_: Literal[False] = False, **request_kwargs) Iterator[dict][source]¶
- p115client.tool.download.iter_files_with_url(client: str | PathLike | P115Client | P115OpenClient, cid: int | str | Mapping = 0, suffixes: None | str | Iterable[str] = None, type: Literal[1, 2, 3, 4, 5, 6, 7, 99] = 99, cur: Literal[0, 1] = 0, with_ancestors: bool = False, with_path: bool = False, use_star: None | bool = False, escape: None | bool | Callable[[str], str] = True, normalize_attr: Callable[[dict], dict] = normalize_attr, id_to_dirnode: None | EllipsisType | MutableMapping[int, tuple[str, int]] = None, app: str = 'web', raise_for_changed_count: bool = False, user_agent: str = '', *, async_: Literal[True], **request_kwargs) AsyncIterator[dict]
获取文件信息和下载链接
- Parameters:
client – 115 客户端或 cookies
cid – 目录 id 或 pickcode
suffixes – 扩展名,可以有多个,最前面的 “.” 可以省略
type –
文件类型
1: 文档
2: 图片
3: 音频
4: 视频
5: 压缩包
6: 应用
7: 书籍
99: 仅文件
cur – 仅当前目录。0: 否(将遍历子目录树上所有叶子节点),1: 是
with_ancestors – 文件信息中是否要包含 “ancestors”
with_path – 文件信息中是否要包含 “path”
use_star – 获取目录信息时,是否允许使用星标 (如果为 None,则采用流处理,否则采用批处理)
escape –
对文件名进行转义
如果为 None,则不处理;否则,这个函数用来对文件名中某些符号进行转义,例如 “/” 等
如果为 True,则使用 posixpatht.escape,会对文件名中 “/”,或单独出现的 “.” 和 “..” 用 “" 进行转义
如果为 False,则使用 posix_escape_name 函数对名字进行转义,会把文件名中的 “/” 转换为 “|”
如果为 Callable,则用你所提供的调用,以或者转义后的名字
normalize_attr – 把数据进行转换处理,使之便于阅读
id_to_dirnode – 字典,保存 id 到对应文件的
(name, parent_id)元组的字典app – 使用指定 app(设备)的接口
raise_for_changed_count – 分批拉取时,发现总数发生变化后,是否报错
user_agent – “user-agent” 请求头的值
async – 是否异步
request_kwargs – 其它请求参数
- Returns:
迭代器,产生文件信息,并增加一个 “url” 作为下载链接
- p115client.tool.download.iter_images_with_url(client: str | PathLike | P115Client | P115OpenClient, cid: int | str | Mapping = 0, suffixes: None | str | Iterable[str] = None, cur: Literal[0, 1] = 0, with_ancestors: bool = False, with_path: bool = False, use_star: None | bool = False, escape: None | bool | Callable[[str], str] = True, normalize_attr: Callable[[dict], dict] = normalize_attr, id_to_dirnode: None | EllipsisType | MutableMapping[int, tuple[str, int]] = None, app: str = 'web', raise_for_changed_count: bool = False, *, async_: Literal[False] = False, **request_kwargs) Iterator[dict][source]¶
- p115client.tool.download.iter_images_with_url(client: str | PathLike | P115Client | P115OpenClient, cid: int | str | Mapping = 0, suffixes: None | str | Iterable[str] = None, cur: Literal[0, 1] = 0, with_ancestors: bool = False, with_path: bool = False, use_star: None | bool = False, escape: None | bool | Callable[[str], str] = True, normalize_attr: Callable[[dict], dict] = normalize_attr, id_to_dirnode: None | EllipsisType | MutableMapping[int, tuple[str, int]] = None, app: str = 'web', raise_for_changed_count: bool = False, *, async_: Literal[True], **request_kwargs) AsyncIterator[dict]
获取图片文件信息和下载链接
Attention
请不要把不能被 115 识别为图片的文件扩展名放在 suffixes 参数中传入,这只是浪费时间,最后也只能获得普通的下载链接
- Parameters:
client – 115 客户端或 cookies
cid – 目录 id 或 pickcode
suffixes – 扩展名,可以有多个,最前面的 “.” 可以省略(请确保扩展名确实能被 115 认为是图片,否则会因为不能批量获取到链接而浪费一些时间再去单独生成下载链接);如果不传(默认),则会获取所有图片
cur – 仅当前目录。0: 否(将遍历子目录树上所有叶子节点),1: 是
with_ancestors – 文件信息中是否要包含 “ancestors”
with_path – 文件信息中是否要包含 “path”
use_star – 获取目录信息时,是否允许使用星标 (如果为 None,则采用流处理,否则采用批处理)
escape –
对文件名进行转义
如果为 None,则不处理;否则,这个函数用来对文件名中某些符号进行转义,例如 “/” 等
如果为 True,则使用 posixpatht.escape,会对文件名中 “/”,或单独出现的 “.” 和 “..” 用 “" 进行转义
如果为 False,则使用 posix_escape_name 函数对名字进行转义,会把文件名中的 “/” 转换为 “|”
如果为 Callable,则用你所提供的调用,以或者转义后的名字
normalize_attr – 把数据进行转换处理,使之便于阅读
id_to_dirnode – 字典,保存 id 到对应文件的
(name, parent_id)元组的字典app – 使用指定 app(设备)的接口
raise_for_changed_count – 分批拉取时,发现总数发生变化后,是否报错
async – 是否异步
request_kwargs – 其它请求参数
- Returns:
迭代器,产生文件信息,并增加一个 “url” 作为下载链接
- p115client.tool.download.iter_subtitle_batches(client: str | PathLike | P115Client | P115OpenClient, file_ids: Iterable[int | str], batch_size: int = 1000, app: str = 'web', *, async_: Literal[False] = False, **request_kwargs) Iterator[dict][source]¶
- p115client.tool.download.iter_subtitle_batches(client: str | PathLike | P115Client | P115OpenClient, file_ids: Iterable[int | str], batch_size: int = 1000, app: str = 'web', *, async_: Literal[True], **request_kwargs) AsyncIterator[dict]
批量获取字幕文件的信息和下载链接
Caution
这个函数运行时,会把相关文件以 1,000 为一批,同一批次复制到同一个新建的目录,在批量获取链接后,自动把目录删除到回收站。
Attention
目前看来 115 只支持:”.srt”、”.ass”、”.ssa”,如果不能被 115 识别为字幕,将会被自动略过
- Parameters:
client – 115 客户端或 cookies
file_ids – 一组文件的 id 或 pickcode
batch_size – 每一个批次处理的个量
async – 是否异步
request_kwargs – 其它请求参数
- Returns:
迭代器,产生文件信息,并增加一个 “url” 作为下载链接,文件信息中的 file_id 是复制所得的文件信息,不是原来文件的 id
- p115client.tool.download.iter_subtitles_with_url(client: str | PathLike | P115Client | P115OpenClient, cid: int | str | Mapping = 0, suffixes: str | Iterable[str] = ('.srt', '.ass', '.ssa'), cur: Literal[0, 1] = 0, with_ancestors: bool = False, with_path: bool = False, use_star: None | bool = False, escape: None | bool | Callable[[str], str] = True, normalize_attr: Callable[[dict], dict] = normalize_attr, id_to_dirnode: None | EllipsisType | MutableMapping[int, tuple[str, int]] = None, app: str = 'web', raise_for_changed_count: bool = False, *, async_: Literal[False] = False, **request_kwargs) Iterator[dict][source]¶
- p115client.tool.download.iter_subtitles_with_url(client: str | PathLike | P115Client | P115OpenClient, cid: int | str | Mapping = 0, suffixes: str | Iterable[str] = ('.srt', '.ass', '.ssa'), cur: Literal[0, 1] = 0, with_ancestors: bool = False, with_path: bool = False, use_star: None | bool = False, escape: None | bool | Callable[[str], str] = True, normalize_attr: Callable[[dict], dict] = normalize_attr, id_to_dirnode: None | EllipsisType | MutableMapping[int, tuple[str, int]] = None, app: str = 'web', raise_for_changed_count: bool = False, *, async_: Literal[True], **request_kwargs) AsyncIterator[dict]
获取字幕文件信息和下载链接
Caution
这个函数运行时,会把相关文件以 1,000 为一批,同一批次复制到同一个新建的目录,在批量获取链接后,自动把目录删除到回收站。
Attention
目前看来 115 只支持:”.srt”, “.ass”, “.ssa”
请不要把不能被 115 识别为字幕的文件扩展名放在 suffixes 参数中传入,这只是浪费时间,最后也只能获得普通的下载链接
- Parameters:
client – 115 客户端或 cookies
cid – 目录 id 或 pickcode
suffixes – 扩展名,可以有多个,最前面的 “.” 可以省略(请确保扩展名确实能被 115 认为是字幕,否则会因为不能批量获取到链接而浪费一些时间再去单独生成下载链接)
cur – 仅当前目录。0: 否(将遍历子目录树上所有叶子节点),1: 是
with_ancestors – 文件信息中是否要包含 “ancestors”
with_path – 文件信息中是否要包含 “path”
use_star – 获取目录信息时,是否允许使用星标 (如果为 None,则采用流处理,否则采用批处理)
escape –
对文件名进行转义
如果为 None,则不处理;否则,这个函数用来对文件名中某些符号进行转义,例如 “/” 等
如果为 True,则使用 posixpatht.escape,会对文件名中 “/”,或单独出现的 “.” 和 “..” 用 “" 进行转义
如果为 False,则使用 posix_escape_name 函数对名字进行转义,会把文件名中的 “/” 转换为 “|”
如果为 Callable,则用你所提供的调用,以或者转义后的名字
normalize_attr – 把数据进行转换处理,使之便于阅读
id_to_dirnode – 字典,保存 id 到对应文件的
(name, parent_id)元组的字典app – 使用指定 app(设备)的接口
raise_for_changed_count – 分批拉取时,发现总数发生变化后,是否报错
async – 是否异步
request_kwargs – 其它请求参数
- Returns:
迭代器,产生文件信息,并增加一个 “url” 作为下载链接
- p115client.tool.download.iter_url_batches(client: str | PathLike | P115Client | P115OpenClient, pickcodes: Iterator[int | str], user_agent: str = '', batch_size: int = 10, app: str = 'android', *, async_: Literal[False] = False, **request_kwargs) Iterator[P115URL][source]¶
- p115client.tool.download.iter_url_batches(client: str | PathLike | P115Client | P115OpenClient, pickcodes: Iterator[int | str], user_agent: str = '', batch_size: int = 10, app: str = 'android', *, async_: Literal[True], **request_kwargs) AsyncIterator[P115URL]
批量获取下载链接
Attention
请确保所有的 pickcode 都是有效的,要么是现在存在的,要么是以前存在过被删除的。
如果有目录的 pickcode 混在其中,则会自动排除。
- Parameters:
client – 115 客户端或 cookies
pickcodes – 一个迭代器,产生 pickcode 或 id
user_agent – “user-agent” 请求头的值
batch_size – 每一个批次处理的个量
app – 使用指定 app(设备)的接口
async – 是否异步
request_kwargs – 其它请求参数
- Returns:
字典,key 是文件 id,value 是下载链接,自动忽略所有无效项目
- p115client.tool.download.make_db(client: str | PathLike | P115Client, dbfile: str | PathLike | Connection | Cursor = 'p115updatedb.db', cid: int | str | Mapping = 0, recursive: bool = True, max_workers: None | int = None, app: str = 'android', *, async_: Literal[False] = False, **request_kwargs) dict[source]¶
- p115client.tool.download.make_db(client: str | PathLike | P115Client, dbfile: str | PathLike | Connection | Cursor = 'p115updatedb.db', cid: int | str | Mapping = 0, recursive: bool = True, max_workers: None | int = None, app: str = 'android', *, async_: Literal[True], **request_kwargs) Coroutine[Any, Any, dict]
对某个目录执行一次拉取,以更新 SQLite 数据
- Parameters:
client – 115 客户端或 cookies
dbfile – 数据库路径或连接
cid – 目录 id 或 pickcode
recursive – 如果为 True,则拉取所有以之为祖先(先驱)节点的节点信息;否则,拉取所有以之为父(前驱)节点的节点信息
max_workers – 最大并发数,如果为 None 或 < 0 则自动确定,如果为 0 则单工作者惰性执行
app – 使用指定 app(设备)的接口
async – 是否异步
request_kwargs – 其它请求参数
- Returns:
一些统计信息
- p115client.tool.download.make_strm(client: str | PathLike | P115Client, cid: int | str | Mapping = 0, save_dir: bytes | str | PathLike = '.', base_url: str = '', with_root: None | bool = None, without_suffix: bool = True, clean: bool = True, replace: bool = True, predicate: None | Literal[1, 2, 3, 4, 5, 6, 7] | str | tuple[str, ...] | Callable[[dict], bool] = 4, id_to_dirnode: None | EllipsisType | MutableMapping[int, tuple[str, int]] = None, path_already: bool = False, app: str = 'android', max_workers: None | int = None, *, async_: Literal[False] = False, **request_kwargs) dict[source]¶
- p115client.tool.download.make_strm(client: str | PathLike | P115Client, cid: int | str | Mapping = 0, save_dir: bytes | str | PathLike = '.', base_url: str = '', with_root: None | bool = None, without_suffix: bool = True, clean: bool = True, replace: bool = True, predicate: None | Literal[1, 2, 3, 4, 5, 6, 7] | str | tuple[str, ...] | Callable[[dict], bool] = 4, id_to_dirnode: None | EllipsisType | MutableMapping[int, tuple[str, int]] = None, path_already: bool = False, app: str = 'android', max_workers: None | int = None, *, async_: Literal[True], **request_kwargs) Coroutine[Any, Any, dict]
拉取目录树,保存到 .strm 文件
- Parameters:
client – 115 客户端或 cookies
cid – 目录 id 或 pickcode
save_dir – 本地的保存目录,默认是当前工作目录
base_url – STRM 链接(或者说 302 服务)的基地址
with_root –
是否保留根
如果为 True,则在
save_dir保留从根目录 / 开始的目录结构如果为 False,则在
save_dir保留从拉取目录开始的目录结构如果为 None,则在
save_dir下创建一个和cid目录名字相同的目录作为save_dir,然后保留从拉取目录开始的目录结构
without_suffix –
是否去除原来的扩展名
如果为 True,则去掉原来的扩展名后再拼接
如果为 False,则直接用 “.strm” 拼接到原来的路径后面
clean – 是否清理
save_dir,如果为 True,则删除所有不包含本次更新所涉及到的 .strm 文件和相应目录replace – 遇到路径下有 .strm 文件时是否替换
predicate –
断言,断言为真的文件才会生成 .strm 文件
如果为 None,则不进行筛选
如果为整数,则筛选某一类型的文件
1: 文档
2: 图片
3: 音频
4: 视频
5: 压缩包
6: 应用
7: 书籍
如果是 str 或元组,则是后缀或一组后缀,筛选这些后缀的文件
如果是 Callable,则逐个对获取到的文件信息调用它,返回值为 True 才保留
id_to_dirnode – 字典,保存 id 到对应文件的
(name, parent_id)元组的字典path_already – 如果为 True,则说明
id_to_dirnode中已经具备构建路径所需要的目录节点,所以不会再去拉取目录节点的信息app – 使用指定 app(设备)的接口
max_workers – 最大并发数,用户拉取目录树,但写入本地文件仍然是单线程的(经过测试如此效率更高)
async – 是否异步
request_kwargs – 其它请求参数
- Returns:
一些统计信息