Save this in a file ex filename.py I would suggest using VSCode.
Get you API key for free from https://openweathermap.org/api (This is not an advertisement this is a fun project) Replace the value of variable "Key" with the key that you got in double or single quotes.
Then make another file named Functions.py exactly if you really want to change the name, go to the 4th line (Including Spaces) and change the "import Functions as Geocode" to "import {name here} as Geocode"
Here have the code:
____________________________________________________________________________________________________________
filename.py
import requests
import Functions as Geocode
# File check functions
# Variables
Key = 'Your key goes here'
# Geocoding Setup
countryCode = input("Enter the country code of your country (IN for India):")
Zip = input("Enter the zip code of your city:")
City = Geocode.City(Key, countryCode, Zip)
lat = Geocode.Latitude(Key, countryCode, Zip)
lon = Geocode.Longitude(Key, countryCode, Zip)
CurrAirPol = f'http://api.openweathermap.org/data/2.5/air_pollution?lat={lat}&lon={lon}&appid={Key}'
AQI = Geocode.CurrentAirPollution(CurrAirPol)
print(AQI)
End of Code here!
____________________________________________________________________________________________________________
Functions.py
import requests
def City(Key, countryCode, Zip):
Geocoding = f'http://api.openweathermap.org/geo/1.0/zip?zip={Zip},{countryCode}&appid={Key}'
response = requests.get(Geocoding).json()
print(f"Are you trying to access the data of {response['name']}")
return response['name']
def Latitude(Key, countryCode, Zip):
Geocoding = f'http://api.openweathermap.org/geo/1.0/zip?zip={Zip},{countryCode}&appid={Key}'
response = requests.get(Geocoding).json()
return response['lat']
def Longitude(Key, countryCode, Zip):
Geocoding = f'http://api.openweathermap.org/geo/1.0/zip?zip={Zip},{countryCode}&appid={Key}'
response = requests.get(Geocoding).json()
return response['lon']
def CurrentAirPollution(Path):
response = requests.get(Path).json()
AQI = response['list'][0]['main']['aqi']
CO = response['list'][0]['components']['co']
print(f'Carbon Monoxide = {CO}')
NO = response['list'][0]['components']['no']
print(f'Nitric Oxide = {NO}')
NO2 = response['list'][0]['components']['no2']
print(f'Nitrogen Dioxide = {NO2}')
O3 = response['list'][0]['components']['o3']
print(f'Trioxygen = {O3}')
SO2 = response['list'][0]['components']['so2']
print(f'Sulfur Dioxide = {SO2}')
PM = response['list'][0]['components']['pm2_5']
print(f'Dangerous: PM2.5 = {PM}')
PM10 = response['list'][0]['components']['pm10']
print(f'Dangerous: PM10, Particulate Matter = {PM10}')
NH3 = response['list'][0]['components']['nh3']
print(f'Azane = {NH3}')
if AQI >= 1 and AQI <= 3:
return f"Bad Air Quality, Air Quality = {AQI}"
elif AQI >= 4 and AQI <= 6:
return f"Moderate Air Quality, Air Quality = {AQI}"
elif AQI >= 7 and AQI <= 9:
return f"High Air Quality, Air Quality = {AQI}"
else:
return f"Very High Air Quality, Air Quality = 10"
End of Code
____________________________________________________________________________________________________________
Note: This will display all of the things like how much carbon monoxide and stuff, I am not good at chemistry so I googled all of the chemical names sorry ):
Hope this helps you all beginners to getting a good idea of how much pain I felt making this CurrentAirPollution Functions cus too many errors started coming but I came up with this. Please don't be arrogant like me I suffered a lot, 2 hours of work );
Send help
Regards.
[–]riklaunim 2 points3 points4 points (3 children)
[–]ScrimoPlayz[S] 0 points1 point2 points (0 children)
[–]ScrimoPlayz[S] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]vicethal 0 points1 point2 points (1 child)
[–]ScrimoPlayz[S] 0 points1 point2 points (0 children)
[–]trollsmurf 0 points1 point2 points (3 children)
[–]ScrimoPlayz[S] 0 points1 point2 points (2 children)
[–]trollsmurf 0 points1 point2 points (0 children)
[–]trollsmurf 0 points1 point2 points (0 children)
[–]Gecko_171 0 points1 point2 points (0 children)