Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chat-bot/covidCaseAPI.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
70 lines (65 sloc)
3 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Code for covid cases API | |
import requests | |
from datetime import date | |
def getTravelRestriction(countryName): | |
try: | |
unchangedCountryName = countryName.title() | |
apiList = [] | |
fixedTravelRestriction = ' ' | |
countryName = countryName.lower() | |
countryName = countryName.replace(' ', '-') | |
url = "https://api.covid19api.com/premium/travel/country/" + countryName | |
payload = {} | |
headers = { | |
'X-Access-Token': '5cf9dfd5-3449-485e-b5ae-70a60e997864' | |
} | |
response = requests.request("GET", url, headers=headers, data=payload) | |
apiList = str(response.text) | |
apiList = apiList.replace('"', '') | |
apiList = list(apiList.split(",")) | |
for word in apiList: | |
if 'Note:' in word: | |
travelRestriction = word | |
travelRestriction = travelRestriction.replace('Note:','') | |
travelRestriction = list(travelRestriction.split(' ')) | |
for i in range(len(travelRestriction)): | |
if 'http' in travelRestriction[i]: | |
travelRestriction[i] = 'Source: ' + travelRestriction[i] | |
if len(travelRestriction) <= 4: #If API provides invalid data | |
return("Sorry, I don't have any travel restriction information in " + unchangedCountryName) | |
fixedTravelRestriction = fixedTravelRestriction.join(travelRestriction) | |
return ('Travel restriction for ' + unchangedCountryName + ': ' + fixedTravelRestriction) | |
except UnboundLocalError: #If API has no data on that country | |
return ("Sorry, I don't have any travel restriction information in " + unchangedCountryName) | |
def getStatsByCountry(countryName): | |
try: | |
unchangedCountryName = countryName.title() | |
today = str(date.today()) | |
countryName = countryName.lower() | |
countryName = countryName.replace(' ', '-') | |
url = "https://api.covid19api.com/premium/country/" + countryName | |
payload = {} | |
headers = { | |
'X-Access-Token': '5cf9dfd5-3449-485e-b5ae-70a60e997864' | |
} | |
response = requests.request("GET", url, headers=headers, data=payload) | |
apiList = str(response.text.encode('utf8')) | |
apiList = apiList.replace('"', '') | |
apiList = list(apiList.split(",")) | |
for words in apiList: | |
if 'NewCases:' in words: | |
dailyCases = words.replace('NewCases:','') | |
if 'TotalCases:' in words: | |
totalCases = words.replace('TotalCases:', '') | |
if 'NewDeaths:' in words: | |
deathCases = words.replace('NewDeaths:', '') | |
if 'TotalDeaths:' in words: | |
totalDeathCases = words.replace('TotalDeaths:', '') | |
#if statsType == 'new-cases': | |
return ('Daily cases in ' + unchangedCountryName + ' on ' + today + ' is ' + dailyCases + '\n' | |
+'Total cases in ' + unchangedCountryName + ' on ' + today + ' is ' + totalCases +'\n' | |
+'Death cases in ' + unchangedCountryName + ' on ' + today + ' is ' + deathCases + '\n' | |
+ 'Total death cases in ' + unchangedCountryName + ' on ' + today + ' is ' + totalDeathCases + '\n' | |
+ getTravelRestriction(countryName)) | |
except UnboundLocalError: | |
return ("Sorry, I don't have any COVID case information in this location") |