publish-meetups/publish_meetups/__init__.py

68 lines
1.5 KiB
Python

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:
def __init__(self):
self.config_file: Path = Path(Paths.CONFIG_PATH, "publish-meetups.toml")
print(self.config_file)
self.config = {}
self.config = {
"active_feeds": [
"mastodon",
],
"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))
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)