montrouge/montrouge.py

62 lines
2.0 KiB
Python

# coding: utf-8
import json
import sys
import bs4 as BeautifulSoup
import requests
data = {
"comitesDeQuartier": {
"Ferry-Buffalo": [],
"Jean Jaurès": [],
"Plein Sud": [],
"Portes de Montrouge": [],
"Montrouge Est": [],
"Vieux Montrouge": [],
},
"conseilsMunicipaux": []
}
# Get Conseils Municipaux
r = requests.get(
"https://www.ville-montrouge.fr/621-les-seances-publiques-et-deliberations.htm"
)
soup = BeautifulSoup.BeautifulSoup(r.text, features='lxml')
main= soup.find('main', attrs={"id": "corps"})
table = main.find('table')
a = table.findAll('a')
data["conseilsMunicipaux"] = ["https://www.ville-montrouge.fr" + x.attrs['href'] for x in a]
# Handle Comités de Quartier
URLS = {
"Ferry-Buffalo": "https://www.ville-montrouge.fr/688-comite-ferry-buffalo.htm",
"Jean Jaurès": "https://www.ville-montrouge.fr/689-comite-jean-jaures.htm",
"Plein Sud": "https://www.ville-montrouge.fr/692-comite-plein-sud.htm",
"Portes de Montrouge": "https://www.ville-montrouge.fr/690-comite-portes-de-montrouge.htm",
"Montrouge Est": "https://www.ville-montrouge.fr/691-comite-montrouge-est.htm",
"Vieux Montrouge": "https://www.ville-montrouge.fr/693-comite-vieux-montrouge.htm",
}
for (k, url) in URLS.items():
r = requests.get(url)
soup = BeautifulSoup.BeautifulSoup(r.text, features='lxml')
div = soup.find('div', attrs={"class": "enSavoirPlusParagraphe PRS_ENCADRE txt"})
if div:
a = div.findAll('a')
data["comitesDeQuartier"][k] = ["https://www.ville-montrouge.fr" + x.attrs['href'] for x in a]
dump = json.dumps(data, sort_keys=True,
indent=4, separators=(',', ': '))
try:
with open('lastrun.json', 'r') as fh:
lastrun = fh.read()
# If something changed, print the fetched data and exit 1 to ensure chronic
# sends an email.
if lastrun != dump:
print(dump)
sys.exit(1)
except IOError:
pass
with open('lastrun.json', 'w') as fh:
fh.write(dump)