publish-meetups/publish_meetups/__init__.py

77 lines
1.9 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
2024-02-12 21:17:48 +00:00
from .feeds.twitter_feed import TwitterFeed
2024-02-11 19:48:45 +00:00
NAME_TO_FEED = {
"mastodon": MastodonFeed,
2024-02-12 21:17:48 +00:00
"twitter": TwitterFeed,
2024-02-11 19:48:45 +00:00
}
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",
2024-02-12 21:17:48 +00:00
"twitter",
2024-02-11 18:00:48 +00:00
],
"mastodon": {},
2024-02-12 21:17:48 +00:00
"twitter": {},
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():
2024-02-12 21:17:48 +00:00
with self.config_file.open("r", encoding="utf-8") as f:
2024-02-11 18:00:48 +00:00
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
2024-02-12 21:17:48 +00:00
with self.config_file.open("w", encoding="utf-8") as f:
2024-02-11 19:48:45 +00:00
toml.dump(self.config, f)
2024-02-12 21:17:48 +00:00
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()
2024-02-11 19:48:45 +00:00
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-12 21:17:48 +00:00
with self.config_file.open("w", encoding="utf-8") as f:
2024-02-11 18:00:48 +00:00
toml.dump(self.config, f)