Python guangyapan client.
Project description
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file guangyapan-0.0.0.tar.gz.
File metadata
- Download URL: guangyapan-0.0.0.tar.gz
- Upload date:
- Size: 24.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.4.1 CPython/3.13.5 Darwin/25.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ef493e7d2c6c6b65ace0179d7771e8eacf5b918374bceec05ea1a614595c01d1
|
|
| MD5 |
b2a5bf3d61c09221bae577252597bf71
|
|
| BLAKE2b-256 |
5939e664f529bab953d0b6fbaec512e9fe3b75f7cec760630acb1ad6ba8d5f6e
|
File details
Details for the file guangyapan-0.0.0-py3-none-any.whl.
File metadata
- Download URL: guangyapan-0.0.0-py3-none-any.whl
- Upload date:
- Size: 27.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.4.1 CPython/3.13.5 Darwin/25.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8106615eff92b8eed80c4e92fa9bea8fd32cc7701f18d7c49f40c7e0288d169e
|
|
| MD5 |
277754ccb44fe960a222db8eb1740dc3
|
|
| BLAKE2b-256 |
4e1f8ce1c2a3f558f127289c65ce26a0be5d5b5a03dc4f99aaa1a27f6e8d5a11
|