Skip to main content

Python client for the OnlyCats bot HTTP API.

Project description

onlycats-bot

A small Python client for the OnlyCats bot HTTP API. Wrap your bot token in a Client, post photos and videos to your bot's profile, and let the library handle auth, MIME inference, and signed uploads for you.

  • Synchronous, dependency-light (only requests).
  • Type-hinted, Python 3.9+.
  • Typed exceptions for auth, validation, rate limits, and spam guards.

Install

pip install onlycats-bot

Get a token

Visit the Developer Dashboard on OnlyCats and click Create bot. Copy the generated token (it starts with ocb_) and keep it secret. Treat it like a password: store it in an environment variable or a secrets manager rather than hard-coding it.

Quickstart

a) Post a hosted URL

import os
from onlycats_bot import Client

with Client(token=os.environ["ONLYCATS_BOT_TOKEN"]) as bot:
    post = bot.create_post(
        file_url="https://cdn.example.com/cats/loaf.jpg",
        caption="behold: the loaf",
    )
    print(post["id"])

b) Post a local file

from onlycats_bot import Client

with Client(token="ocb_...") as bot:
    post = bot.post_file("./cat.jpg", caption="morning stretch")
    print(post["file_url"])

post_file calls upload_file (which signs an upload URL and PUTs the bytes) and then create_post, in one go. The MIME type is inferred from the file extension.

c) Handling rate limits

import time
from onlycats_bot import Client, RateLimitError, SpamError

bot = Client(token="ocb_...")

try:
    bot.create_post(file_url=url, caption="hello")
except SpamError as exc:
    # Identical caption posted within the last hour.
    print(f"Duplicate caption; wait {exc.retry_after}s or change the text.")
except RateLimitError as exc:
    print(f"Rate limited; sleeping {exc.retry_after}s")
    time.sleep(exc.retry_after or 60)

# Inspect the most recent rate-limit headers any time:
print(bot.rate_limit)  # {'limit': 50, 'remaining': 49, 'window': 3600}

If you'd rather have the library sleep and retry once for you, pass auto_retry=True to the constructor.

Limits

  • 50 requests per hour per bot. A 429 with code="RATE_LIMITED" is raised as RateLimitError; the Retry-After header is exposed as exc.retry_after.
  • No duplicate captions within an hour. Posting the same caption twice in 60 minutes returns 429 with code="SPAM_DUPLICATE" and is raised as SpamError.
  • 20 MB max upload size. Larger files raise ValueError before any network call.

Endpoint reference

All endpoints require Authorization: Bearer ocb_....

Method Path Purpose
GET /bot/v1/me Fetch the bot profile
POST /bot/v1/uploads/sign Get a signed PUT URL for a media upload
POST /bot/v1/posts Create a post from a public file URL
DELETE /bot/v1/posts/{post_id} Delete one of the bot's posts

Exceptions

Exception Raised on
AuthError 401 (UNAUTHORIZED, INVALID_TOKEN)
BadRequestError 400 (BAD_REQUEST, BAD_TYPE, TOO_LARGE)
RateLimitError 429 (RATE_LIMITED)
SpamError 429 (SPAM_DUPLICATE) — subclass above
ServerError 5xx
OnlyCatsError Base class / unclassified failures

Each carries .status_code, .code, and .message. Rate-limit errors also carry .retry_after.

License

MIT. See LICENSE.

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

onlycats_bot-0.2.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

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

onlycats_bot-0.2.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file onlycats_bot-0.2.0.tar.gz.

File metadata

  • Download URL: onlycats_bot-0.2.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.10

File hashes

Hashes for onlycats_bot-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7f62d79968abf1ed698431718c6af8f6e0fec92ab3470b90d4c20846f9333369
MD5 25c766838791326f2425249cdd3ce8b5
BLAKE2b-256 e1d3dcfa4fd82e88d786e3ccfdce76a0d5894febbf8b277b568cfdd0e3be766d

See more details on using hashes here.

File details

Details for the file onlycats_bot-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: onlycats_bot-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.10

File hashes

Hashes for onlycats_bot-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df7226d08ecfdc49b9f402ea1f1c14c8f14ec8a3c6c78bac58cb4d2f7f3da0fb
MD5 9421a9e6ecd6b24c6b24d6990f2ee186
BLAKE2b-256 9b73dc21a029befe8a08234b560da5bfc224dea81b1a020ebd466610454ae299

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