Compare commits

...

2 Commits

Author SHA1 Message Date
Hellow
e85681a708 fixed nested config 2024-02-15 01:08:45 +01:00
Hellow
0c4c41ff84 implementing formatting text 2024-02-15 00:27:52 +01:00
4 changed files with 61 additions and 30 deletions

View File

@@ -52,9 +52,7 @@ class TwitterFeed(Feed):
)
logged_in = True
config.set_field(self.__config_name__, {
'cookies': dict(self.client.http.client.cookies)
}, update_dict=True)
config.set_field(f"feed.{self.__config_name__}.cookies", dict(self.client.http.client.cookies), update_dict=True)
def post(self, message: str):

View File

@@ -5,7 +5,7 @@ from pathlib import Path
import requests
from ics import Calendar, Event
from ..utils import config, error, prompt, ICS_FILE
from ..utils import config, error, prompt, ICS_FILE, date
from ..feeds import *
@@ -26,15 +26,13 @@ class Routine:
@staticmethod
def init_feed(feed: Type[Feed]) -> Feed:
feed_config = config.get_field(feed.__config_name__, {})
feed_config = config.get_field(f"feed.{feed.__config_name__}", {})
try:
feed_instance = feed(**feed_config)
return feed(**feed_config)
except (TypeError, error.InvalidCredential) as e:
print(feed_config)
raise e
config.set_field(feed.__name__, feed.prompt_auth(), update_dict=True)
feed_config = config.get_field(feed.__config_name__, {})
config.set_field(f"feed.{feed.__config_name__}", {feed.prompt_auth()}, update_dict=True)
feed_config = config.get_field(f"feed.{feed.__config_name__}", {})
try:
return feed(**feed_config)
@@ -69,8 +67,19 @@ class Routine:
@staticmethod
def get_event_string(event: Event) -> str:
return f""
event_config = config.get_field("messages", {}).get(event.name, {})
if not len(event_config):
return ""
message = event_config.get("message", "")
data = date.event_formatting_values(event, "de")
for key, value in data.items():
message = message.replace(f"{{{key}}}", value)
return message.strip()
def run(self):
pass

View File

@@ -2,18 +2,15 @@ from . import Routine
from ..feeds import Feed, TwitterFeed
from ..utils import date
from datetime import datetime
from ics import Calendar
class Development(Routine):
def test_feeds(self):
for feed in self.iter_feeds():
feed_instance = self.init_feed(feed)
feed_instance.post(f"Hello World!!! ({datetime.now()})")
def run(self):
calendar = self.get_ics()
for event in calendar.events:
print(event.name, date.event_formatting_values(event, "de"))
break
event_description = f"""
Wir treffen uns {event.begin.humanize(locale="de")} zum {event.name}."""
print(event_description.strip())
print()
self.test_feeds()

View File

@@ -7,16 +7,26 @@ from . import PROGRAM_DATA_DIR, PROGRAM_NAME
logger = logging.getLogger("config")
_config: dict = {
"ics_url": "",
"locale": "en",
"date_format": "%Y-%m-%d %H:%M:%S",
"time_format": "%H:%M",
"time_format_full_hour": "%H",
"weekday_translations": {},
"months_translations": {},
"active_feeds": [
"mastodon",
"twitter",
],
"weekday_translations": {},
"months_translations": {},
"ics_url": "",
"mastodon": {},
"twitter": {},
"lemmy": {},
"feed": {
"mastodon": {},
"twitter": {},
"lemmy": {},
},
}
@@ -41,16 +51,33 @@ def write():
def get_field(__name: str, default=None):
global _config
return _config.get(__name, default)
_parts = __name.split(".")
_field_container = _config
for _part in _parts[:-1]:
_field_container = _field_container.get(_part, {})
return _field_container.get(_parts[-1], default)
def set_field(__name: str, __value, update_dict=False):
global _config
_parts = __name.split(".")
_field_container = _config
for _part in _parts[:-1]:
if _part not in _field_container:
_field_container[_part] = {}
_field_container = _field_container[_part]
if update_dict and isinstance(__value, dict) and isinstance(_config.get(__name), dict):
_config[__name].update(__value)
_field_container[_parts[-1]].update(__value)
else:
_config[__name] = __value
_field_container[_parts[-1]] = __value
write()