def proportion_daily_country(data_type, country): try: data = util.read_json(f"csv_{data_type}.json") ret = {"proportion-daily": {}} for region in list(data.keys()): if util.pattern_match(country, region, data[region]["iso2"], data[region]["iso3"]): if data[region]["iso3"] in util.populations: pop = float(util.populations[data[region]["iso3"]]) else: util.populations = util.csv_to_dict(util.CSV_POPULATIONS) pop = float(util.populations[data[region]["iso3"]]) prev = 0 for d, h in data[region]["history"].items(): ret["proportion-daily"][ d] = f"{round((h - prev) / pop * 100, 10):.10f}" prev = h ret["iso2"] = data[region]["iso2"] ret["iso3"] = data[region]["iso3"] ret["name"] = region return jsonify(ret) raise CountryNotFound("This region cannot be found. Please try again.") except CountryNotFound as e: return util.response_error(message=f"{type(e).__name__} : {e}", status=404) except Exception as e: return util.response_error(message=f"{type(e).__name__} : {e}")
def all_country(country): try: data = util.read_json("data.json") for region in data: if util.pattern_match(country, region["country"], region["iso2"], region["iso3"]): return jsonify(region) raise CountryNotFound("This region cannot be found. Please try again.") except CountryNotFound as e: return util.response_error(message=f"{type(e).__name__} : {e}", status=404) except Exception as e: return util.response_error(message=f"{type(e).__name__} : {e}")
def history_country(data_type, country): try: data = util.read_json(f"csv_{data_type}.json") for region in list(data.keys()): if util.pattern_match(country, region, data[region]["iso2"], data[region]["iso3"]): ret = data[region] ret["name"] = region return jsonify(ret) raise CountryNotFound("This region cannot be found. Please try again.") except CountryNotFound as e: return util.response_error(message=f"{type(e).__name__} : {e}", status=404) except Exception as e: return util.response_error(message=f"{type(e).__name__} : {e}")
def proportion_daily(data_type): try: data = util.read_json(f"csv_{data_type}.json") for region in list(data.keys()): ret = {"proportion-daily": {}} if data[region]["iso3"] == "": # TODO: Note, some regions do not have iso2/3 codes.... data[region] = { "proportion-daily": "This region doesn't work with this function atm" } continue if data[region]["iso3"] in util.populations: pop = float(util.populations[data[region]["iso3"]]) else: util.populations = util.csv_to_dict(util.CSV_POPULATIONS) pop = float(util.populations[data[region]["iso3"]]) prev = 0 for d, h in data[region]["history"].items(): ret["proportion-daily"][ d] = f"{round((h - prev) / pop * 100, 10):.10f}" prev = int(h) ret["iso2"] = data[region]["iso2"] ret["iso3"] = data[region]["iso3"] data[region] = ret return jsonify(data) except Exception as e: return util.response_error(message=f"{type(e).__name__} : {e}")
def history_region_all(data_type, country): try: if country.lower() in ("us", "united states", "usa"): data = util.read_json(f"csv_{data_type}_us_region.json") else: data = util.read_json(f"csv_{data_type}_region.json") for inner_country in list(data.keys()): if util.pattern_match(country, inner_country, data[inner_country]["iso2"], data[inner_country]["iso3"]): return jsonify(data[inner_country]["regions"]) raise CountryNotFound( "This country cannot be found. Please try again.") except CountryNotFound as e: return util.response_error(message=f"{type(e).__name__} : {e}", status=404) except Exception as e: return util.response_error(message=f"{type(e).__name__} : {e}")
def history_region_world(data_type): try: data = util.read_json(f"csv_{data_type}.json") ret = {"history": {}} for d in data.keys(): for h in data[d]["history"].keys(): if h not in ret["history"]: ret["history"][h] = int(data[d]["history"][h]) else: ret["history"][h] += int(data[d]["history"][h]) return jsonify(ret) except Exception as e: return util.response_error(message=f"{type(e).__name__} : {e}")
def proportion_region_world(data_type): try: data = util.read_json(f"csv_{data_type}.json") ret = {"proportion": {}} for d in data.keys(): for h in data[d]["history"].keys(): if h not in ret["proportion"]: ret["proportion"][h] = int(data[d]["history"][h]) else: ret["proportion"][h] += int(data[d]["history"][h]) for h in ret["proportion"]: ret["proportion"][ h] = f"{round(int(ret['proportion'][h]) / int(util.WORLD_POPULATION) * 100, 5):.5f}" return jsonify(ret) except Exception as e: return util.response_error(message=f"{type(e).__name__} : {e}")
def daily(data_type): try: data = util.read_json(f"csv_{data_type}.json") for region in list(data.keys()): ret = {"daily": {}} prev = 0 for d, h in data[region]["history"].items(): ret["daily"][d] = h - prev prev = int(h) ret["iso2"] = data[region]["iso2"] ret["iso3"] = data[region]["iso3"] data[region] = ret return jsonify(data) except Exception as e: return util.response_error(message=f"{type(e).__name__} : {e}")
def proportion_daily_region_world(data_type): try: data = util.read_json(f"csv_{data_type}.json") ret = {"proportion-daily": {}} for d in data.keys(): for h in data[d]["history"].keys(): if h not in ret["proportion-daily"]: ret["proportion-daily"][h] = int(data[d]["history"][h]) else: ret["proportion-daily"][h] += int(data[d]["history"][h]) prev = 0 for d, h in ret["proportion-daily"].items(): ret["proportion-daily"][ d] = f"{round((h - prev) / int(util.WORLD_POPULATION) * 100, 10):.10f}" prev = int(h) return jsonify(ret) except Exception as e: return util.response_error(message=f"{type(e).__name__} : {e}")
def all_data(): try: data = util.read_json("data.json") return jsonify(data) except Exception as e: return util.response_error(message=f"{type(e).__name__} : {e}")
def history(data_type): try: data = util.read_json(f"csv_{data_type}.json") return jsonify(data) except Exception as e: return util.response_error(message=f"{type(e).__name__} : {e}")