Created
April 10, 2022 17:41
-
-
Save ejetzer/de4caec65979591bf41a39122cc71794 to your computer and use it in GitHub Desktop.
Un petit programme qui va chercher les statistiques sur le nombre de nouveaux tests positifs à certaines maladies infectieuses sur le site de l'INSPQ, et les garde en mémoire dans un petit fichier.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.10 | |
# -*- coding: utf-8 -*- | |
"""Obtenir les statistiques à jour sur les maladies infectieuses.""" | |
import requests | |
import logging | |
import pathlib | |
import time | |
import datetime as dt | |
from html.parser import HTMLParser | |
import pandas as pd | |
import schedule | |
ADRESSE = 'https://www.inspq.qc.ca/influenza' | |
SORTIE = pathlib.Path('~/Documents/Personnel/Santé/INSPQ Maladies infectieuses.csv').expanduser() | |
class InfectiousParser(HTMLParser): | |
def __init__(self): | |
super().__init__() | |
self.dans_litem = False | |
self.statistiques = {} | |
self.catégories = ('influenza\xa0A', 'influenza\xa0B', 'para-influenza\xa01', | |
'para-influenza\xa02', 'para-influenza\xa03', | |
'para-influenza\xa04', 'virus respiratoire syncytial', | |
'adénovirus', 'métapneumo virus humain', | |
'coronavirus commun') | |
def handle_starttag(self, tag: str, attrs: list[tuple[str, str]]): | |
if tag == 'li': | |
self.dans_litem = True | |
def handle_data(self, data: str): | |
if self.dans_litem: | |
catégorie = list(filter(lambda c: c in data, self.catégories)) | |
if ( len(catégorie) > 0 ) and ( '\xa0' in data ): | |
catégorie = catégorie.pop() | |
nombre = data.split('\xa0', 1).pop(0) # Espace insécable | |
if nombre.isdigit(): | |
nombre = int(nombre) | |
self.statistiques[catégorie] = nombre | |
def handle_endtag(self, tag: str): | |
if self.dans_litem and tag == 'li': | |
self.dans_litem = False | |
def main(): | |
requête = requests.get(ADRESSE) | |
contenu = requête.text | |
parseur = InfectiousParser() | |
parseur.feed(contenu) | |
parseur.close() | |
nouveau = pd.DataFrame(parseur.statistiques, index=[dt.date.today()]) | |
if SORTIE.exists(): | |
df = pd.read_csv(SORTIE, | |
index_col=0, | |
parse_dates=True, | |
date_parser=dt.date.fromisoformat) | |
df.loc[pd.Timestamp(dt.date.today()), :] = nouveau.iloc[0, :] | |
else: | |
df = nouveau | |
df.to_csv(SORTIE) | |
if __name__ == '__main__': | |
logging.basicConfig(level=logging.INFO) | |
main() | |
schedule.every().sunday.at('10:00').do(main) | |
while True: | |
schedule.run_pending() | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment