publish-meetups/publish_meetups/__init__.py
2024-02-12 22:17:48 +01:00

77 lines
1.9 KiB
Python

from pathlib import Path
import json
import logging
import toml
from .utils import Paths, PROGRAM_NAME
from .feeds.mastodon_feed import MastodonFeed
from .feeds.twitter_feed import TwitterFeed
NAME_TO_FEED = {
"mastodon": MastodonFeed,
"twitter": TwitterFeed,
}
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",
"twitter",
],
"mastodon": {},
"twitter": {},
}
self.logger = logging.getLogger(__name__)
def __enter__(self):
if self.config_file.exists():
with self.config_file.open("r", encoding="utf-8") 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", encoding="utf-8") as f:
toml.dump(self.config, f)
with feed_class(**feed_config, config=feed_config) as feed_instance:
self.config[feed].update(feed_instance.config)
with self.config_file.open("w", encoding="utf-8") as f:
toml.dump(self.config, f)
feed_instance.run()
def __exit__(self, exc_type, exc_val, exc_tb):
print("Exiting")
with self.config_file.open("w", encoding="utf-8") as f:
toml.dump(self.config, f)