Skip to main content

The Xavi's ActivityPub Python package

Set of utilities to assist on simple Python projects, when it comes to connecting to ActivityPub software like Mastodon.

This is actually an abstraction of these classes relating to the Fediverse, to leave the original pyxavi package in a more lower and generic state, which becomes then a dependency for this pyxavi-activitypub package.

Disclaimer

This is a constant work in progress package, adding and improving the libraries within with the goal of abstracting and reusing code, and easing the coding experience of real life projects.

Suggestions are welcome :)

Modules included in the package

This package contains a set of modules, divided by functionality.

The Firefish module

A class that wraps the API for Firefish. It is meant to be interchangeable with the Mastodon.py wrapper library, so one could inject any of both.

At this point of time it only covers:

  • Posting a new status (creating a note in Firefish).
  • Posting new media (create a drive/media in Firefish)

The MastodonHelper module

A class that abstracts the instantiation of the Mastodon-like API wrapper. At this point it supports the original Mastodon.py wrapper that at its time supports Mastodon, Pleroma and Akkoma, and Firefish through the Firefish module above (which support is limited).

The class is meant to receive an object MastodonConnectionParams that is responsible of bringing in the parameters that facilitate the connection to the Mastodon wrappers and define some specifics regarding the server connecting to, like maximum post length and visibility.

Also includes a StatusPost that is meant to encapsulate everything that is needed to represent a Status to be posted. Internally it makes use of StatusPostVisibility and StatusPostContentType that are also referenced from the MastodonConnectionParams. While this object is meant to easy the transport of the status publishing item, it is not required and totally optional.

The benefit of using this set of tools is to encapsulate and abstract what is needed to initiate a connection to the Mastodon-like API and post a status, including the authorisation, making it really simple to include into a given app. One can even instantiate different wrappers to publish into different servers at the same time.

connection_params = MastodonConnectionParams.from_dict({
  "app_name": "SuperApp",
  "instance_type": "mastodon",
  "api_base_url": "https://mastodon.social",
  "credentials": {
    "user_file": "user.secret",
    "client_file": "client.secret",
    "user": {
        "email": "bot@my-fancy.site",
        "password": "SuperSecureP4ss",
    }
  }
})

mastodon_instance =  MastodonHelper.get_instance(
  connection_params=connection_params
)

mastodon_instance.status_post(
  status="I am a text"
)

The MastodonPublisher module

A class that abstracts the process of publishing text and media into a Mastodon-like API.

Benefits of using it:

  • Total encapsulation of MastodonHelper related work.
  • Facilitates methods to publish simple text, full StatusPost objects and media URLs or paths.
  • Retries with delay in case the communication is poor.
  • Slices the text so that it fits within the defined status maximum length
  • Proxies the posting through any of the supported instance types.
  • Supports the definition of a dry run so that execution can be tested without actual publishing
  • Supports parametrisation through Config objects

Having a Config like the following YAML:

publisher:
  media_storage: "storage/media/"
  dry_run: False
  named_account: test
mastodon:
  named_accounts:
    test:
      app_name: "Test"
      api_base_url: "https://mastodon.social"
      instance_type: "mastodon"
      credentials:
        client_file: "client_test.secret"
        user_file: "user_test.secret"
        user:
          email: "test@my-fancy.site"
          password: "SuperSecureP4ss"

Publishing is as simple as:

Publisher(config=Config()).publish_text("This is a test")

How to use it

  1. Assuming you have pip installed:
pip install pyxavi-activitypub

You can also add the pyxavi-activitypub package as a dependency of your project in its requirements.txt or pyproject.toml file.

  1. Import the desired module in your code. For example, in your my_python_script.py:
from pyxavi-activitypub import MastodonPublisher

Publisher(config=Config()).publish_text("This is a test")

ToDo

  • Documentation per module
  • Iterate inline documentation
  • Empty the NEXT MAJOR list

Metadata

Release files for pyxavi-activitypub 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyxavi-activitypub 0.1.0
File Size Uploaded Provenance
pyxavi_activitypub-0.1.0.tar.gz 27.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pyxavi-activitypub 0.1.0
File Interpreter ABI Platform
pyxavi_activitypub-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 54.8 kB

Release files / pyxavi_activitypub-0.1.0.tar.gz

Download URL pyxavi_activitypub-0.1.0.tar.gz
Size 27.4 kB
Tags Source
SHA-256 checksum
How to use checksums
fd03dabe946176c1cb11007594647cf634154877838aef9dd5859c3a860111f0
BLAKE2b-256 checksum
How to use checksums
68fe2343921ee259f88583e5ae12266c9935bf58fc9bc99cc6d32a11ef2b0ccc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.10.1 Darwin/23.1.0

Release files / pyxavi_activitypub-0.1.0-py3-none-any.whl

Download URL pyxavi_activitypub-0.1.0-py3-none-any.whl
Size 27.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ae1418149cb325adb863255be6b211e18d34a2263647f9ecc90311fcc49ce9f2
BLAKE2b-256 checksum
How to use checksums
1aabac8ef1e4e9c5bcf318622265feeb962d526d4e65bd303d52e14a306ff491
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.10.1 Darwin/23.1.0

Security for pyxavi-activitypub 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page