Created
January 18, 2021 00:06
-
-
Save bergpb/269f628cde0cb0cdf6369a4f57a3a7bb to your computer and use it in GitHub Desktop.
Uses the OpenWeatherMap to get an emoji based on the current weather // OpenWeaterMap emoji matcher in python.
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
# Based on ruby version: https://gist.github.com/michels/90327b8d284646a238e6 | |
import requests | |
OPENWEATHER_APIKEY = "" # TODO add your apikey here | |
CITY = "Fortaleza" | |
def get_weather_emoji(weather_id): | |
# Openweathermap Weather codes and corressponding emojis | |
thunderstorm = "💨" # Code: 200's Thunderstorm | |
drizzle = "💧" # Code: 300's Drizzle | |
rain = "☔" # Code: 500's Rain | |
snowflake = "❄" # Code: 600's Snow | |
atmosphere = "🌁" # Code: 700's Atmosphere | |
clear_sky = "☀️" # Code: 800 Clear | |
few_clouds = "⛅" # Code: 801 sun behind clouds | |
clouds = "☁️" # Code: 802-803-804 clouds general | |
default_emoji = "🌀" # default emojis | |
weather_id = str(weather_id) | |
if weather_id[0] == '2': | |
return thunderstorm | |
elif weather_id[0] == '3': | |
return drizzle | |
elif weather_id[0] == '5': | |
return rain | |
elif weather_id[0] == '6': | |
return snowflake | |
elif weather_id[0] == '7': | |
return atmosphere | |
elif weather_id == '800': | |
return clear_sky | |
elif weather_id == '801': | |
return few_clouds | |
elif weather_id in ("802", "803", "804"): | |
return clouds | |
else: | |
return default_emoji | |
weather_url = f"http://api.openweathermap.org/data/2.5/weather?q={CITY}&units=metric&appid={OPENWEATHER_APIKEY}" | |
weather = requests.get(weather_url).json() | |
print(weather['weather'][0]['description'] + ' ' + get_weather_emoji(weather['weather'][0]['id'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment