Esempio n. 1
0
def get_data_country_by_category_by_province(category, country_code):
    """
    Get all the data of a country by category. There is three category (confirmed | death | recovered)
    """
    data = request.get_data_time_series(category)
    # The normalized locations.
    locations = []

    for item in data:
        if country_code.upper() == countrycodes.country_code(
                item['Country/Region']):
            # Filter out all the dates.
            history = dict(
                filter(lambda element: date_util.is_date(element[0]),
                       item.items()))
            # Sorted date history
            history = sorted_history_date(formated_date(history))
            # Country for this location.
            country = item['Country/Region']
            # Latest data insert value.
            latest = list(history.values())[-1]
            # Normalize the item and append to locations.
            locations.append({
                # General info.
                'country': country,
                'country_code': countrycodes.country_code(country),
                'province': item['Province/State'],
                # History.
                'history': history,
                # Latest statistic.
                'total': int(latest or 0),
            })

    # Check if the country without province exist
    data_country_all_province = []

    # By country and province
    for country in locations:
        if country['country_code'] == country_code.upper(
        ) and country['province'] == '':
            return {
                'data': country['history'],
                'country': country['country'],
                'country_code': country['country_code'],
                'province': country['province'],
                'last_updated': ''
            }
    # Otherwise regrouped all provinces of the country
    for country in locations:
        if country['country_code'] == country_code.upper():
            data_country_all_province.append(country['history'])

    return {
        'data': data_country_by_province(data_country_all_province),
        'country': country['country'],
        'country_code': country['country_code'],
        'province': '',
        'last_updated': ''
    }
Esempio n. 2
0
def get_all_data():
    """
    Get all the data information (death, confirmed, recovered,) of each country.
    """
    data = request.get_data_daily_reports()
    locations = []

    for item in data:
        locations.append({
            # General info.
            'country': item['Country_Region'],
            'country_code': countrycodes.country_code(item['Country_Region']),
            'province': item['Province_State'],
            'total': {
                'confirmed': item['Confirmed'],
                'deaths': item['Deaths'],
                'recovered': item['Recovered'],
                'active': item['Active'],
                'Incident_Rate': item['Incident_Rate'],
                'Case_Fatality_Ratio': item['Case_Fatality_Ratio']
            }
        })

    return {
        'locations': locations,
        'last_updated': datetime.utcnow().isoformat() + 'Z'
    }
Esempio n. 3
0
def get_all_data_grouped_by_country():
    """
    Get all the data information (death, confirmed, recovered,) grouped by country (Many country have many provinces).
    """
    data = request.get_data_daily_reports()
    locations = []
    country_added = {}
    index = 0
    total_confirmed = 0
    total_deaths = 0
    total_recovered = 0

    for item in data:
        # country/province exist
        country_code = countrycodes.country_code(item['Country_Region'])

        if country_code not in country_added or country_code == 'XX':
            locations.append({
                'country': item['Country_Region'],
                'country_code': country_code,
                'total': {
                    'confirmed':
                    int(item['Confirmed']),
                    'deaths':
                    int(item['Deaths']),
                    'recovered':
                    int(item['Recovered']),
                    'active':
                    ('0' if item['Active'] == '' else int(item['Active']))
                }
            })
            country_added[country_code] = index
            index += 1
        else:
            locations[
                country_added[country_code]]['total']['confirmed'] += int(
                    item['Confirmed'])
            locations[
                country_added[country_code]]['total']['recovered'] += int(
                    item['Recovered'])
            locations[country_added[country_code]]['total']['active'] += (
                0 if item['Active'] == '' else int(item['Active']))
            locations[country_added[country_code]]['total']['deaths'] += int(
                item['Deaths'])

        total_confirmed += int(item['Confirmed'])
        total_deaths += int(item['Deaths'])
        total_recovered += int(item['Recovered'])

    return {
        'locations': locations,
        'latest': {
            'confirmed': total_confirmed,
            'deaths': total_deaths,
            'recovered': total_recovered,
            'active': total_confirmed - total_deaths - total_recovered,
        },
        'last_updated': datetime.utcnow().isoformat() + 'Z'
    }
