fetching and reading calendar

This commit is contained in:
Hellow
2024-02-13 22:29:35 +01:00
parent d19406d3b4
commit 420deb16cf
5 changed files with 41 additions and 7 deletions

View File

@@ -1,6 +1,11 @@
from typing import Generator, Type, Dict
import os.path
from pathlib import Path
from ..utils import config, error
import requests
from ics import Calendar
from ..utils import config, error, prompt, ICS_FILE
from ..feeds import *
@@ -36,6 +41,32 @@ class Routine:
except (TypeError, error.InvalidCredential):
raise error.InvalidCredential(f"Invalid credentials for {feed.__name__}.")
@staticmethod
def get_ics(use_cached: bool = False) -> Path:
if use_cached and ICS_FILE.exists():
with ICS_FILE.open("r") as f:
return Calendar(f.read())
ics_url = config.get_field("ics_url")
if not len(ics_url):
ics_url = prompt.for_string("url or path to the ICS file")
config.set_field("ics_url", ics_url)
origin_path = Path(ics_url)
if origin_path.is_file():
with origin_path.open("rb") as f:
with ICS_FILE.open("wb") as t:
t.write(f.read())
else:
r = requests.get(ics_url)
with ICS_FILE.open("wb") as t:
t.write(r.content)
with ICS_FILE.open("r") as f:
return Calendar(f.read())
def run(self):
pass

View File

@@ -4,7 +4,4 @@ from ..feeds import Feed
class Development(Routine):
def run(self):
for feed_class in self.iter_feeds():
feed: Feed = self.init_feed(feed_class)
feed.post(message="This worked second try!")
print(self.get_ics())