def __parseDates(country,province,dates,data): #Used to check the data file with the original name of province checkprovince = province if province == "": province = "All Provinces" #Keep track of the current position in the dates list to be able to reference the previous date index = 0 for date in dates: #Check if date's directory exists, if not, create it fileManager.mkexistsdir(__getDateDir(country, province, date)) #Get the province's json path provincejson = __getProvinceJson(country, province) #Check if dates json file exists, if not, create it fileManager.jsonPreset(provincejson,"dates") #Load existing dates.json file dateslist = fileManager.readList(provincejson,"dates") #Get the date's json path path = __getDateJson(country, province, date) #If the date doesn't exist in the directory create it, otherwise skip it as date information #doesn't need to be rewritten if not fileManager.exists(path): #Keep track of whether the date was found in the retrieved API information foundfromapi = False cases = __blankCaseDict() #Check all the dates listed from the API json for value in data: #If the value of the current date and province matches the API data assign the case counts if(value["Date"].startswith(date) and value["Province"] == checkprovince): cases = __assignCases(value) foundfromapi = True break #If the case info couldn't be found in the API json, use the previous dates values instead if(not foundfromapi): try: #Get the previous date json prevdate = fileManager.readJson(__getDateJson(country, province, dates[index-1])) cases = __assignCases(prevdate) except FileNotFoundError: cases = __blankCaseDict() #Write the case information to the date's json file fileManager.writeJson(path,cases) #Create the new date's information dictionary and append it to the list of dates newinfodict = {"date":date,"file":path,"cases":cases} dateslist.append(newinfodict) #Rewrite the province's json file fileManager.writeList(provincejson,"dates",dateslist) index += 1
def findWorldwideJson(date): if(fileManager.exists(worldjsonpath)): worldjson = fileManager.readJson(worldjsonpath) for value in worldjson: if value["date"] == date: return value["cases"] return None
def findCountryJson(countryslug, date): try: #Searches through the countries json file to pick the appropriate country's case numbers if(fileManager.exists(countriesjsonpath)): countries = fileManager.readJson(countriesjsonpath) targetcountry = [country for country in countries["countries"] if country["slug"] == countryslug] for i in range(len(targetcountry[0]["dates"])): cases = [country["dates"][i]["cases"] for country in countries["countries"] if country["slug"] == countryslug and country["dates"][i]["date"] == date] #Provided the country exists in the database the country's case counts will be returned if cases != []: return cases[0] except IndexError: pass return None
def __parseProvinces(country,provinces): for province in provinces: if province == "": province = "All Provinces" #Get the country's json path countryjsonpath = __getCountryJson(country) #Check if provinces json file exists, if not, create it fileManager.jsonPreset(countryjsonpath,"provinces") #Get index of province in json array provinceslist = fileManager.readList(countryjsonpath,"provinces") index = __indexOfValueInList("province", province, provinceslist) #Get the most recent number of cases provincedir = __getProvinceDir(country, province) cases = __blankCaseDict() datecount = fileManager.directoryCount(provincedir) #Update the province's cases dict to the most recent dates values for i in range(datecount): currDate = __convertDatetimeString(datetime.today() - timedelta(days=i)) datepath = f"{provincedir}/{currDate}/{currDate}.json" if fileManager.exists(datepath): recent = fileManager.readJson(datepath) cases = recent break #Get the province's json path provincejson = __getProvinceJson(country, province) #Create the province's new information dictionary with the updated case counts newinfodict = {"province":province,"file":provincejson,"cases":cases} #Replace the province's old values if it already exists within the list, #otherwise append the new province's information if(index < len(provinceslist)): provinceslist[index] = newinfodict else: provinceslist.append(newinfodict) #Rewrite the country's json file fileManager.writeList(countryjsonpath,"provinces",provinceslist)
def findProvinceJson(countryslug, province, date): countryname = slugtoname[countryslug] provincejsonname = __jsonnameConversion(province) provincepath = f"JSON/Countries/{countryname}/{province}/{provincejsonname}.json" try: #Searches through the countries json file to pick the appropriate country's case numbers if(fileManager.exists(provincepath)): provincejson = fileManager.readJson(provincepath) cases = [dateval["cases"] for dateval in provincejson["dates"] if dateval["date"] == date] #Provided the country exists in the database the country's case counts will be returned if cases != []: return cases[0] except IndexError: pass #If the country's case counts can not be found the following will be executed #Fetch most recent data from Covid19 API and update the JSON directory return None