From 6e9574f37682c4245682e3f0955ca98a18e8aa1b Mon Sep 17 00:00:00 2001 From: Hellow <74311245+HeIIow2@users.noreply.github.com> Date: Sun, 11 Feb 2024 20:48:45 +0100 Subject: [PATCH] implemented mastodon --- .vscode/launch.json | 14 ++++++ publish_meetups/__init__.py | 38 ++++++++++++++++- publish_meetups/__main__.py | 6 ++- publish_meetups/feeds/__init__.py | 16 +++++-- publish_meetups/feeds/mastodon_feed.py | 59 ++++++++++---------------- publish_meetups/utils/prompt.py | 4 +- 6 files changed, 93 insertions(+), 44 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..7b4add1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python Debugger: Module", + "type": "debugpy", + "request": "launch", + "module": "publish_meetups" + } + ] +} \ No newline at end of file diff --git a/publish_meetups/__init__.py b/publish_meetups/__init__.py index 4fb4515..b4bbcff 100644 --- a/publish_meetups/__init__.py +++ b/publish_meetups/__init__.py @@ -1,9 +1,17 @@ from pathlib import Path import json +import logging import toml from .utils import Paths, PROGRAM_NAME +from .feeds.mastodon_feed import MastodonFeed + + + +NAME_TO_FEED = { + "mastodon": MastodonFeed, +} class PublishMeetups: @@ -19,15 +27,41 @@ class PublishMeetups: "mastodon": {}, } + self.logger = logging.getLogger(__name__) + def __enter__(self): if self.config_file.exists(): with self.config_file.open("r") as f: self.config.update(toml.load(f)) - print(self.config['active_feeds'][0]) - return self + def run(self): + for feed in self.config['active_feeds']: + self.run_feed(feed) + + def run_feed(self, feed: str): + if feed not in NAME_TO_FEED: + self.logger.error(f"Feed {feed} is not implemented.") + return + + + feed_config = self.config.get(feed, {}) + feed_class = NAME_TO_FEED[feed] + + if not len(feed_config): + feed_config.update(feed_class.prompt_auth(feed_config)) + self.config[feed] = feed_config + + with self.config_file.open("w") as f: + toml.dump(self.config, f) + + + with feed_class(**feed_config) as f: + f.run() + + def __exit__(self, exc_type, exc_val, exc_tb): + print("Exiting") with self.config_file.open("w") as f: toml.dump(self.config, f) diff --git a/publish_meetups/__main__.py b/publish_meetups/__main__.py index 45212cc..e212366 100644 --- a/publish_meetups/__main__.py +++ b/publish_meetups/__main__.py @@ -3,4 +3,8 @@ from . import PublishMeetups def cli(): with PublishMeetups() as p: - print(p) + p.run() + + +if __name__ == "__main__": + cli() diff --git a/publish_meetups/feeds/__init__.py b/publish_meetups/feeds/__init__.py index 950baac..b272755 100644 --- a/publish_meetups/feeds/__init__.py +++ b/publish_meetups/feeds/__init__.py @@ -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 diff --git a/publish_meetups/feeds/mastodon_feed.py b/publish_meetups/feeds/mastodon_feed.py index c8e336e..42e386c 100644 --- a/publish_meetups/feeds/mastodon_feed.py +++ b/publish_meetups/feeds/mastodon_feed.py @@ -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) diff --git a/publish_meetups/utils/prompt.py b/publish_meetups/utils/prompt.py index 691ac72..fcd0fbf 100644 --- a/publish_meetups/utils/prompt.py +++ b/publish_meetups/utils/prompt.py @@ -12,10 +12,10 @@ def for_string(msg: str, default: Optional[str] = None) -> str: return input(msg + ': ').strip() - result = _real_prompt + result = _real_prompt() print("> " + result) return result def for_password(msg: str) -> str: - return getpass.getpass(msg + ': ').strip() + return getpass(msg + ': ').strip()