Ajout d'un fichier de configuration

Signed-off-by: ynerant <ynerant@zamokv5.crans.org>
This commit is contained in:
ynerant 2021-05-11 14:26:33 +02:00
parent 5e6834c749
commit 263da6e5eb
3 changed files with 82 additions and 16 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
__pycache__ __pycache__
config.yml

View File

@ -3,13 +3,50 @@
from dataclasses import dataclass from dataclasses import dataclass
from datetime import date from datetime import date
from math import acos, cos, pi, sin from math import acos, cos, pi, sin
import os
import requests import requests
import sys
from threading import Thread from threading import Thread
from time import sleep from time import sleep
import yaml
from irc import IRCClient from irc import IRCClient
@dataclass
class Config:
@dataclass
class IRCConfig:
nickname: str = ""
host: str = ""
channel: str = ""
@dataclass
class SearchConfig:
position: "Location" = None
radius: float = 0
departements: list[int] = None
mentions: list[str] = None
irc: "IRCConfig" = None
search: list["SearchConfig"] = None
def __init__(self, irc: "IRCConfig", search: list["SearchConfig"]):
if isinstance(irc, dict):
irc = Config.IRCConfig(**irc)
if not search:
search = []
search = [Config.SearchConfig(**sc) if isinstance(sc, dict) else sc for sc in search]
for s in search:
if isinstance(s.position, dict):
s.position = Location(**s.position)
self.irc = irc
self.search = search
@dataclass @dataclass
class Location: class Location:
longitude: float = 0.0 longitude: float = 0.0
@ -87,36 +124,41 @@ def check_dpt(dpt_number: int, position: Location, radius: int = 20):
def main(): def main():
gif = Location(latitude=48.7090418, longitude=2.1648068, city="Gif-sur-Yvette") if not os.path.isfile('config.yml'):
lyon = Location(latitude=45.7579502, longitude=4.8001017, city="Lyon") print("Le fichier de configuration n'existe pas. "
chambéry = Location(latitude=45.5822142, longitude=5.8713341, city="Chambéry") "Commencez par copier l'exemple depuis config.yml.example.", file=sys.stderr)
nantes = Location(latitude=47.2382007, longitude=-1.6300954, city="Nantes") exit(1)
marseille = Location(latitude=43.2803692, longitude=5.3104571, city="Marseille")
irc_client = IRCClient('irc.crans.org', 'chronodose') # Chargement de la configuration
with open('config.yml') as f:
config = yaml.safe_load(f)
config = Config(**config)
irc_client = IRCClient(config.irc.host, config.irc.nickname)
Thread(target=irc_client.start).start() Thread(target=irc_client.start).start()
# Connexion à IRC
sleep(10) sleep(10)
irc_client.join('#chronodose') irc_client.join(config.irc.channel)
irc_client.privmsg('#chronodose', 'coucou') irc_client.privmsg(config.irc.channel, 'coucou')
already_indicated = [] already_indicated = []
def msg(*mesg: str) -> None: def msg(*mesg: str) -> None:
# Afficher un message dans la console et sur IRC # Afficher un message dans la console et sur IRC
print(*mesg) print(*mesg)
irc_client.privmsg('#chronodose', ' '.join(str(a) for a in mesg)) irc_client.privmsg(config.irc.channel, ' '.join(str(a) for a in mesg))
while True: while True:
places = {} for search in config.search:
for dpt, ville in [(91, gif), (92, gif), (94, gif), (78, gif), (69, lyon), places = []
(73, chambéry), (44, nantes), (13, marseille)]: for dpt in search.departements:
places[dpt] = check_dpt(dpt, ville) places.extend(check_dpt(dpt, search.position, search.radius))
for dpt, places in places.items():
if not places: if not places:
print("Pas de dose disponible dans le", dpt) print("Pas de place disponible autour de", search.position.city)
continue continue
print(sum(place[1] for place in places), "doses disponibles dans le", dpt)
print(sum(place[1] for place in places), "doses disponibles autour de", search.position.city)
for centre, count in places: for centre, count in places:
if (centre.internal_id, date.today()) in already_indicated: if (centre.internal_id, date.today()) in already_indicated:
# Message déjà envoyé, on spam pas # Message déjà envoyé, on spam pas
@ -127,6 +169,7 @@ def main():
msg("Type de vaccin :", ", ".join(centre.vaccine_type)) msg("Type de vaccin :", ", ".join(centre.vaccine_type))
msg(centre.metadata.address, centre.metadata.phone_number) msg(centre.metadata.address, centre.metadata.phone_number)
msg("Réserver sur", centre.url) msg("Réserver sur", centre.url)
msg(*search.mentions)
msg(" ") msg(" ")
# 5 minutes # 5 minutes

22
config.yml.example Normal file
View File

@ -0,0 +1,22 @@
---
irc:
nickname: chronodose
host: irc.crans.org
channel: '#chronodose'
search:
- position:
longitude: 2.2770207
latitude: 48.8588377
city: Paris
radius: 15
departements:
- 75
- 77
- 78
- 92
- 93
- 94
- 95
mentions:
- toto