Snippets

Andrew My notes are in doc strings. Great write up!

Created by Andrew
import requests

API_KEY = "d3244198490a7673f8fc7d2a955a8f25"

'''
Takes 10 mins for API Key to work, wasnt working for me and found this comment:
https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/issues/46#issuecomment-307685076
Might mention that in the post. 
'''

city_name = "Houston"

url = f"http://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={API_KEY}"
response = requests.get(url).json()
print(response)
# {'coord': {'lon': -95.37, 'lat': 29.76}, 'weather': [{'id': 501, 'main': 'Rain', 'description': 'moderate rain', 'icon': '10n'}], 'base': 'stations', 'main': {'temp': 285.01, 'pressure': 1015, 'humidity': 93, 'temp_min': 283.15, 'temp_max': 287.59}, 'visibility': 16093, 'wind': {'speed': 6.7, 'deg': 340, 'gust': 11.8}, 'rain': {'1h': 1.56}, 'clouds': {'all': 90}, 'dt': 1572002284, 'sys': {'type': 1, 'id': 5828, 'country': 'US', 'sunrise': 1572006578, 'sunset': 1572046874}, 'timezone': -18000, 'id': 4699066, 'name': 'Houston', 'cod': 200}

'''
Can you show us how to make ^ the output look like json as you have displayed it? I know you can curl and jq it, how can we print it that way? I believe it was like indent = 4 
'''

def convert_kelvin_to_celsius(kelvin_temperature):
    return round(kelvin_temperature - 273.15, 2)

current_temperature_kelvin = response['main']['temp']

current_temperature_celsius = convert_kelvin_to_celsius(current_temperature_kelvin)
print(current_temperature_celsius)

'''
Celsius means squat to me :P So i converted it to Fahrenheit... Americans
Might consider adding this but no worries if not :)
'''

def convert_celsius_to_Fahrenheit():
    celsius = float(current_temperature_celsius)
    Fahrenheit = ((9 * celsius) / 5) + 32
    print (Fahrenheit, "Degree Fahrenheit")


convert_celsius_to_Fahrenheit()

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.