Esempio n. 4
0
def get_data(category):
    """
    Retrieves the data for the provided type. The data is cached for 1 hour.
    """

    # Adhere to category naming standard.
    category = category.lower().capitalize();

    # Request the data
    request = requests.get(base_url % category)
    text    = request.text

    # Parse the CSV.
    data = list(csv.DictReader(text.splitlines()))

    # The normalized locations.
    locations = []

    for item in data:
        # Filter out all the dates.
        history = dict(filter(lambda element: date_util.is_date(element[0]), item.items()))

        # Country for this location.
        country = item['Country/Region']

        # Latest data insert value.
        latest = list(history.values())[-1];

        # Normalize the item and append to locations.
        locations.append({
            # General info.
            'country':  country,
            'country_code': countrycodes.country_code(country),
            'province': item['Province/State'],

            # Coordinates.
            'coordinates': {
                'lat':  item['Lat'],
                'long': item['Long'],
            },

            # History.
            'history': history,

            # Latest statistic.
            'latest': int(latest or 0),
        })

    # Latest total.
    latest = sum(map(lambda location: location['latest'], locations))

    # Return the final data.
    return {
        'locations': locations,
        'latest': latest,
        'last_updated': datetime.utcnow().isoformat() + 'Z',
        'source': 'https://github.com/ExpDev07/coronavirus-tracker-api',
    }
Esempio n. 5
0
def get_data(category):
    """
    Retrieves the data for the provided type. The data is cached for 1 hour.
    """
    category = category.lower()

    # URL to request data from.
    url = base_url + "time_series_covid19_%s_global.csv" % category

    request = requests.get(url)
    text = request.text
    # Parse the CSV.
    data = list(csv.DictReader(text.splitlines()))
    # The normalized locations.
    locations = []

    for item in data:
        # Filter out all the dates.
        history = dict(
            filter(lambda element: date_util.is_date(element[0]),
                   item.items()))
        # Sorted date history
        history = sorted_history_date(formated_date(history))
        # Country for this location.
        country = item['Country/Region']
        # Latest data insert value.
        latest = list(history.values())[-1]
        # Normalize the item and append to locations.
        locations.append({
            'country': country,
            'country_code': countrycodes.country_code(country),
            'province': item['Province/State'],
            # History.
            'history': history,
            # Latest statistic.
            'total': int(latest or 0),
        })

    # Latest total.
    total = sum(map(lambda location: location['total'], locations))
    # Return the final data.
    return {
        'locations': locations,
        'total': total,
        'last_updated': datetime.utcnow().isoformat() + 'Z'
    }
Esempio n. 6
0
def get_all_data_by_category(category):
    """
    Get all the data of all the countries by category. There is three category (confirmed | death | recovered)
    """
    data = request.get_data_time_series(category)
    # The normalized locations.
    locations = []

    for item in data:
        # Filter out all the dates.
        history = dict(
            filter(lambda element: date_util.is_date(element[0]),
                   item.items()))
        # Sorted date history
        history = sorted_history_date(formated_date(history))
        # Country for this location.
        country = item['Country/Region']
        # Latest data insert value.
        latest = list(history.values())[-1]
        # Normalize the item and append to locations.
        locations.append({
            # General info.
            'country': country,
            'country_code': countrycodes.country_code(country),
            'province': item['Province/State'],
            # History.
            'history': history,
            # Latest statistic.
            'total': int(latest or 0),
        })

    # Latest total.
    total = sum(map(lambda location: location['total'], locations))
    # Return the final data.
    return {
        'locations': locations,
        'total': total,
        'last_updated': datetime.utcnow().isoformat() + 'Z'
    }
Esempio n. 7
0
def test_countrycodes_synonym(country_name_synonym, expected_country_code):
    assert countrycodes.country_code(country_name_synonym) == expected_country_code
Esempio n. 8
0
def test_countrycodes_is_3166_1(country_name, expected_country_code):
    assert countrycodes.country_code(country_name) == expected_country_code
