-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
69 lines (50 loc) · 1.76 KB
/
utils.py
File metadata and controls
69 lines (50 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from datetime import datetime
from pathlib import Path
import apprise
import yaml
CATEGORY_URLS = {
"101": "cinemas",
"102": "spectacles",
"103": "musees-expos-monuments",
"104": "parcs-et-zoos",
"105": "sports",
}
class Config:
_data: dict = {}
@classmethod
def load(cls, required_keys: list[str]):
file = Path(__file__).with_name("env.yaml")
if not file.exists():
raise FileNotFoundError("Missing env.yaml")
with file.open("r", encoding="utf-8") as f:
cls._data = yaml.safe_load(f) or {}
missing = [k for k in required_keys if k not in cls._data]
if missing:
raise KeyError(f"Missing keys: {missing}")
@classmethod
def get(cls, key: str, default=None):
return cls._data.get(key, default)
class Cache:
def __init__(self, file: Path):
self._file = file
self._items: set[str] = set()
if file.exists():
with file.open("r") as f:
self._items = {line.strip() for line in f if line.strip()}
def hit(self, item: str) -> bool:
return item in self._items
def add(self, item: str):
if item not in self._items:
self._items.add(item)
with self._file.open("w") as f:
f.write("\n".join(sorted(self._items)))
class Notifier:
def __init__(self, urls: list[str]):
self._app = apprise.Apprise()
[self._app.add(url) for url in urls]
async def send(self, message: str):
self._app.notify(body=message)
def get_category_url(category_id: str) -> str:
return CATEGORY_URLS.get(str(category_id), "/spectacles")
def to_bon_francais_date(date_str: str) -> str:
return datetime.strptime(date_str, "%Y-%m-%d").strftime("%d/%m/%Y")