Purge_Registres_D-c-s_INSEE/purge-registres-deces-insee/site_eglise.py

100 lines
3.5 KiB
Python

"""
Copyright (c) 2020 Sdj Geek
Voir le fichier LICENSE
Classe d'accès aux données du site de l'Église
"""
import requests
import browser_cookie3
from membre_base import MembreBase, MembreProvider
class SiteEglise(MembreProvider):
def __init__(self, unite, cookie_path=None):
"""
:param unite: numéro de l'unité (paroisse, branche)
:param cookie_path: chemin vers le répertoire où inscrire les fichiers de sortie
"""
self.unite = unite
self.cookie_jar = browser_cookie3.firefox(cookie_file=cookie_path)
self.as_json = None
def __len__(self):
return len(self.as_json)
class Membre(MembreBase):
def __init__(self, provider, data):
super().__init__(provider)
self.completed = False
# Données fournies
self.r_id = data['legacyCmisId']
self.r_last_name, self.r_first_name = data['nameListPreferredLocal'].upper().split(',')
self.r_last_name = self.r_last_name.strip().split(' ')[0]
self.r_first_name = self.r_first_name.strip().split(' ')[0]
self.r_annee, self.r_mois, self.r_jour = data['birth']['date']['date'].split('-')
self.r_sexe = data['sex']
# Rechercher le nom de jeune fille si besoin
if data['isSpouse']:
self.complete()
def complete(self):
if not self.completed:
member_profile = self.provider.get_member_profile(self.r_id)
self.r_maiden_name = member_profile['individual']['maidenNameGroup']['name1']['family']
if self.r_maiden_name is not None:
self.r_maiden_name = self.r_maiden_name.strip().split(' ')[0].upper()
self.r_ville = member_profile['individual']['birthPlace']
if not self.r_ville:
self.r_ville = "<VIDE>"
self.completed = True
def set_insee(self, insee):
self.complete()
super().set_insee(insee)
def get_name(self):
return f"Unité_{self.unite}"
def load(self):
r = requests.get('https://lcr.churchofjesuschrist.org/services/umlu/report/member-list',
params={'lang': "fra", 'unitNumber': self.unite},
headers={'Accept': "application/json"},
cookies=self.cookie_jar)
r.raise_for_status()
self.as_json = r.json()
return len(self.as_json)
def get_member_list(self):
"""Recevoir la liste des membres
:returns: la liste des membres sous forme d'objet JSON
"""
for member in self.as_json:
try:
yield self.Membre(self, member)
except ValueError:
print(f"Error with member [{member['nameListPreferredLocal']}, {member['birth']['date']['date']}]")
continue
def get_member_profile(self, member_id):
"""Recevoir les informations sur un membre
:param member_id: ID du membre
:returns: les informations sur le membre sous forme d'objet JSON
"""
r = requests.get(f'https://lcr.churchofjesuschrist.org/records/member-profile/service/{member_id}',
params={'lang': "fra"},
headers={'Accept': "application/json",
'Referer': f"https://lcr.churchofjesuschrist.org/records/member-profile/{member_id}?lang=fra"},
cookies=self.cookie_jar)
r.raise_for_status()
return r.json()