Skip to main content

Python 115 webdisk client.

Project description

Python 115 网盘客户端.

PyPI - Python Version PyPI - Version PyPI - Downloads PyPI - Format PyPI - Status

安装

你可以从 pypi 安装最新版本

pip install -U p115client

入门介绍

1. 导入模块和创建实例

导入模块

from p115client import P115Client

创建客户端对象,需要传入 cookies,如果不传,则需要扫码登录

cookies = "UID=...; CID=...; SEID=..."
client = P115Client(cookies)

如果你的 cookies 保存在 ~/115-cookies.txt

from pathlib import Path

client = P115Client(Path("~/115-cookies.txt").expanduser())

如果想要在接口返回时自动捕获 405 HTTP 响应码,进行自动扫码,并把更新后的 cookies 写回文件,然后重试接口调用

client = P115Client(Path("~/115-cookies.txt").expanduser(), check_for_relogin=True)

所以综上,推荐的初始化代码为

from p115client import P115Client
from pathlib import Path

client = P115Client(Path("~/115-cookies.txt").expanduser(), check_for_relogin=True)

2. 接口调用

所有需要直接或间接执行 HTTP 请求的接口,都有同步和异步的调用方式

# 同步调用
client.method(payload)
client.method(payload, async_=False)

# 异步调用
await client.method(payload, async_=True)

从根本上讲,除了几个 staticmethod,它们都会调用 P115Client.request

url = "https://webapi.115.com/files"
response = client.request(url=url, params={"cid": 0, "show_dir": 1})

当你需要构建自己的扩展模块,以增加一些新的 115 web 接口时,就需要用到此方法了

from collections.abc import Coroutine
from typing import overload, Any, Literal

from p115client import P115Client

class MyCustom115Client(P115Client):

    @overload
    def foo(
        self, 
        payload: dict, 
        /, 
        async_: Literal[False] = False, 
        **request_kwargs, 
    ) -> dict:
        ...
    @overload
    def foo(
        self, 
        payload: dict, 
        /, 
        async_: Literal[True], 
        **request_kwargs, 
    ) -> Coroutine[Any, Any, dict]:
        ...
    def foo(
        self, 
        payload: dict, 
        /, 
        async_: bool = False, 
        **request_kwargs, 
    ) -> dict | Coroutine[Any, Any, dict]:
        api = "https://webapi.115.com/foo"
        return self.request(
            api, 
            method="GET", 
            params=payload, 
            async_=async_, 
            **request_kwargs, 
        )

    @overload
    def bar(
        self, 
        payload: dict, 
        /, 
        async_: Literal[False] = False, 
        **request_kwargs, 
    ) -> dict:
        ...
    @overload
    def bar(
        self, 
        payload: dict, 
        /, 
        async_: Literal[True], 
        **request_kwargs, 
    ) -> Coroutine[Any, Any, dict]:
        ...
    def bar(
        self, 
        payload: dict, 
        /, 
        async_: bool = False, 
        **request_kwargs, 
    ) -> dict | Coroutine[Any, Any, dict]:
        api = "https://webapi.115.com/bar"
        return self.request(
            api, 
            method="POST", 
            data=payload, 
            async_=async_, 
            **request_kwargs, 
        )

3. 检查响应

接口被调用后,如果返回的是 dict 类型的数据(说明原本是 JSON),则可以用 p115client.check_response 执行检查。首先会查看其中名为 "state" 的键的对应值,如果为 True、1 或不存在,则原样返回被检查的数据;否则,"state" 的对应值大概是 False 或 0,说明有问题出现,会根据实际情况抛出一个异常,但都是 p115client.P115OSError 的实例。

from p115client import check_response

# 检查同步调用
data = check_response(client.method(payload))
# 检查异步调用
data = check_response(await client.method(payload, async_=True))

4. 辅助工具

一些简单的封装工具可能是必要的,特别是那种实现起来代码量比较少,可以封装成单个函数的。我把平常使用过程中,积累的一些经验具体化为一组工具函数。这些工具函数分别有着不同的功能,如果组合起来使用,或许能解决很多问题。

from p115client import tool

其它资源

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

p115client-0.0.2.4.1.tar.gz (57.3 kB view details)

Uploaded Source

Built Distribution

p115client-0.0.2.4.1-py3-none-any.whl (61.4 kB view details)

Uploaded Python 3

File details

Details for the file p115client-0.0.2.4.1.tar.gz.

File metadata

  • Download URL: p115client-0.0.2.4.1.tar.gz
  • Upload date:
  • Size: 57.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.11.8 Darwin/23.5.0

File hashes

Hashes for p115client-0.0.2.4.1.tar.gz
Algorithm Hash digest
SHA256 40eea56fc991343a5240bb5325d3c32200dbaeb741a7b8775716cf71db4c343f
MD5 b86f78dd20b197a9cc004f17fa38ce5e
BLAKE2b-256 228ffd32bfc2e3728b777882cc5ed11ec352bf0ed53df6e2faf75e701c98b108

See more details on using hashes here.

Provenance

File details

Details for the file p115client-0.0.2.4.1-py3-none-any.whl.

File metadata

  • Download URL: p115client-0.0.2.4.1-py3-none-any.whl
  • Upload date:
  • Size: 61.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.11.8 Darwin/23.5.0

File hashes

Hashes for p115client-0.0.2.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ada56dece558c59339969794a2420bd78ce784cdb2906c1c0874ae8bf8e7c36d
MD5 64378a7c4bfb97e731a158944fea8ed6
BLAKE2b-256 3fc6a4c0ee086d6987fb5552c18e35b6369b7cdcac696169b557cc26f2f08973

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page