Example #1
0
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}")
Example #2
0
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}")
Example #3
0
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}")
Example #4
0
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}")
Example #5
0
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}")
Example #6
0
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}")
Example #7
0
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}")
Example #8
0
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}")
Example #9
0
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}")
Example #10
0
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}")
Example #11
0
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}")