-
-
Notifications
You must be signed in to change notification settings - Fork 264
create covidExit.py #1110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
create covidExit.py #1110
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
284 changes: 284 additions & 0 deletions
AutomationScripts/covidExit.py
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,284 @@ | ||
# to escape from a country affected by COVID-19 | ||
|
||
#!/usr/bin/env python | ||
|
||
|
||
from json import loads | ||
from requests import Session | ||
from datetime import datetime | ||
from difflib import get_close_matches | ||
def getStats(date,province): | ||
with Session() as s: | ||
url = f"https://cdn.jsdelivr.net/gh/CSSEGISandData/COVID-19/csse_covid_19_data/csse_covid_19_daily_reports/{date}.csv" | ||
r = s.get(url) | ||
csv = (str(r.content).replace('\\r','\n').replace(r'\n','\n').replace("b'",'').replace("'",'').replace(", "," - ")).split('\n') | ||
headings = ['FIPS','Admin2','Province_State','Country_Region','Last_Update','Lat','Long_','Confirmed','Deaths','Recovered','Active','Combined_Key','Incident_Rate','Case_Fatality_Ratio'] | ||
for i in csv: | ||
try: | ||
dictionary = dict(zip(headings,i.split(','))) | ||
if dictionary["Province_State"].upper().replace(' ','') == province.upper().replace(' ',''): | ||
return dictionary | ||
except: | ||
continue | ||
for i in csv: | ||
try: | ||
dictionary = dict(zip(headings,i.split(','))) | ||
if dictionary["Country_Region"].upper().replace(' ','') == province.upper().replace(' ',''): | ||
return dictionary | ||
except: | ||
continue | ||
return 0 | ||
def getCountry(): | ||
with Session() as s: | ||
url = 'http://ip-api.com/json' | ||
r = s.get(url) | ||
locadiction = loads(r.content) | ||
return locadiction["country"] | ||
def getPopulation(country): | ||
with Session() as s: | ||
url = f"https://restcountries.eu/rest/v2/name/{country}" | ||
r = s.get(url) | ||
try: | ||
countries = [] | ||
for i in loads(r.content): | ||
countries.append(i["name"]) | ||
theone = get_close_matches(country,countries, n=1)[0] | ||
c=0 | ||
for i in loads(r.content): | ||
if i["name"] == theone: | ||
break | ||
c+=1 | ||
for i in loads(r.content)[c].keys(): | ||
print((i).upper()," : ",loads(r.content)[c][i]) | ||
return (loads(r.content)[c]) | ||
except: | ||
return 0 | ||
def getAirport(province): | ||
with Session() as s: | ||
url = 'https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat' | ||
r = s.get(url) | ||
csv = (str(r.content).replace('\\r','\n').replace(r'\n','\n').replace("b'",'').replace("'",'').replace(", "," - ").replace('"','')).split('\n') | ||
headings = ['OA code','airport name','city','country','IATA','ICAO','lat','lon','elev','X','x','continent/city','type','source'] | ||
for i in csv: | ||
try: | ||
dictionary = dict(zip(headings,i.split(','))) | ||
if dictionary["city"].upper().replace(' ','') == province.upper().replace(' ',''): | ||
return [dictionary] | ||
except: | ||
continue | ||
national = [] | ||
for i in csv: | ||
try: | ||
dictionary = dict(zip(headings,i.split(','))) | ||
if dictionary["country"].upper().replace(' ','') == province.upper().replace(' ',''): | ||
national.append(dictionary) | ||
except: | ||
continue | ||
return national | ||
def getProvinceIATA(iata): | ||
with Session() as s: | ||
url = 'https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat' | ||
r = s.get(url) | ||
csv = (str(r.content).replace('\\r','\n').replace(r'\n','\n').replace("b'",'').replace("'",'').replace(", "," - ").replace('"','')).split('\n') | ||
headings = ['OA code','airport name','city','country','IATA','ICAO','lat','lon','elev','X','x','continent/city','type','source'] | ||
for i in csv: | ||
try: | ||
dictionary = dict(zip(headings,i.split(','))) | ||
if dictionary["IATA"].upper().replace(' ','') == iata.upper().replace(' ',''): | ||
return [dictionary] | ||
except: | ||
continue | ||
national = [] | ||
for i in csv: | ||
try: | ||
dictionary = dict(zip(headings,i.split(','))) | ||
if dictionary["IATA"].upper().replace(' ','') == iata.upper().replace(' ',''): | ||
national.append(dictionary) | ||
except: | ||
continue | ||
return national[0]["country"] | ||
def getAirlines(province): | ||
with Session() as s: | ||
url = 'https://raw.githubusercontent.com/jpatokal/openflights/master/data/airlines.dat' | ||
r = s.get(url) | ||
csv = (str(r.content).replace('\\r','\n').replace(r'\n','\n').replace("b'",'').replace("'",'').replace(", "," - ").replace('"','')).split('\n') | ||
headings = ['OA Code','Airline Name','x','IATA','ICAO','Callsign','Country','X'] | ||
national = [] | ||
for i in csv: | ||
try: | ||
dictionary = dict(zip(headings,i.split(','))) | ||
if dictionary["Country"].upper().replace(' ','') == province.upper().replace(' ',''): | ||
national.append(dictionary) | ||
except: | ||
continue | ||
return national | ||
def getAirlinesIATA(iata): | ||
with Session() as s: | ||
province = getProvinceIATA(iata)[0]["country"] | ||
url = 'https://raw.githubusercontent.com/jpatokal/openflights/master/data/airlines.dat' | ||
r = s.get(url) | ||
csv = (str(r.content).replace('\\r','\n').replace(r'\n','\n').replace("b'",'').replace("'",'').replace(", "," - ").replace('"','')).split('\n') | ||
headings = ['OA Code','Airline Name','x','IATA','ICAO','Callsign','Country','X'] | ||
national = [] | ||
for i in csv: | ||
try: | ||
dictionary = dict(zip(headings,i.split(','))) | ||
if dictionary["Country"].upper().replace(' ','') == province.upper().replace(' ',''): | ||
national.append(dictionary["IATA"]) | ||
except: | ||
continue | ||
return national | ||
def getAirlineName(iata): | ||
with Session() as s: | ||
url = 'https://raw.githubusercontent.com/jpatokal/openflights/master/data/airlines.dat' | ||
r = s.get(url) | ||
csv = (str(r.content).replace('\\r','\n').replace(r'\n','\n').replace("b'",'').replace("'",'').replace(", "," - ").replace('"','')).split('\n') | ||
headings = ['OA Code','Airline Name','x','IATA','ICAO','Callsign','Country','X'] | ||
national = [] | ||
for i in csv: | ||
try: | ||
dictionary = dict(zip(headings,i.split(','))) | ||
if dictionary["IATA"].upper().replace(' ','') == iata.upper().replace(' ',''): | ||
national.append(dictionary["Airline Name"]) | ||
except: | ||
continue | ||
return national | ||
def getRoutesFrom(iata): | ||
with Session() as s: | ||
url = 'https://raw.githubusercontent.com/jpatokal/openflights/master/data/planes.dat' | ||
r = s.get(url) | ||
typecsv = (str(r.content).replace('\\r','\n').replace(r'\n','\n').replace("b'",'').replace("'",'').replace(", "," - ").replace('"','')).split('\n') | ||
url = 'https://raw.githubusercontent.com/jpatokal/openflights/master/data/routes.dat' | ||
r = s.get(url) | ||
csv = (str(r.content).replace('\\r','\n').replace(r'\n','\n').replace("b'",'').replace("'",'').replace(", "," - ").replace('"','')).split('\n') | ||
headings = ['Operator IATA','Airline OA Code','From IATA','From OA Code','To IATA','To OA Code','X','x','aircraft types (separated by space)'] | ||
shortlist = national = [] | ||
for i in csv: | ||
try: | ||
dictionary = dict(zip(headings,i.split(','))) | ||
if dictionary["From IATA"].upper().replace(' ','') == iata.upper().replace(' ',''): | ||
shortlist.append(dictionary) | ||
except: | ||
continue | ||
iataairlines = getAirlinesIATA(iata) | ||
for sk in shortlist: | ||
try: | ||
i = sk | ||
types = i["aircraft types (separated by space)"].split(' ') | ||
thetypes = [] | ||
for j in types: | ||
headings = ['Full Name', 'OA Code'] | ||
for x in typecsv: | ||
try: | ||
dictionary = dict(zip(headings,x.split(','))) | ||
if dictionary["OA Code"].upper().replace(' ','') == j.upper().replace(' ',''): | ||
thetypes.append(dictionary["Full Name"]) | ||
except: | ||
continue | ||
namee = getAirlineName(sk["Operator IATA"]) | ||
i.update({"Airframes" : thetypes, "Airline" : namee}) | ||
del i['aircraft types (separated by space)'] | ||
del i['Operator IATA'] | ||
del i['x'] | ||
del i['X'] | ||
del i['Airline OA Code'] | ||
del i['From OA Code'] | ||
del i['To OA Code'] | ||
national.append(i) | ||
except: | ||
pass | ||
return national | ||
def covidExit(province=False): | ||
if province==False: | ||
province = getCountry() | ||
database = [] | ||
date = str((int(datetime.today().strftime('%d')))) | ||
if len(date)==1: | ||
date = f"0{date}" | ||
month = str((int(datetime.today().strftime('%m')))) | ||
if len(month)==1: | ||
month = f"0{month}" | ||
year = str((int(datetime.today().strftime('%Y')))) | ||
changed = False | ||
c=0 | ||
for iterator in range(1,11): | ||
c+=1 | ||
if changed == False: | ||
diff = iterator | ||
else: | ||
diff = c | ||
thedate = str(int(date)-diff) | ||
if len(thedate)==1: | ||
thedate = f"0{thedate}" | ||
if int(thedate)<=0: | ||
changed = True | ||
month = str((int(datetime.today().strftime('%m')))-1) | ||
if len(month)==1: | ||
month = f"0{month}" | ||
thedate = date = 31 | ||
c = 0 | ||
if int(month)==0: | ||
year = str((int(datetime.today().strftime('%Y')))-1) | ||
month = 12 | ||
data = (getStats(f"{month}-{thedate}-{year}",province)) | ||
database.append(data) | ||
country = '' | ||
for i in database: | ||
if type(i)==dict: | ||
country = i["Country_Region"] | ||
break | ||
countrydet = getPopulation(country) | ||
population = countrydet["population"] | ||
demonym = countrydet["demonym"] | ||
i = 0 | ||
while True: | ||
try: | ||
latest = database[i]["Confirmed"] | ||
seclatest = database[i+1]["Confirmed"] | ||
break | ||
except: | ||
i+=1 | ||
avg = 0 | ||
i = 0 | ||
for i in database: | ||
try: | ||
avg += float(i["Confirmed"]) | ||
except: | ||
pass | ||
print(f'''LATEST # OF COVID-19 CASES : {latest} | ||
SECOND-LATEST # OF COVID-19 CASES : {seclatest} | ||
HIKE IN # OF COVID-19 CASES IN 1 DAY : {abs(float(latest)-float(seclatest))} | ||
AVERAGE # OF COVID-19 CASES FROM PAST 10 DAYS : {avg}''') | ||
availairports = getAirport(province) | ||
if availairports == []: | ||
availairports = getAirport(country) | ||
print('\nAIRPORTS THAT OPERATE IN THE REGION') | ||
for i in availairports: | ||
print(i["airport name"]) | ||
availairlines = getAirlines(country) | ||
print('\nAIRLINES THAT ARE BASED ON THE COUNTRY') | ||
matchairlineavail = [] | ||
for i in availairlines: | ||
matchairlineavail.append(i["Airline Name"]) | ||
print(i["Airline Name"]) | ||
availroutes = [] | ||
for i in availairports: | ||
availroutes.append(getRoutesFrom(i["IATA"])) | ||
print(f'\n{demonym.upper()} AIRLINES DEPARTING FROM THE REGION/COUNTRY') | ||
for i in availroutes[0]: | ||
try: | ||
for tryline in i['Airline']: | ||
if tryline in matchairlineavail: | ||
break | ||
print(f"{tryline} from {i['From IATA']} to {i['To IATA']} in aircraft(s) {','.join(i['Airframes'])}") | ||
except: | ||
continue | ||
print(f'\nAIRLINES DEPARTING FROM THE REGION/COUNTRY') | ||
for i in availroutes[0]: | ||
try: | ||
print(f"{i['Airline'][0]} from {i['From IATA']} to {i['To IATA']} in aircraft(s) {','.join(i['Airframes'])}") | ||
except: | ||
continue | ||
|
||
|
||
covidExit('Oman') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.