def covid_api(city: str): """Fetches data from Covid-19 API It returns a dictionary with countries in GB and City specified in config.json file as keys and data about covid as values. Data contains such information: "date","areaName","areaCode","newCasesByPublishDate", "cumCasesByPublishDate","newDeathsByDeathDate", "cumDeathsByDeathDate","cumDeathsByDeathDate" to access this information: api["country"]["data"][days since data gained]["information you want to access"] where 0 stands for the most recent data in days since data gained. example: england_covid = APIs_fetcher.covid_api()["England"]['data'][0]['cumCasesByPublishDate'] is the cumulative number of cases in England until yesterday. """ # create a filter for each counry in the UK list_of_countries_filters = [] countries = ["England", "Scotland", "Northern Ireland", "Wales"] for country in countries: list_of_countries_filters.append( [ 'areaType=nation', 'areaName={}'.format(country), 'date={}'.format(datetime.strftime(datetime.now() - timedelta(1), "%Y-%m-%d")) ] ) # create a filter for the city from config.json file city_filter = [ 'areaName={}'.format(city), 'date={}'.format(datetime.strftime(datetime.now() - timedelta(1), "%Y-%m-%d")) ] cases_and_deaths = { "date": "date", "newCasesByPublishDate": "newCasesByPublishDate", "cumCasesByPublishDate": "cumCasesByPublishDate", "newDeathsByDeathDate": "newDeathsByDeathDate", "cumDeathsByDeathDate": "cumDeathsByDeathDate" } apis = {} for i in enumerate(list_of_countries_filters): try: country = Cov19API(filters=list_of_countries_filters[i], structure=cases_and_deaths).get_json() except: logger.error("Encountered problem while trying to retrieve data from covid api.") apis[countries[i]] = country try: apis[city] = Cov19API(filters=city_filter, structure=cases_and_deaths).get_json() except: logger.error("Encountered problem while trying to " "retrieve data from covid api.") return apis
def get_region_data(metrics=None, nhs_metrics=None): if metrics is None: metrics = [ "newCasesByPublishDate", "newCasesBySpecimenDate", "newDeathsByDeathDate", "uniquePeopleTestedBySpecimenDateRollingSum", "uniqueCasePositivityBySpecimenDateRollingSum", "newDeaths28DaysByPublishDate", "newDeaths28DaysByDeathDate", ] structure = { "date": "date", "areaName": "areaName", } for metric in metrics: structure[metric] = metric filters = [ "areaType=region", ] api = Cov19API(filters=filters, structure=structure) df_region = api.get_dataframe() df_region['date'] = pd.to_datetime(df_region['date']) if nhs_metrics is None: nhs_metrics = ["newAdmissions", "covidOccupiedMVBeds", "hospitalCases"] structure = { "date": "date", "areaName": "areaName", } for metric in nhs_metrics: structure[metric] = metric filters = ["areaType=nhsRegion"] api = Cov19API(filters=filters, structure=structure) df_nhs_region = api.get_dataframe() df_nhs_region['date'] = pd.to_datetime(df_nhs_region['date']) # Combine regions to be the same as NHS regions df_region['areaName'] = df_region['areaName'].replace({ 'Yorkshire and The Humber': 'North East and Yorkshire', 'North East': 'North East and Yorkshire', 'East Midlands': 'Midlands', 'West Midlands': 'Midlands' }) df_grouped_region = df_region.groupby(['date', 'areaName'], as_index=False).sum() # Merge data from regions and NHS regions df = pd.merge(df_grouped_region, df_nhs_region, on=['date', 'areaName'], how='outer') return df
def covid_fetch(): """This function fetches the data from the COVID API as a dictionary""" #Sets the structure of the data retrieved from the API cases_and_deaths = { "date": "date", "areaName": "areaName", "areaCode": "areaCode", "newCasesByPublishDate": "newCasesByPublishDate", "cumCasesByPublishDate": "cumCasesByPublishDate", "newDeathsByDeathDate": "newDeathsByDeathDate", "cumDeathsByDeathDate": "cumDeathsByDeathDate" } #Sets the filter for the API using config.json covid_nation = ['areaType=nation'] nation = 'areaName=' + str(config_fetcher("covid_region")) covid_nation.append(nation) #Gets API latest data covid_api = Cov19API( filters=covid_nation, structure=cases_and_deaths, ) #Gets data in form of dictionary covid_json = covid_api.get_json() #Gets timestamp for last update covid_timestamp = covid_api.last_update #Assign data to variables covid_data = covid_json[ 'data'] #This formats the data as a list, while I want a dictionary, hence the next line. return covid_data
def covid_handler(): """This module grabs covid data from a module""" # covidstructure is specified in config file api = Cov19API(filters=england_only, structure=covidstructure) covid_data = api.get_json() # Gets a json file of covid data logging.info("covid_data retrieved") return covid_data["data"][0] # Gets the latest day's data
def test_options(self) -> None: data = Cov19API.options() self.assertIn("servers", data) server_url = data["servers"][0]["url"] self.assertEqual(Cov19API.endpoint, server_url)
def total_cases(): '''Function for total cases in England''' total_cases = {"cumCasesByPublishDate": "cumCasesByPublishDate",} api = Cov19API(filters=england_only, structure=total_cases) json_data = api.get_json(as_string=True) latest_data = json_data[34:41] return latest_data
def parse(self, sources: Dict[str, str], aux: Dict[str, DataFrame], **parse_opts) -> DataFrame: cases = { "date": "date", "areaCode": "areaCode", "newCasesBySpecimenDate": "newCasesBySpecimenDate", "cumCasesBySpecimenDate": "cumCasesBySpecimenDate", } api = Cov19API(filters=["areaType=utla"], structure=cases) data = api.get_dataframe() data.areaCode = data.areaCode.apply(_apply_area_code_map) data = data.groupby(["date", "areaCode"], as_index=False).sum() data = table_rename( data, { "areaCode": "subregion2_code", "newCasesBySpecimenDate": "new_confirmed", "cumCasesBySpecimenDate": "total_confirmed", "date": "date", }, drop=True, ) data.date = data.date.apply( lambda x: datetime_isoformat(x, "%Y-%m-%d")) return data
def covid_update(region=region): """ This function takes in a region as argument and returns a string giving information about the latst coronavirus data """ SouthWest = ["areaType=region", "areaName=" + region] cases_and_deaths = {"newCasesByPublishDate": "newCasesByPublishDate"} api = Cov19API( filters=SouthWest, structure=cases_and_deaths, ) if api: f = open("latest_covid_info.txt", "w") f.write(str(api)) f.close() else: f = open("latest_covid_info.txt") api = f.read() data = api.get_json()['data'] latestCasesByPublishDate = data[0]['newCasesByPublishDate'] rate = 1 + (latestCasesByPublishDate - data[1]['newCasesByPublishDate'] ) / data[0]['newCasesByPublishDate'] rate = int(rate * 100) / 100 rate = str(rate) + '. ' newCases = 'Latest data for new cases by publish date is ' + str( latestCasesByPublishDate) + ' cases in your region. ' return rate + newCases
def make_notifications() -> None: ''' Returns ------- None This function calls on the news_filter and weather_update modules to collect the data form the relevant API's. It then unpacks them an appends them to the NOTIFICATIONS list for rendering.''' global NOTIFICATIONS NOTIFICATIONS = [] api = Cov19API(filters=covid_data["nationally"], structure=covid_data["cases_and_deaths"]) data = api.get_json()['data'][0] NOTIFICATIONS.append({'title': 'Covid Data',\ 'content': Markup( f'<p style="text-align:left;">The COVID data for the\ {data["areaName"]} as of {data["Date"]} is:\ <br />Cases:\ <br /> Total Cases: {str(data["cases"]["cumCasesByPublishDate"])}\ <br /> New Cases: {str(data["cases"]["newCasesByPublishDate"])}\ <br />Deaths:\ <br /> Total Deaths: {str(data["deaths"]["cumDeaths28DaysByPublishDate"])}\ <br /> New Deaths: {str(data["deaths"]["newDeaths28DaysByPublishDate"])}</p>')}) NOTIFICATIONS.append({'title':'Weather',\ 'content':Markup(get_weather(location['city'], API_keys['weather']))}) NOTIFICATIONS.append({'title':'Headlines',\ 'content':Markup(get_news(location['country'], API_keys['news'], True))}) for notification in get_news(location['country'], API_keys['news']): NOTIFICATIONS.append(notification) logging.info('Notifications have been reloaded.')
def newest_deaths(): '''Function for deaths per day in England''' new_deaths = {"newDeathsByDeathDate": "newDeathsByDeathDate",} api = Cov19API(filters=england_only, structure=new_deaths) json_data = api.get_json(as_string=True) latest_data = json_data[33:36] return latest_data
def newest_cases(): '''Function for cases per day in England''' new_cases = {"newCasesByPublishDate": "newCasesByPublishDate",} api = Cov19API(filters=england_only, structure=new_cases) json_data = api.get_json(as_string=True) latest_data = json_data[34:39] return latest_data
def total_deaths(): '''Function for total deaths in England''' tot_deaths = {"cumDeathsByDeathDate": "cumDeathsByDeathDate"} api = Cov19API(filters=england_only, structure=tot_deaths) json_data = api.get_json(as_string=True) latest_data = json_data[33:38] return latest_data
def get_covid() -> dict: """Scrapes the most recent covid update data and concatenates it to a dictionary to be returned""" area = "areaName=" + get_json_info("city") england_only = [area] api = Cov19API(filters=england_only, structure=cases_and_deaths,\ latest_by = "newCasesByPublishDate") covid_response = api.get_json() json_response_covid = covid_response["data"] covidtimenow = strftime("%Y-%m-%dT", time.localtime()) + "00:00" covid_string = "There have been " if json_response_covid[0]["newCasesByPublishDate"] != 0: covid_string += str(json_response_covid[0]["newCasesByPublishDate"])\ + " new cases, for a total of "\ + str(json_response_covid[0]["cumCasesByPublishDate"]) +" cases. " else: covid_string += " no new cases, for a total of "\ + str(json_response_covid[0]["cumCasesByPublishDate"]) + " cases. " if json_response_covid[0]["newDeathsByDeathDate"] is not None: covid_string += "There have been "\ + str(json_response_covid[0]["newDeathsByDeathDate"])\ + "new deaths, for a total of "\ +str(json_response_covid[0]["cumDeathsByDeathDate"]) + " deaths. " else: if json_response_covid[0]["cumDeathsByDeathDate"] is not None: covid_string += "There have been no new deaths, for a total of "\ +str(json_response_covid[0]["cumDeathsByDeathDate"]) + "." else: covid_string += "There have been no deaths." logging.info("Got COVID-19 update") return { "title": "COVID-19 Update", "content": covid_string, "publishingTime": covidtimenow }
def GetUKData(area_name): area = [ "areaType=utla", f"areaName={area_name}", ] cases: StructureType = { "date": "date", "areaName": "areaName", "newCasesBySpecimenDate": "newCasesBySpecimenDate", "cumCasesBySpecimenDate": "cumCasesBySpecimenDate", "cumCasesBySpecimenDateRate": "cumCasesBySpecimenDateRate", } api = Cov19API( filters=area, structure=cases, ) df = cache.get_or_set(area_name, api.get_dataframe(), 5 * 60) df = df[::-1] return df
def covid_new_cases_uk(): """ Returns new cases since last report :return: int """ all_nations = ["areaType=nation"] cases_and_deaths = { "date": "date", "areaName": "areaName", "areaCode": "areaCode", "newCasesByPublishDate": "newCasesByPublishDate", "cumCasesByPublishDate": "cumCasesByPublishDate", "newDeathsByDeathDate": "newDeathsByDeathDate", "cumDeathsByDeathDate": "cumDeathsByDeathDate" } api = Cov19API(filters=all_nations, structure=cases_and_deaths, latest_by="newCasesByPublishDate") data = api.get_json() # data = json.loads(data) total = 0 for nation in data["data"]: total += nation["newCasesByPublishDate"] return total
def covid_stats(): '''Calls data from Public Health Service, uses the uk_covid19 module to do this. Makes a loud statement report right before finishing.''' area_filter = ['areaType=nation', 'areaName=England'] cases_and_deaths = { "date": "date", "areaName": "areaName", "areaCode": "areaCode", "newCasesByPublishDate": "newCasesByPublishDate", "cumCasesByPublishDate": "cumCasesByPublishDate", "newDeathsByDeathDate": "newDeathsByDeathDate", "cumDeathsByDeathDate": "cumDeathsByDeathDate" } api = Cov19API(filters=area_filter, structure=cases_and_deaths) data = api.get_json() date = data['data'][0]['date'] location = region new_deaths = data['data'][0]["newDeathsByDeathDate"] new_cases = data['data'][0]["newCasesByPublishDate"] total_cases = data['data'][0]["cumCasesByPublishDate"] formatted_date = datetime.datetime.strptime(date, "%Y-%m-%d") formatted_date = datetime.datetime.strftime(formatted_date, "%-d %B %Y") text_to_read_covid = ('Covid news ' + str(location) + '. In ' + str(formatted_date) + ', ' + str(new_deaths) + ' has died of coronavirus; ' + str(new_cases) + ' people has been diagnosed by covid-19. ' + str(total_cases) + ' is the total case in ' + str(location) + 'today.') announce(text_to_read_covid)
def main(): pcr_testing = { "Date": "date", "newPillarOneTestsByPublishDate": "newPillarOneTestsByPublishDate", "cumPillarOneTestsByPublishDate": "cumPillarOneTestsByPublishDate", "newPillarTwoTestsByPublishDate": "newPillarTwoTestsByPublishDate", "cumPillarTwoTestsByPublishDate": "cumPillarTwoTestsByPublishDate" } api = Cov19API(filters=["areaType=overview"], structure=pcr_testing) df = api.get_dataframe() df.loc[:, "Cumulative total"] = (df.cumPillarOneTestsByPublishDate.add( df.cumPillarTwoTestsByPublishDate)) df.loc[:, "Daily change in cumulative total"] = ( df.newPillarOneTestsByPublishDate.add( df.newPillarTwoTestsByPublishDate)) df = df[["Date", "Cumulative total", "Daily change in cumulative total"]] df.loc[:, "Country"] = "United Kingdom" df.loc[:, "Units"] = "tests performed" df.loc[:, "Source label"] = "Department of Health and Social Care and Public Health England" df.loc[:, "Source URL"] = "https://coronavirus.data.gov.uk/developers-guide" df.loc[:, "Notes"] = "Sum of tests processed for pillars 1 and 2" df.loc[:, "Testing type"] = "PCR only" df.to_csv("automated_sheets/United Kingdom - tests.csv", index=False)
def getData(typ, areatype, areaname): structure = { "date": "date", "name": "areaName", "code": "areaCode", "dailyCases": "newCasesByPublishDate", "newAdmissions": "newAdmissions", "Deaths": "newDeaths28DaysByPublishDate", "MVCases": "covidOccupiedMVBeds", "DoseA": "cumPeopleVaccinatedFirstDoseByPublishDate", "DoseB": "cumPeopleVaccinatedSecondDoseByPublishDate", "Tests": "newTestsByPublishDate", } if areatype == 'overview': filters = [f"areaType={ areatype }"] else: filters = [f"areaType={ areatype }", f"areaName={ areaname }"] api = Cov19API(filters=filters, structure=structure) upddt = api.get_release_timestamp() print('Data last updated: ', upddt) # get data as csv. Can also get as json or xml # and save it to a file # data = api.get_csv() fname = os.path.join('./', areaname + '.csv') _ = api.get_csv(save_as=fname) data = pandas.read_csv(fname) return data
def get_covid() -> dict: '''This function is a standard fetch function, and it allows the program to fetch the amount of new deaths and new cases in the past day, in a specific region.''' with open('config.json') as json_file: data = json.load(json_file) moredata = data['covid'] areatype = moredata['area type'] areaname = moredata['area name'] england_only = [] england_only.append('areaType=' + areatype) england_only.append('areaName=' + areaname) coviddict = {} cases_and_deaths = { "date": "date", "areaName": "areaName", "areaCode": "areaCode", "newCasesByPublishDate": "newCasesByPublishDate", "cumCasesByPublishDate": "cumCasesByPublishDate", "newDeathsByDeathDate": "newDeathsByDeathDate", "cumDeathsByDeathDate": "cumDeathsByDeathDate" } api = Cov19API(filters=england_only, structure=cases_and_deaths) data = api.get_json() for iteral in data: casesanddeaths = data["data"] todaysdate = casesanddeaths[0] cases_today = str(todaysdate["newCasesByPublishDate"]) deaths_today = str(todaysdate["newDeathsByDeathDate"]) coviddict["title"] = 'COVID-19 Update' coviddict[ "content"] = 'Cases today: ' + cases_today + '\n Deaths today: ' + deaths_today iteral += '' return coviddict
def __init__(self): self.api = Cov19API(filters=self.filters, structure=self.structure) PHEstored = configs.config.get('Wales') if PHEstored: self.Wales_cases = PHEstored.get('wales_total_cases') else: self.Wales_cases = None self.top()
def covid_API(cases_and_deaths: dict) -> dict: """ Imports Covid Data :param cases_and_deaths: This obtains dictionary from config file :return: A dictionary of covid information """ api = Cov19API(filters=england_only, structure=cases_and_deaths) data = api.get_json() return data
def __init__(self, force_update=False): self.today = date.today() self.api = Cov19API(filters=self.filters, structure=self.structure) self.edition = None self.sequences = ['ltla', 'region'] #self.api.latest_by='cumCasesBySpecimenDate' - this fetches only latest cases #self.fetch - get self.data_all = [] self.force_update = force_update
def get_covid_data(pop, proj_days): all_nations = ["areaType=nation"] get_data = { "date": "date", "areaName": "areaName", "newPeopleVaccinatedFirstDoseByPublishDate": "newPeopleVaccinatedFirstDoseByPublishDate", "newPeopleVaccinatedSecondDoseByPublishDate": "newPeopleVaccinatedSecondDoseByPublishDate", "cumPeopleVaccinatedFirstDoseByPublishDate": "cumPeopleVaccinatedFirstDoseByPublishDate", "cumPeopleVaccinatedSecondDoseByPublishDate": "cumPeopleVaccinatedSecondDoseByPublishDate", "newCasesBySpecimenDate": "newCasesBySpecimenDate", "newDeaths28DaysByDeathDate": "newDeaths28DaysByDeathDate", "newAdmissions": "newAdmissions", "cumAdmissionsByAge": "cumAdmissionsByAge", "maleCases": "maleCases", "femaleCases": "femaleCases", "newDeaths28DaysByDeathDateAgeDemographics": "newDeaths28DaysByDeathDateAgeDemographics" } api = Cov19API(filters=all_nations, structure=get_data) last_update_utc = api.get_json()['lastUpdate'] last_update_utc = datetime.fromisoformat(last_update_utc[:-1]) last_update_utc = last_update_utc.replace(tzinfo=tz.tzutc()) # Convert time zone last_update_local = last_update_utc.astimezone(tz.gettz("Europe/London")) last_update = last_update_local.strftime("%a %d %b %H:%M") full_df = api.get_dataframe() recent_cases_df, recent_cases_fit_end = get_cases(full_df) cases_by_age_df, cases_age_groups = get_cases_by_age(full_df) recent_deaths_df, recent_deaths_fit_end = get_deaths(full_df) deaths_by_age_df, deaths_age_groups = get_deaths_by_age(full_df) recent_admissions_df, recent_admissions_fit_end = get_admissions(full_df) admissions_by_age_df, ad_age_groups = get_admissions_by_age(full_df) vacc_df, rolling_avg, end_date = get_vaccinations(full_df, pop, proj_days) return vacc_df, last_update, rolling_avg, end_date, recent_cases_df, recent_cases_fit_end, \ recent_deaths_df, recent_deaths_fit_end, recent_admissions_df, recent_admissions_fit_end, \ admissions_by_age_df, ad_age_groups, cases_by_age_df, cases_age_groups, \ deaths_by_age_df, deaths_age_groups
def get_nations_data(): current_update = read_last_update() if Update.last_update == '' or Update.last_update != current_update: api_all_nation = Cov19API(filters=ALL_NATIONS, structure=CASES_AND_DEATHS_NATION) api_uk_overview = Cov19API(filters=UK, structure=CASES_AND_DEATHS_NATION) Update.data_json = api_all_nation.get_json() Update.data_json_uk = api_uk_overview.get_json() api_all_nation.get_json(save_as="tmp\data_all_nation.json") Update.last_update = current_update release_timestamp = Cov19API.get_release_timestamp() Update.release_date = datetime.fromisoformat( release_timestamp.strip("Z")).strftime("%d.%m.%Y %H:%M:%S") #print(datetime.now()) #print(Update.release_date) Update.data_obj_list = convert_update_to_obj() print('inside service.get_nations_data()') return Update.data_obj_list
def set_up_api(): """this function sets up the api for use""" filter_specs = ['areaType=nation', 'areaName=' + get_location('nation')] cases_and_deaths = { "newCasesByPublishDate": "newCasesByPublishDate", "cumCasesByPublishDate": "cumCasesByPublishDate", "newDeathsByDeathDate": "newDeathsByDeathDate", "cumDeathsByDeathDate": "cumDeathsByDeathDate" } api = Cov19API(filters=filter_specs, structure=cases_and_deaths) return api
def get_nhs_region() -> pd.DataFrame: filters = ["areaType=nhsRegion"] metrics = { "Year": "date", "Country": "areaName", "areaCode": "areaCode", "weekly_hospital_admissions": "newAdmissionsRollingSum", "people_in_hospital": "hospitalCases", } api = Cov19API(filters=filters, structure=metrics) return api.get_dataframe()
def get_coronavirus_data_test() -> str: """This function is a test function to show the functionality of the covid API """ with open("config.json", "r") as file: json_file = json.load(file) cases_and_deaths = json_file['covid-data-filters']['cases_and_deaths'] overview = ['areaType=overview'] covid_api = Cov19API(filters=overview, structure=cases_and_deaths) covid_data = covid_api.get_json() # Here the area code is exctracted as its value stays the same throughout the json object area_code = covid_data['data'][0]['code'] return area_code
def get_covid_info() -> dict : """ Gets covid data based on users perameters in config file, then formats it for the use in notifications and announcements. areaCode = gives the area in the UK the user wants data for. cases_and_deaths = the specific type of covid data being retrieved. Local variables: filter_param = a list containing the areaCode from the config file. Global variables: covid_notif = a list (of dictionaries) on covid data previously collected. current_notif = a (list of dictionaries) containing current notifications (weather, COVID and news). Returns: A string containing the formatted covid notification. """ filter_param = [] #Getting the parameters for the covid info JSON file with open("config.json", "r") as config_file: json_config = json.load(config_file) try: covid_api = json_config["covid_api"] filter_param = [covid_api["areaCode"]] except KeyError as excep: logging.error( excep + " area code or covid dictionary is missing in JSON file ") cases_and_deaths = { "date": "date", "areaName": "areaName", "areaCode": "areaCode", "newCasesByPublishDate": "newCasesByPublishDate", "cumCasesByPublishDate": "cumCasesByPublishDate", "newDeaths28DaysByPublishDate": "newDeaths28DaysByPublishDate", "cumDeaths28DaysByPublishDate": "cumDeaths28DaysByPublishDate" } #Getting the JSON file of user-defined parameters api = Cov19API(filters=filter_param, structure=cases_and_deaths, latest_by="date") covid_json = api.get_json() covid_data = covid_json['data'] #Creates a user friendly version of the covid info in the form of a string covid_notification = {"title" : "COVID Update" , "content": "There are " + str(covid_data[0]['newCasesByPublishDate']) + " new cases today in " + covid_data[0]["areaName"] + ". A total of " + str(covid_data[0]['cumCasesByPublishDate']) + " cases. There has been " + str( covid_data[0]["newDeaths28DaysByPublishDate"]) + " new deaths. A total of " + str( covid_data[0]["cumDeaths28DaysByPublishDate"]) + " deaths."} #Checking if the covid notification isnt already in the list of current notifications if covid_notification not in global_variables.covid_notif: global_variables.covid_notif.append(covid_notification) global_variables.current_notifs.insert(1,covid_notification) logging.info(covid_notification["content"] + " was added to the notification list.") return covid_notification
def test_website_timestamp(self): release_timestamp = Cov19API.get_release_timestamp() api_date = self.api.last_update self.assertIn("Z", release_timestamp) self.assertNotEqual(release_timestamp, api_date) website_date = datetime.fromisoformat( release_timestamp.strip("Z")).date() api_date = datetime.fromisoformat(api_date.strip("Z")).date() self.assertEqual(website_date, api_date)
def uk_cumulative_cases(): print(1) api = Cov19API(filters=all_nations, structure=cases_and_deaths, latest_by="newCasesByPublishDate") print(2) df = api.get_dataframe() print(df)