implemented mastodon

This commit is contained in:
Hellow
2024-02-11 20:48:45 +01:00
parent b4926729d6
commit 6e9574f376
6 changed files with 93 additions and 44 deletions

View File

@@ -1,9 +1,19 @@
class Feed:
@classmethod
def prompt_auth(cls, existing_config: dict) -> dict:
return existing_config
def __init__(self):
pass
def prompt_auth(self):
return {}
def __enter__(self):
return self
def create_post(self):
def post(self, message: str):
pass
def run(self):
self.post("Hello, World!")
def __exit__(self, exc_type, exc_value, traceback):
pass

View File

@@ -3,48 +3,35 @@ from pathlib import Path
from mastodon import Mastodon
from . import Feed
from ..utils import CONFIG_PATH, PROGRAM_NAME, prompt
from ..utils import Paths, PROGRAM_NAME, prompt
class MastodonFeed(Feed):
CLIENTCRED_PATH: Path = CONFIG_PATH.joinpath("mastodon_clientcred.secret")
USERCRED_PATH: Path = CONFIG_PATH.joinpath("mastodon_usercred.secret")
CLIENTCRED_PATH: Path = Paths.CONFIG_PATH.joinpath("mastodon_clientcred.secret")
@classmethod
def prompt_auth(cls, existing_config: dict) -> dict:
"""
mastodon needs:
- the instance used
- an access token
"""
return {
**existing_config,
"api_base_url": prompt.for_string("The instance you use", "https://mastodon.social"),
"access_token": prompt.for_password("Access token"),
}
# https://github.com/halcy/Mastodon.py
def __init__(self, username: str, password: str, api_base_url: str):
if not self.CLIENTCRED_PATH.exists():
self._create_app(api_base_url)
self.mastodon = Mastodon(client_id=self.CLIENTCRED_PATH)
self.mastodon.log_in(
username=username,
password=password,
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 _create_app(self, api_base_url: str):
client_id, client_secret = Mastodon.create_app(
PROGRAM_NAME,
api_base_url=api_base_url,
to_file=self.CLIENTCRED_PATH
)
self._save_clientcred(client_id, client_secret)
def prompt_auth(self) -> dict:
"""
mastodon needs:
- the instance used
- the email of the user
- the password of the user
"""
return {
"api_base_url": prompt.for_string("The instance you use", "https://mastodon.social"),
"username": prompt.for_string("E-Mail"),
"password": prompt.for_password("Password"),
}
def post(self, message: str):
print(f"Posting to Mastodon: {message}")
self.mastodon.toot(message)