Skip to main content

A nano implementation of the AT Protocol (Authenticated Transfer Protocol) for Python.

Project description

nanoatp

PyPI GitHub License GitHub last commit   EN | JA | ES | ZH

A nano implementation of the AT Protocol (Authenticated Transfer Protocol) for Python.

Getting started

  • First, install the package.
pip install nanoatp
  • Set your credentials as environment variables. Or you can pass them to BskyAgent.login().
export ATP_IDENTIFIER="foo.bsky.social"
export ATP_PASSWORD="password"
  • Then in your application,
from nanoatp import BskyAgent, RichText

agent = BskyAgent("https://bsky.social")
agent.login()

# post a simple text
record = {"text": "Hello World!"}
response = agent.post(record)
print(response)

# create a RichText
rt = RichText("Hello @ota.bsky.social, check out this link: https://example.com")
rt.detectFacets(agent)
print(rt.facets)

# upload an image
image = agent.uploadImage("example.png")

# post it
embed = {"$type": "app.bsky.embed.images#main", "images": [image]}
record = {"text": rt.text, "facets": rt.facets, "embed": embed}
response = agent.post(record)
print(response)

See examples for more.

Usage

Session management

Log into a server using these APIs. You'll need an active session for most methods.

from nanoatp import BskyAgent

agent = BskyAgent("https://bsky.social")

# if you don't specify credentials,
# ATP_IDENTIFIER and ATP_PASSWORD environment variables will be used
agent.login("alice@mail.com", "hunter2")

API calls

The agent includes methods for many common operations, including:

# Feeds and content
agent.getPost(repo, rkey, cid)
agent.post(record)
agent.deletePost(postUri)
agent.uploadBlob(data, encoding)
agent.uploadImage(path, alt, encoding)  # wrapper for uploadBlob

# Identity
agent.resolveHandle(handle)

# Session management
agent.login(identifier, password)

Rich text

Some records (ie posts) use the app.bsky.richtext lexicon. At the moment richtext is only used for links and mentions, but it will be extended over time to include bold, italic, and so on.

ℹ️ Currently the implementation is very naive. I have not tested it with UTF-16 text.

from nanoatp import BskyAgent, RichText

agent = BskyAgent()
agent.login()

rt = RichText("Hello @ota.bsky.social, check out this link: https://example.com")
rt.detectFacets(agent)
record = {"text": rt.text, "facets": rt.facets}
agent.post(record)

Advanced

Advanced API calls

The methods above are convenience wrappers. It covers most but not all available methods.

The AT Protocol identifies methods and records with reverse-DNS names. You can use them on the agent as well:

res1 = agent.createRecord(
    agent.session["did"],  # repo
    "app.bsky.feed.post",  # collection
    {
        "$type": "app.bsky.feed.post",
        "text": "Hello, world!",
        "createdAt": datetime.datetime.now(datetime.timezone.utc).isoformat().replace("+00:00", "Z")
    }
)

Development

export ATP_IDENTIFIER="foo.bsky.social"
export ATP_PASSWORD="password"
poetry install
poetry run pytest -s     # run pytest once
poetry run -- ptw -- -s  # watch for changes and run pytest

TODO:

  • split BskyAgent and AtpAgent code
  • implement a proper RichText parser with UTF-16 (currently it's very naive)
  • type definitions
  • structured tests
  • more APIs

License

MIT License. See LICENSE for details.

Author

S. Ota

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

nanoatp-0.3.2.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

nanoatp-0.3.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file nanoatp-0.3.2.tar.gz.

File metadata

  • Download URL: nanoatp-0.3.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.9 Darwin/22.3.0

File hashes

Hashes for nanoatp-0.3.2.tar.gz
Algorithm Hash digest
SHA256 0bd5ef86979f98b264a3da0e09244cc1bff69e224ce9f497659bb7c873307aba
MD5 1e1c934c20b818294cd364d72d4eda5c
BLAKE2b-256 37bf7d86a6b27c9fa1f01db462dae5ee86e928ba6c13ddc2bc64c6c3887a5823

See more details on using hashes here.

File details

Details for the file nanoatp-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: nanoatp-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.9 Darwin/22.3.0

File hashes

Hashes for nanoatp-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 99cf9ece56c1f3f10e6817176ca27fffedfeaa2cbd70b1e90fa77c864ed8c3fd
MD5 078856d5576d6e0133796c72169e60c6
BLAKE2b-256 226412fc7c6c782f9c6dd080e986c9494beddaa418dc57ec630c590dbd9c49c4

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 Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page