Skip to main content

No project description provided

Project description

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: 作者的 URL
  • proxies: 代理配置(可选)

初始化用户

在使用任何方法前,首先需要初始化用户(获取 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 次重试。
  • 请求错误将记录为警告日志并抛出异常。

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

telegraph-sdk-0.1.4.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

telegraph_sdk-0.1.4-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file telegraph-sdk-0.1.4.tar.gz.

File metadata

  • Download URL: telegraph-sdk-0.1.4.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.6

File hashes

Hashes for telegraph-sdk-0.1.4.tar.gz
Algorithm Hash digest
SHA256 60b94c1d6d44eb62937040c62bde55c260fbb0d39d91d8bbd9e54d7842bb58c0
MD5 ecb3b7521a8fc950c925088e8dda1c48
BLAKE2b-256 ba94f0afadc06a39ab7b82841e7c5d109306957e792cb5fa3fd89d3a373a3c40

See more details on using hashes here.

File details

Details for the file telegraph_sdk-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for telegraph_sdk-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6d5ecf4e2e6af6f4819f057d8c7f4886b41dce2ba5d1377b44f575ea2ecfa916
MD5 68c90fbfea7b387f877c9639edb7467d
BLAKE2b-256 f216bde05095990c717df2c884f348adfd74857b9ac5c3a1bc1609d32e46dc65

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page