guangyapan client
guangyapan 是一个 光鸭网盘 的 Python 客户端模块,不过仅提供最直接的接口包装。
支持同步和异步操作,全面封装了各种接口。
安装
你可以从 pypi 安装最新版本
pip install -U guangyapan
或者从 github 安装最新版本
pip install -U git+https://github.com/ChenyangGao/guangyapan@main
入门介绍
1. 导入模块
导入模块
from guangyapan import GuangyaPanClient
2. 创建实例
创建客户端对象,可以传入 access_token 和 refresh_token,如果不传,就自己调用 GuangyaPanClient.login() 方法登录
1. 用 refresh_token 创建实例
refresh_token = "..."
client = GuangyaPanClient(refresh_token)
# NOTE: 立即刷新一个访问令牌出来
client.refresh_access_token()
2. 用 access_token 创建实例
access_token = "..."
client = GuangyaPanClient(access_token)
# NOTE: 底层会在检测到 refresh_token 缺失,然后用这个 access_token 授权登录然后获取新的 access_token 和 refresh_token
client.refresh_access_token()
3. 手动登录
client = GuangyaPanClient()
# NOTE: 命令行等待扫码登录
client.login()
# 或者提供手机号,则会发送验证码登录
mobile = "+86 xxxxxxxxxxx"
client.login(mobile)
3. 接口调用
我推荐你选择
ipython作为执行环境,可以交互式地执行代码和分析结果
所有需要直接或间接执行 HTTP 请求的接口,都有同步和异步的调用方式
# 同步调用
client.method(payload)
client.method(payload, async_=False)
# 异步调用
await client.method(payload, async_=True)
它们都能接受一个参数 request,具体要求可以查看 guangyapan.request 的文档。我也封装了一些模块, 它们都能提供一个符合要求的 request 函数。更一般的实现,可以参考 python-http_request。
- aiohttp_client_request
- aiosonic_request
- asks_request
- blacksheep_client_request
- curl_cffi_request
- http_client_request
- httpcore_request
- httpx_request
- niquests_request
- pycurl_request
- python-urlopen
- requests_request
- tornado_client_request
- urllib3_request
- urllib3_future_request
注意:从根本上讲,所有接口的封装,最终都会调用 GuangyaPanClient.request
4. 检查响应
接口被调用后,如果返回的是 dict 类型的数据(说明原本是 JSON),则可以用 guangyapan.check_response 执行检查。首先会查看其中名为 "state" 的键的对应值,如果为 True、1 或不存在,则原样返回被检查的数据;否则,"state" 的对应值大概是 False 或 0,说明有问题出现,会根据实际情况抛出一个异常,但都是 guangyapan.P115OSError 的实例。
from guangyapan import check_response
# 检查同步调用
data = check_response(client.method(payload))
# 检查异步调用
data = check_response(await client.method(payload, async_=True))
# 或者
data = await check_response(client.method(payload, async_=True))
5. 辅助工具
一些简单的封装工具可能是必要的,特别是那种实现起来代码量比较少,可以封装成单个函数的。我把平常使用过程中,积累的一些经验具体化为一组工具函数。这些工具函数分别有着不同的功能,如果组合起来使用,或许能解决很多问题。
from guangyapan import tool
Metadata
Release files for guangyapan 0.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| guangyapan-0.0.0.tar.gz | 24.9 kB | Without provenance | Details for guangyapan-0.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| guangyapan-0.0.0-py3-none-any.whl | Python 3 | none | any | Details for guangyapan-0.0.0-py3-none-any.whl |
Total release size: 52.7 kB
Release files / guangyapan-0.0.0.tar.gz
| Download URL | guangyapan-0.0.0.tar.gz |
|---|---|
| Size | 24.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ef493e7d2c6c6b65ace0179d7771e8eacf5b918374bceec05ea1a614595c01d1
|
|
BLAKE2b-256 checksum How to use checksums |
5939e664f529bab953d0b6fbaec512e9fe3b75f7cec760630acb1ad6ba8d5f6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.13.5 Darwin/25.0.0
|
Release files / guangyapan-0.0.0-py3-none-any.whl
| Download URL | guangyapan-0.0.0-py3-none-any.whl |
|---|---|
| Size | 27.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8106615eff92b8eed80c4e92fa9bea8fd32cc7701f18d7c49f40c7e0288d169e
|
|
BLAKE2b-256 checksum How to use checksums |
4e1f8ce1c2a3f558f127289c65ce26a0be5d5b5a03dc4f99aaa1a27f6e8d5a11
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.13.5 Darwin/25.0.0
|
Security for guangyapan 0.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.