Skip to main content

重要通知!!:由于原树洞更改了鉴权方式和请求地址,导致本包 v2.0.0 之前的版本均不可用,请及时将您的项目更新至 v2.0.0 及以上版本。

新版本树洞认证方法见下文


TreeHole

A simple Python interface to PKU Tree Hole

GitHub - License Git - Last Doc Commit

PyPI - Version PyPI - Downloads per month

Pytest - Result Git - Last Commit

目前正在持续开发完善中,欢迎使用和提出建议!

亮点

  • 简单易用
  • 文档齐全
  • token 登陆
  • Python 3.6+
  • 支持异步请求
  • 自动处理图像 url
  • 所有树洞数据模型均已封装为数据类
  • 涵盖(几乎?)所有暴露给用户的业务逻辑

安装

已发布至 Pypi 源,可直接使用 pip 安装:

pip3 install TreeHole

使用

身份验证

您有两种方式来验证身份:

  1. 使用用户名和密码登陆(不推荐)

    可以在实例化 TreeHoleClient 时传入 uid(学号)和 password(密码)参数即可,使用 IAAA 账号登陆

  2. 使用 token 登陆(推荐,相对安全)

    树洞 token 的获取方式请参考 @Guyutongxue 的操作说明(即此说明中的“北大树洞 JWT”),您也可以在浏览器的 cookies 中的 pku_token 字段处找到它。

代码示例

from treehole import TreeHoleClient

# 使用 token 认证
client = TreeHoleClient(token=<Your Token>)
# 使用 IAAA 账号认证
client = TreeHoleClient(uid=<UID>, password=<Password>)
# 获取单个树洞
hole = client.get_hole(<Hole ID>)
# 获取树洞评论
comments = client.get_comment(<Hole ID>)
# 获取首页树洞列表
holes = client.get_holes(<Page Num>)
# 获取关注树洞列表
holes = client.get_followed(<Page Num>)
# 切换关注状态
success, status = client.post_toggle_followed(<Hole ID>)
# 发布树洞
success = client.post_hole(<Text>, <Image File>)
# 发布评论
success = client.post_comment(<Hole ID>, <Text>, <Reply To>)
# 举报树洞 (!!!!!! 请勿轻易尝试)
success = client.post_report(<Hole ID>, <Reason>)

用例请参考 非异步异步

开发

克隆此仓库:

git clone git@github.com:TeddyHuang-00/pyTreeHole.git

编辑模式下:

pip3 install -e ".[test]"

欢迎提 issues 与 PR!

Roadmap

  • 支持新版树洞
  • 支持 IAAA 账号登陆
  • 树洞数据模型
  • 客户端封装
  • 获取单个树洞
  • 获取首页树洞
  • 获取关注树洞
  • 获取树洞回复
  • 关注/取关树洞
  • 回复树洞
  • 发布树洞
  • 举报树洞
  • 支持异步处理
  • 支持自定义加载长度
  • 更多功能待补充 ...

Release files for TreeHole 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for TreeHole 2.0.0
File Size Uploaded
TreeHole-2.0.0.tar.gz 11.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for TreeHole 2.0.0
File Interpreter ABI Platform
TreeHole-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.1 kB

Release files / TreeHole-2.0.0.tar.gz

Download URL TreeHole-2.0.0.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ee50931adf2d31ac576269234361b6e5a1526a7937e91ad42ca18cf0eccb632f
BLAKE2b-256 checksum
How to use checksums
2cda9759777e3c8229788dab3e7331b762423899df720d3afc1d3f3c7c5ae56b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.9

Release files / TreeHole-2.0.0-py3-none-any.whl

Download URL TreeHole-2.0.0-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9bd881e6f3f09a06767b11a87740eed386c63a5ebb96826764bed559d7928b51
BLAKE2b-256 checksum
How to use checksums
1c7c168710fb2c12d52b285d7b35823beccecdf696ebd09dae351000c55d723d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.9

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page