telegraph-sdk
简介
Telegraph 是一个异步客户端,用于与 Telegraph API 交互。它支持创建用户账户、发布文章、查看文章阅读次数以及获取用户的文章列表。
安装
首先,确保您已经安装了 telegraph-sdk 库:
pip install telegraph-sdk
用法
创建 Telegraph 对象
from telegraph_sdk import Telegraph
tg = Telegraph(
short_name='your_short_name',
author_name='your_author_name',
author_url='your_author_url',
proxy='http://127.0.0.1:51623'
)
short_name: 用户的短名称(字母和数字组合)author_name: 作者名称author_url: 作者的 URLproxies: 代理配置(可选)
初始化用户
在使用任何方法前,首先需要初始化用户(获取 access_token)。
await tg.init_user()
创建文章页面
page_url = await tg.create_page(
title='Page Title',
content='<p>Page Content</p>',
author_name='Author Name',
author_url='https://author.url'
)
print(f"Page created: {page_url}")
title: 页面标题content: 页面内容,支持 HTML 格式author_name: 作者名称(可选)author_url: 作者 URL(可选)
获取文章阅读次数
views = await tg.get_views('page-path')
print(f"Views: {views}")
page-path: 文章路径,例如telegra.ph/page-title-123
获取文章列表
pages = await tg.get_page_list()
print(f"Pages: {pages}")
关闭客户端
await tg.close()
示例
import asyncio
from telegraph_sdk import Telegraph
async def main():
tg = Telegraph(
short_name='example_short_name',
author_name='example_author',
author_url='https://example.com',
proxy='http://127.0.0.1:51623'
)
try:
# 创建页面
page_url = await tg.create_page(
title='Test Page',
content='<p>This is a test page.</p>',
author_name='Example Author',
author_url='https://example.com'
)
print(f"Page created: {page_url}")
finally:
# 关闭会话
await tg.close()
# 运行异步事件循环
asyncio.run(main())
错误处理
- 如果请求失败,客户端会进行最多 3 次重试。
- 请求错误将记录为警告日志并抛出异常。
Release files for telegraph-sdk 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| telegraph-sdk-0.1.4.tar.gz | 3.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| telegraph_sdk-0.1.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.0 kB
Release files / telegraph-sdk-0.1.4.tar.gz
| Download URL | telegraph-sdk-0.1.4.tar.gz |
|---|---|
| Size | 3.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
60b94c1d6d44eb62937040c62bde55c260fbb0d39d91d8bbd9e54d7842bb58c0
|
|
BLAKE2b-256 checksum How to use checksums |
ba94f0afadc06a39ab7b82841e7c5d109306957e792cb5fa3fd89d3a373a3c40
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.6
|
Release files / telegraph_sdk-0.1.4-py3-none-any.whl
| Download URL | telegraph_sdk-0.1.4-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6d5ecf4e2e6af6f4819f057d8c7f4886b41dce2ba5d1377b44f575ea2ecfa916
|
|
BLAKE2b-256 checksum How to use checksums |
f216bde05095990c717df2c884f348adfd74857b9ac5c3a1bc1609d32e46dc65
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.6
|