Set of utilities related to the Fediverse to assist on simple Python projects
Project description
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
MastodonHelperrelated work. - Facilitates methods to publish simple text, full
StatusPostobjects 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
Configobjects
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
- Assuming you have
pipinstalled:
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.
- 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyxavi_activitypub-0.1.0.tar.gz.
File metadata
- Download URL: pyxavi_activitypub-0.1.0.tar.gz
- Upload date:
- Size: 27.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.10.1 Darwin/23.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd03dabe946176c1cb11007594647cf634154877838aef9dd5859c3a860111f0
|
|
| MD5 |
9fadd6768c8e14e119ae44ad37a07f56
|
|
| BLAKE2b-256 |
68fe2343921ee259f88583e5ae12266c9935bf58fc9bc99cc6d32a11ef2b0ccc
|
File details
Details for the file pyxavi_activitypub-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pyxavi_activitypub-0.1.0-py3-none-any.whl
- Upload date:
- Size: 27.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.10.1 Darwin/23.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae1418149cb325adb863255be6b211e18d34a2263647f9ecc90311fcc49ce9f2
|
|
| MD5 |
e706331d20451d38954b031aded7d7bd
|
|
| BLAKE2b-256 |
1aabac8ef1e4e9c5bcf318622265feeb962d526d4e65bd303d52e14a306ff491
|