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,
|
|
|
|
)
|
|
|
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def post(self, message: str):
|
|
|
|
print(f"Posting to Mastodon: {message}")
|
|
|
|
self.mastodon.toot(message)
|