Skip to content

Instantly share code, notes, and snippets.

@kraftwerk28
Last active August 8, 2021 12:45
Show Gist options
  • Save kraftwerk28/b1639cd42d218e2b221980d855afe9c2 to your computer and use it in GitHub Desktop.
Save kraftwerk28/b1639cd42d218e2b221980d855afe9c2 to your computer and use it in GitHub Desktop.
Openweather widget for waybar

In order to make it work, supply OPENWEATHER_APP_ID environment variable, which you must get from https://openweathermap.org/ and, optionally, OPENWEATHER_LOCATION, otherwise script will grab your IP-based geolocation using https://ip-api.com.

{
// ...
"modules-right": [
// ...
"custom/openweather",
// ...
],
"custom/openweather": {
"exec": "OPENWEATHER_APP_ID=<app_id> ~/.config/waybar/weather.py",
"on-click": "OPENWEATHER_APP_ID=<app_id> ~/.config/waybar/weather.py open",
"interval": 1800
},
// ...
}
// vim: ft=jsonc: sw=4: et
#!/usr/bin/env python
import requests
import os
import sys
def get_geo():
url = "http://ip-api.com/json"
resp = requests.get(url)
json = resp.json()
return json["lat"], json["lon"]
def get_response(app_id, location=None):
url = "https://api.openweathermap.org/data/2.5/weather"
params = {"appid": app_id, "units": "metric"}
if location is None:
lat, lon = get_geo()
params.update({"lat": lat, "lon": lon})
else:
params["q"] = location
return requests.get(url, params=params)
def get_weather_string(app_id, location=None):
resp = get_response(app_id, location)
data = resp.json()
temperature = int(data["main"]["temp"])
if temperature > 0:
temperature = f"+{temperature}"
else:
temperature = f"{temperature}"
condition = data["weather"][0]["main"]
location_icon = "\uf450 " if location is None else ""
return f"{location_icon}{temperature}°C, {condition}"
def get_browser_url(app_id, location=None):
json = get_response(app_id, location).json()
id = json["id"]
return f"https://openweathermap.org/city/{id}"
if __name__ == "__main__":
if (app_id := os.getenv("OPENWEATHER_APP_ID")) is None:
print("OPENWEATHER_APP_ID is not defined")
sys.exit(1)
try:
location = os.getenv("OPENWEATHER_LOCATION")
if len(sys.argv) >= 2 and sys.argv[1] == "open":
os.system(f"xdg-open {get_browser_url(app_id, location)}")
else:
print(get_weather_string(app_id, location))
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment