NexusPHP API Client - A Python client for interacting with NexusPHP forum API
Project description
NexusPHP API Client
NexusPHP API 客户端,用于与 NexusPHP 论坛 API 进行交互的 Python 库。
项目结构
nexusphp_api/
├── api/ # API 数据模型
│ ├── bookmark/ # 收藏相关
│ ├── comment/ # 评论相关
│ ├── section/ # 分区相关
│ ├── torrent/ # 种子相关
│ └── user/ # 用户相关
├── api_client/ # API 客户端实现
├── config/ # 配置管理
├── utils/ # 工具库
├── tests/ # 测试代码
├── doc/ # API 文档
├── pyproject.toml # PEP 517/518 构建配置
├── setup.cfg # 包元数据和配置
└── requirements.txt # 项目依赖
功能模块
1. 收藏管理 (bookmark)
- 添加收藏
- 删除收藏
2. 评论管理 (comment)
- 获取评论列表
3. 分区管理 (section)
- 获取分区信息
4. 种子管理 (torrent)
- 获取种子列表
- 获取种子详情
- 上传种子
5. 用户管理 (user)
- 获取用户资料
安装
从 PyPI 安装(推荐)
pip install nexusphp-api
从源代码安装
git clone https://github.com/nexusphp/nexusphp-api.git
cd nexusphp-api
pip install -e .
安装开发依赖
pip install -r requirements.txt
快速开始
基本使用
from nexusphp_api import NexusAPIClient
# 初始化客户端
client = NexusAPIClient(
base_url="https://dev.nexusphp.org",
token="your-api-token"
)
# 获取当前用户资料
profile = client.profile.get_profile()
print(f"用户: {profile.data.data.username}")
# 添加收藏
bookmark = client.bookmark.add_bookmark(torrent_id=123)
print(f"收藏状态: {bookmark.msg}")
# 删除收藏
result = client.bookmark.delete_bookmark(torrent_id=123)
print(f"删除结果: {result.msg}")
动态更新配置
# 更新 token
client.set_token("new-token")
# 更新 base_url
client.set_base_url("https://api.example.com")
配置管理
项目支持通过环境变量进行配置:
| 环境变量 | 默认值 | 说明 |
|---|---|---|
| API_BASE_URL | https://api.nexusphp.org | API 基础 URL |
| API_TIMEOUT | 30 | API 请求超时时间(秒) |
| API_TOKEN | "" | 认证 Token |
| LOG_LEVEL | INFO | 日志级别 |
| LOG_FILE | api.log | 日志文件 |
| MAX_RETRIES | 3 | 最大重试次数 |
| RETRY_INTERVAL | 1.0 | 重试间隔(秒) |
API 参考
NexusAPIClient 主类
from nexusphp_api import NexusAPIClient
# 初始化
client = NexusAPIClient(
base_url="https://dev.nexusphp.org", # 可选,默认值
token="your-token" # 可选
)
# 子客户端
client.profile # 用户资料
client.bookmark # 收藏管理
client.comment # 评论管理
client.section # 分区管理
client.torrent # 种子管理
可用的客户端方法
ProfileClient(用户资料)
get_profile(user_id=None, includes=None, include_fields=None)
BookmarkClient(收藏)
add_bookmark(torrent_id)delete_bookmark(torrent_id)
CommentClient(评论)
- 详见代码和文档
SectionClient(分区)
- 详见代码和文档
TorrentClient(种子)
- 详见代码和文档
运行测试
运行所有测试
pytest tests/
运行特定测试
pytest tests/test_api_client.py
运行测试并显示覆盖率
pytest tests/ --cov=nexusphp_api --cov-report=html
使用 unittest 运行
python -m unittest discover tests
代码质量检查
项目配置了以下代码质量工具:
格式化代码
black .
isort .
代码 lint 检查
flake8 .
类型检查
mypy .
响应格式
所有 API 响应都遵循以下格式:
{
"ret": 0, // 结果码,0 成功,其他失败
"msg": "success", // 文本说明
"data": {}, // 响应数据
"rid": "uuid", // 请求 ID
"time": 0.123 // 响应耗时(秒)
}
工具库
项目提供了以下工具函数,用于数据转换和处理:
from_str- 字符串转换from_int- 整数转换from_int_optional- 可空整数转换from_float- 浮点数转换from_bool- 布尔值转换from_none- None 转换from_union- 联合类型转换from_list- 列表转换from_dict- 字典转换to_float- 浮点数输出转换to_class- 类实例转换to_enum- 枚举转换
开发和贡献
本地开发
- Fork 项目仓库
- 克隆到本地
- 安装开发依赖
- 创建功能分支
- 提交变更
- 推送到分支
- 创建 Pull Request
构建和发布
# 构建包
python -m build
# 发布到 PyPI
twine upload dist/*
注意事项
- 使用前请确保已获取有效的 API Token
- 遵守 API 调用频率限制
- 处理 API 响应时请检查
ret字段确认操作是否成功 - 网络请求失败时会自动重试(默认 3 次)
许可证
MIT License
支持和联系
如有问题或建议,请:
- 提交 Issue: GitHub Issues
- 查看文档: 项目 Wiki
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
nexusphp_api-0.1.1.tar.gz
(24.4 kB
view details)
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 nexusphp_api-0.1.1.tar.gz.
File metadata
- Download URL: nexusphp_api-0.1.1.tar.gz
- Upload date:
- Size: 24.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c06aa27917ec393472328a6045977a54b2d199395bfadb669ff043368d47859a
|
|
| MD5 |
414d5c804e71fcd35cdc3dada7d990ec
|
|
| BLAKE2b-256 |
47a26e777aaed48aa46a38abe97c2901d681c26bc0a640e6230c56224b0e5503
|
File details
Details for the file nexusphp_api-0.1.1-py3-none-any.whl.
File metadata
- Download URL: nexusphp_api-0.1.1-py3-none-any.whl
- Upload date:
- Size: 36.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1185195491e51d76ab1ff4e970bfee100e879ea219adee0bed5e736b8229f4d
|
|
| MD5 |
36c1b15a06816ef0d3f7240f6358c585
|
|
| BLAKE2b-256 |
04516a1f5ec27225d00d88afd02f239d047de4dff59c852f38bffc04a821aecd
|