|
|
|
@ -1,11 +1,14 @@
|
|
|
|
|
import datetime
|
|
|
|
|
import os
|
|
|
|
|
import os.path
|
|
|
|
|
import tempfile
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from icalendar import Calendar
|
|
|
|
|
from webdav3.client import Client as WDClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CACHE_DIR = "~/.cache/smallprint"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Client:
|
|
|
|
|
def __init__(self, config):
|
|
|
|
|
"""
|
|
|
|
@ -19,6 +22,13 @@ class Client:
|
|
|
|
|
self.calendar_path = config["CALENDAR_PATH"]
|
|
|
|
|
self.client = WDClient(options)
|
|
|
|
|
|
|
|
|
|
cache_path = Path(CACHE_DIR).expanduser() # Generate the full path
|
|
|
|
|
if not cache_path.is_dir():
|
|
|
|
|
# Create the cache dir
|
|
|
|
|
os.mkdir(cache_path)
|
|
|
|
|
|
|
|
|
|
self.cache_path = cache_path
|
|
|
|
|
|
|
|
|
|
def retrieve_calendars_list(self):
|
|
|
|
|
"""
|
|
|
|
|
Retrieves the calendar list
|
|
|
|
@ -30,15 +40,20 @@ class Client:
|
|
|
|
|
"""
|
|
|
|
|
Given a filename, tries to read the calendar event
|
|
|
|
|
"""
|
|
|
|
|
f, temp_path = tempfile.mkstemp()
|
|
|
|
|
rpath = os.path.join(self.calendar_path, pathname)
|
|
|
|
|
self.client.download_sync(remote_path=rpath,
|
|
|
|
|
local_path=temp_path)
|
|
|
|
|
# Read the temporary data, close the file, remove it
|
|
|
|
|
fics = open(temp_path, 'r')
|
|
|
|
|
data = fics.read()
|
|
|
|
|
fics.close()
|
|
|
|
|
os.remove(temp_path)
|
|
|
|
|
cache_file = os.path.join(self.cache_path, pathname)
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(cache_file):
|
|
|
|
|
print(f"Downloading {pathname}")
|
|
|
|
|
# File not in cache, download
|
|
|
|
|
rpath = os.path.join(self.calendar_path, pathname)
|
|
|
|
|
self.client.download_sync(remote_path=rpath,
|
|
|
|
|
local_path=cache_file)
|
|
|
|
|
|
|
|
|
|
# Read the downloaded or cached data
|
|
|
|
|
with open(cache_file, 'r') as fics:
|
|
|
|
|
fics = open(cache_file, 'r')
|
|
|
|
|
data = fics.read()
|
|
|
|
|
|
|
|
|
|
# Now with the data read, we can process it
|
|
|
|
|
return self.parse_calendar(data)
|
|
|
|
|
|
|
|
|
|