Esempio n. 9
0
def get_data_country(country_code):
    """
    Get the data information (death, confirmed, recovered, ) of a country and theirs province.
    """
    data = request.get_data_daily_reports()

    locations = [
        {
            'country': '',
            'country_code': '',
            'population': get_population_by_county(country_code.upper()),
            'total': {
                'confirmed': '',
                'deaths': '',
                'recovered': '',
                'active': '',
                'Incident_Rate': '',
                'Case_Fatality_Ratio': ''
            },
            'provinces': []
        }
    ]
    provinces = []

    for item in data:
        if countrycodes.country_code(item['Country_Region']) == country_code.upper() and item['Province_State'] == '':
            locations[0]['country'] = item['Country_Region']
            locations[0]['country_code'] = countrycodes.country_code(item['Country_Region'])
            locations[0]['total']['confirmed'] = item['Confirmed']
            locations[0]['total']['deaths'] = item['Deaths']
            locations[0]['total']['recovered'] = item['Recovered']
            locations[0]['total']['active'] = item['Active']
            locations[0]['total']['Incident_Rate'] = item['Incident_Rate']
            locations[0]['total']['Case_Fatality_Ratio'] = item['Case_Fatality_Ratio']
        # Add the provinces info to the country mother
        if countrycodes.country_code(item['Country_Region']) == country_code and item['Province_State'] != '':
            locations[0]['country'] = item['Country_Region']
            locations[0]['country_code'] = countrycodes.country_code(item['Country_Region'])
            provinces.append({
                'name': item['Province_State'],
                'administration': item['Admin2'],
                'total': {
                    'confirmed': item['Confirmed'],
                    'deaths': item['Deaths'],
                    'recovered': item['Recovered'],
                    'active': item['Active'],
                    'Incident_Rate': item['Incident_Rate'],
                    'Case_Fatality_Ratio': item['Case_Fatality_Ratio']
                }
            })

    # Add all the provinces
    locations[0]['provinces'] = provinces

    # Check the global result of the country
    if locations[0]['total']['confirmed'] == '':
        locations[0]['total']['confirmed'] = sum(int(province['total']['confirmed']) for province in locations[0]['provinces'])
        locations[0]['total']['deaths'] = sum(int(province['total']['deaths']) for province in locations[0]['provinces'])
        locations[0]['total']['active'] = sum(int(province['total']['active']) for province in locations[0]['provinces'] if province['total']['active'] != '')
        locations[0]['total']['recovered'] = sum(int(province['total']['recovered']) for province in locations[0]['provinces'])

    # Open file and add population for provinces
    data = request.get_data_info_country()
    for province in locations[0]['provinces']:
        province['population'] = get_population_by_province(data, province['name'], province['administration'])

    return {
        'locations': locations,
        'last_updated': datetime.utcnow().isoformat() + 'Z'
    }
def get_data(category):
    """
    Retrieves the data for the provided type. The data is cached for 1 hour.
    """

    # Adhere to category naming standard.
    category = category.lower().capitalize()

    print(">>>>>>", category)
    print("Base_url: >>> ", base_url % category)

    # Request the data
    request = requests.get(base_url % category)
    text = request.text

    # Parse the CSV.
    data = list(csv.DictReader(text.splitlines()))

    # The normalized locations.
    locations = {}

    latest_count = 0  # latest count

    for item in data:
        # Filter out all the dates.
        history = dict(
            filter(lambda element: date_util.is_date(element[0]),
                   item.items()))

        # Country for this location.
        country = item['Country/Region']

        # Latest data insert value.
        latest = list(history.values())[-1]

        # Normalize the item and append to locations.
        if (country not in locations):
            locations[country] = []

        locations[country].append({
            # General info.
            'country':
            country,
            'country_code':
            countrycodes.country_code(country),
            'province':
            item['Province/State'],

            # Coordinates.
            'coordinates': {
                'lat': item['Lat'],
                'long': item['Long'],
            },

            # History.
            'history':
            history,

            # Latest statistic.
            'latest':
            int(latest or 0),
        })
        latest_count += int(latest or 0)

    # Return the final data.
    return {
        'locations': locations,
        'latest': latest_count,
        'last_updated': datetime.utcnow().isoformat() + 'Z'
    }