publish-meetups/publish_meetups/feeds/mastodon_feed.py

41 lines
1.0 KiB
Python
Raw Normal View History

2024-02-11 18:00:48 +00:00
from pathlib import Path
from mastodon import Mastodon
from . import Feed
2024-02-11 19:48:45 +00:00
from ..utils import Paths, PROGRAM_NAME, prompt
2024-02-11 18:00:48 +00:00
class MastodonFeed(Feed):
2024-02-11 19:48:45 +00:00
CLIENTCRED_PATH: Path = Paths.CONFIG_PATH.joinpath("mastodon_clientcred.secret")
2024-02-11 18:00:48 +00:00
2024-02-11 19:48:45 +00:00
@classmethod
def prompt_auth(cls, existing_config: dict) -> dict:
2024-02-11 18:00:48 +00:00
"""
mastodon needs:
- the instance used
2024-02-11 19:48:45 +00:00
- an access token
2024-02-11 18:00:48 +00:00
"""
return {
2024-02-11 19:48:45 +00:00
**existing_config,
2024-02-11 18:00:48 +00:00
"api_base_url": prompt.for_string("The instance you use", "https://mastodon.social"),
2024-02-11 19:48:45 +00:00
"access_token": prompt.for_password("Access token"),
2024-02-11 18:00:48 +00:00
}
2024-02-11 19:48:45 +00:00
# https://github.com/halcy/Mastodon.py
def __init__(self, api_base_url: str, access_token: str, **kwargs):
self.mastodon = Mastodon(
api_base_url=api_base_url,
access_token=access_token,
)
2024-02-12 21:17:48 +00:00
super().__init__(**kwargs)
2024-02-11 19:48:45 +00:00
def post(self, message: str):
2024-02-12 21:17:48 +00:00
kwargs = locals().copy()
2024-02-11 19:48:45 +00:00
self.mastodon.toot(message)
2024-02-12 21:17:48 +00:00
Feed.post(**kwargs)