Skip to main content

Publish Markdown to Telegraph with external image hosting support

Project description

TelePress

中文文档

Publish Markdown, images and zip archives to Telegraph. Handles large files by auto-splitting into multiple linked pages.

Install

pip install telepress

# with REST API
pip install telepress[api]

# from source
git clone https://github.com/zoidberg-xgd/telepress
cd telepress && pip install -e .

Usage

from telepress import publish, publish_text

url = publish("article.md")
url = publish_text("# Hello\n\nWorld!", title="Test")

CLI:

telepress article.md --title "My Post"
telepress photos.zip --title "Album"

REST API:

telepress-server --port 8000

curl -X POST localhost:8000/publish/text \
  -H "Content-Type: application/json" \
  -d '{"content": "# Title\n\nBody", "title": "Test"}'

How it works

Text files are converted to Telegraph format (Markdown supported). Plain text files preserve line breaks as paragraphs. Large content is split at ~10KB boundaries into multiple pages with prev/next navigation.

Token is auto-created on first run and saved to ~/.telegraph_token.

Features

  • External image hosting: Upload to imgbb, imgur, or sm.ms (configurable)
  • Auto compression: Images over 5MB automatically compressed
  • Batch upload: Multi-threaded concurrent uploads with progress callback
  • Deduplication: Same content won't be uploaded twice
  • Auto-pagination: Large content split into multiple linked pages

Limits

  • Telegraph's direct image upload is unavailable, using external image hosts instead
  • 5MB per image (auto-compressed if larger)

Supported: .txt .md .markdown .rst .jpg .png .gif .webp .zip

Image Upload

Supported hosts: imgbb, imgur, sm.ms, Cloudflare R2, Custom API

Configuration File

Create ~/.telepress.json:

{
    "image_host": {
        "type": "r2",
        "account_id": "your_account_id",
        "access_key_id": "your_access_key",
        "secret_access_key": "your_secret_key",
        "bucket": "your_bucket",
        "public_url": "https://pub-xxx.r2.dev"
    }
}

Or use environment variables:

export TELEPRESS_IMAGE_HOST_TYPE=r2
export TELEPRESS_IMAGE_HOST_ACCESS_KEY_ID=xxx
export TELEPRESS_IMAGE_HOST_SECRET_ACCESS_KEY=xxx
export TELEPRESS_IMAGE_HOST_BUCKET=my-bucket
export TELEPRESS_IMAGE_HOST_PUBLIC_URL=https://pub-xxx.r2.dev

Usage

from telepress import ImageUploader

# Load from config file or env vars
uploader = ImageUploader()  # Auto-loads from ~/.telepress.json

# Or specify explicitly
uploader = ImageUploader('imgbb', api_key='your_key')
uploader = ImageUploader('r2', access_key_id='...', secret_access_key='...', bucket='...', public_url='...')

# Custom API endpoint
uploader = ImageUploader('custom',
    upload_url='https://your-api.com/upload',
    headers={'Authorization': 'Bearer xxx'},
    response_url_path='data.url'  # JSON path to URL in response
)

# Upload
url = uploader.upload('photo.jpg')

# Batch upload
results = uploader.upload_batch(image_paths)
print(f"Success: {results.success_rate:.0%}")

Image Compression

from telepress import compress_image_to_size, MAX_IMAGE_SIZE

# Compress to under 5MB
compressed_path, was_compressed = compress_image_to_size(
    "large_photo.png",
    max_size=MAX_IMAGE_SIZE,
    prefer_webp=False  # True for WebP output
)

Project structure

telepress/
├── core.py       # TelegraphPublisher
├── image_host.py # External image hosting (imgbb, imgur, smms)
├── uploader.py   # ImageUploader with batch support
├── utils.py      # Compression utilities
├── server.py     # FastAPI service
└── cli.py        # Command line

Error handling

from telepress import publish, ValidationError, TelePressError

try:
    url = publish("file.md")
except ValidationError as e:
    # bad input: wrong format, too large, etc
    print(e)
except TelePressError as e:
    # other errors: upload failed, auth failed, etc
    print(e)

Integration

# Flask
@app.route('/publish', methods=['POST'])
def api_publish():
    url = publish_text(request.json['content'], title=request.json['title'])
    return {'url': url}

# async
async def async_publish(content, title):
    return await asyncio.to_thread(publish_text, content, title)

Dev

git clone https://github.com/zoidberg-xgd/telepress
cd telepress && pip install -e .[dev]
pytest tests/ -v

License

MIT

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

telepress-0.2.1.tar.gz (45.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telepress-0.2.1-py3-none-any.whl (26.9 kB view details)

Uploaded Python 3

File details

Details for the file telepress-0.2.1.tar.gz.

File metadata

  • Download URL: telepress-0.2.1.tar.gz
  • Upload date:
  • Size: 45.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.16

File hashes

Hashes for telepress-0.2.1.tar.gz
Algorithm Hash digest
SHA256 f8f72f31dd904d07a43ddf178d41500753e7a2a3420ba3ef483847da90ff38cf
MD5 9127ee5131a6d3e2f380d7ad5cc261ca
BLAKE2b-256 99ffd95d3c526da6e8d4714176d1ffa3ff6ee79e4d2e0dba9c845cc00b2c90cf

See more details on using hashes here.

File details

Details for the file telepress-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: telepress-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 26.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.16

File hashes

Hashes for telepress-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b4b156de708e00c2abe9f6f143b9246033c2f72a7670f781bb5b35b73b5428fa
MD5 c3c722a2a3228df4b769e118ded82e25
BLAKE2b-256 3cb107edf1d8ebed6c2efda832f94a3e068d62c295d15ab5b479fbcf0512d896

See more details on using hashes here.

Supported by

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