publish-meetups/publish_meetups/__init__.py

68 lines
1.5 KiB
Python
Raw Normal View History

2024-02-08 22:12:22 +00:00
from pathlib import Path
2024-02-11 18:00:48 +00:00
import json
2024-02-11 19:48:45 +00:00
import logging
2024-02-08 22:12:22 +00:00
2024-02-11 18:00:48 +00:00
import toml
from .utils import Paths, PROGRAM_NAME
2024-02-11 19:48:45 +00:00
from .feeds.mastodon_feed import MastodonFeed
NAME_TO_FEED = {
"mastodon": MastodonFeed,
}
2024-02-08 22:12:22 +00:00
class PublishMeetups:
def __init__(self):
2024-02-11 18:00:48 +00:00
self.config_file: Path = Path(Paths.CONFIG_PATH, "publish-meetups.toml")
print(self.config_file)
self.config = {}
2024-02-08 22:12:22 +00:00
2024-02-11 18:00:48 +00:00
self.config = {
"active_feeds": [
"mastodon",
],
"mastodon": {},
2024-02-08 22:12:22 +00:00
}
2024-02-11 19:48:45 +00:00
self.logger = logging.getLogger(__name__)
2024-02-08 22:12:22 +00:00
def __enter__(self):
2024-02-11 18:00:48 +00:00
if self.config_file.exists():
with self.config_file.open("r") as f:
self.config.update(toml.load(f))
2024-02-08 22:12:22 +00:00
return self
2024-02-11 19:48:45 +00:00
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()
2024-02-08 22:12:22 +00:00
def __exit__(self, exc_type, exc_val, exc_tb):
2024-02-11 19:48:45 +00:00
print("Exiting")
2024-02-11 18:00:48 +00:00
with self.config_file.open("w") as f:
toml.dump(self.config, f)