Last active
March 29, 2020 08:00
-
-
Save lcheylus/a81ceb9406f3a8abc9afc5fb387a7f70 to your computer and use it in GitHub Desktop.
Get Covid-19 datas (total cases, deaths) for France by date
This file contains hidden or 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 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| Get datas for Covid-19 in France (2020). | |
| Datas extracted from https://github.com/opencovid19-fr/ (JSON format) | |
| Python script by Laurent Cheylus ([email protected]) | |
| """ | |
| from argparse import ArgumentParser | |
| import re | |
| import sys | |
| import json | |
| import requests | |
| # Datas aggregated from opencovid19-fr on GitHub | |
| URL = ( | |
| 'https://github.com/opencovid19-fr/data/raw/master/dist/chiffres-cles.json' | |
| ) | |
| def get_datas(date, b_cumul): | |
| """ | |
| Get datas for Covid-19 in France. | |
| Args | |
| date -- date (string as YYYY-MM-DD) to extract datas | |
| b_cumul -- bool to get only cumulative stats | |
| Returns | |
| datas -- list of dicts for extracted datas | |
| cumul -- dict for cumulative datas (total cases, deaths) | |
| """ | |
| r = requests.get(URL) | |
| if r.status_code == 200: | |
| datas = json.loads(r.text) | |
| else: | |
| print("ERROR: Unable to get JSON datas") | |
| sys.exit(0) | |
| dom_tom = ['Guadeloupe', 'Guyane', 'La Réunion', 'Martinique', 'Mayotte'] | |
| fields = ['nom', 'casConfirmes', 'sourceType', 'code'] | |
| extract_datas = list() | |
| for data in datas: | |
| if data['date'] == date: | |
| if data['code'] == 'WORLD': | |
| continue | |
| if ( | |
| data['code'] == 'FRA' | |
| and data['sourceType'] == 'ministere-sante' | |
| ): | |
| cumul_france = data | |
| if b_cumul: | |
| return None, cumul_france | |
| else: | |
| continue | |
| if 'casConfirmes' in data: | |
| extract_data = dict() | |
| for field in fields: | |
| extract_data[field] = data[field] | |
| if data['code'].startswith('DEP'): | |
| extract_data['type'] = 0 | |
| elif data['code'].startswith('COM'): | |
| extract_data['type'] = 1 | |
| elif data['code'].startswith('REG'): | |
| if data['nom'] in dom_tom: | |
| extract_data['type'] = 1 | |
| else: | |
| extract_data['type'] = 2 | |
| extract_datas.append(extract_data) | |
| try: | |
| cumul_france | |
| except NameError: | |
| print("Unable to find datas for date '{0}'".format(date)) | |
| sys.exit(0) | |
| return (extract_datas, cumul_france) | |
| def dump_datas(datas, cumul, b_cumul): | |
| """Display datas. | |
| Args | |
| datas -- list of dicts for extracted datas | |
| cumul -- dict for cumulative datas (total cases, deaths) | |
| b_cumul -- bool to display only cumulative datas | |
| """ | |
| # Cumul France | |
| if 'deces' not in cumul: | |
| cumul['deces'] = 0 | |
| print( | |
| "CUMUL France - Cas confirmés: {0} - Decès: {1}".format( | |
| cumul['casConfirmes'], cumul['deces'] | |
| ) | |
| ) | |
| if b_cumul: | |
| return | |
| print("---------------") | |
| # Datas by department | |
| dep_datas = [ | |
| data for data in datas if data['code'] != 'FRA' and data['type'] == 0 | |
| ] | |
| print("\nPar département") | |
| print("---------------") | |
| for data in sorted(dep_datas, key=lambda k: k['nom']): | |
| print( | |
| "{0} ({1} - {2}) - Cas confirmés: {3}".format( | |
| data['nom'], | |
| data['code'], | |
| data['sourceType'], | |
| data['casConfirmes'], | |
| ) | |
| ) | |
| # Datas by DOM-TOM | |
| dom_datas = [ | |
| data for data in datas if data['code'] != 'FRA' and data['type'] == 1 | |
| ] | |
| print("\nPar DOM-TOM") | |
| print("-----------") | |
| for data in sorted(dom_datas, key=lambda k: k['nom']): | |
| print( | |
| "{0} ({1} - {2}) - Cas confirmés: {3}".format( | |
| data['nom'], | |
| data['code'], | |
| data['sourceType'], | |
| data['casConfirmes'], | |
| ) | |
| ) | |
| # Datas by region | |
| reg_datas = [ | |
| data for data in datas if data['code'] != 'FRA' and data['type'] == 2 | |
| ] | |
| print("\nPar région") | |
| print("----------") | |
| for data in sorted(reg_datas, key=lambda k: k['nom']): | |
| print( | |
| "{0} ({1} - {2}) - Cas confirmés: {3}".format( | |
| data['nom'], | |
| data['code'], | |
| data['sourceType'], | |
| data['casConfirmes'], | |
| ) | |
| ) | |
| def parse_args(): | |
| """Parse arguments.""" | |
| parser = ArgumentParser( | |
| description='Get Covid-19 datas (total cases, deaths) for France' | |
| ) | |
| parser.add_argument( | |
| 'date', help="Date to extract datas (YYYY-MM-DD format)" | |
| ) | |
| parser.add_argument( | |
| '-c', | |
| '--cumul', | |
| help='Get informations only for cumulated stats.', | |
| action='store_true', | |
| ) | |
| return parser.parse_args() | |
| if __name__ == "__main__": | |
| args = parse_args() | |
| pattern = re.compile(r'\d{4}-\d{2}-\d{2}$') | |
| if not pattern.match(args.date): | |
| print("Invalid date '{0}' (format YYYY-MM-DD)".format(args.date)) | |
| sys.exit(0) | |
| datas, cumul = get_datas(args.date, args.cumul) | |
| dump_datas(datas, cumul, args.cumul) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment