Skip to content

Instantly share code, notes, and snippets.

@chespinoza
Created December 1, 2021 00:06
Show Gist options
  • Save chespinoza/ad71505223c2d793e29340c9c08574eb to your computer and use it in GitHub Desktop.
Save chespinoza/ad71505223c2d793e29340c9c08574eb to your computer and use it in GitHub Desktop.
Python script for fetching openweathermap data for polybar
#!/usr/bin/env python
# Author: [email protected] @2021
import argparse
import urllib.request as request
import urllib.parse as parse
import json
import subprocess
ICON_MAP = {
"01d": "",
"01n": "",
"02d": "",
"02n": "",
"03d": "",
"03n": ":",
"04d": "",
"04n": "",
"09d": "",
"09n": "",
"10d": "",
"10n": "",
"11d": "",
"11n": "",
"13d": "",
"13n": "",
"50d": "",
"50n": "",
}
URL = "http://api.openweathermap.org/data/2.5/weather?q={}&appid={}&units={}"
API_ID = ""
CITY = parse.quote_plus("London")
UNITS = "metric"
SHORT_FMT = "{} {} {}%"
LONG_FMT = """
City: {}
Weather: {} {}
Temp: {}
Feels like: {}
Min: {}
Max: {}
Pressure: {}hPa
Humidity: {}%
Wind:
Speed: {}m/s
Deg: {}°
Gust: {}m/s
"""
def main():
parser = argparse.ArgumentParser(description="OpenWeatherMap data for polybar")
parser.add_argument("--long", action="store_true")
args = parser.parse_args()
with request.urlopen(URL.format(CITY, API_ID, UNITS)) as response:
data = json.loads(response.read())
main = data["main"]
temp = int(main["temp"])
humidity = main["humidity"]
icon_code = data["weather"][0]["icon"]
if not args.long:
print(f"{ICON_MAP[icon_code]} {temp} {humidity}%")
return
long_fmt = LONG_FMT.format(
data["name"],
ICON_MAP[icon_code],
data["weather"][0]["description"],
main["temp"],
main["feels_like"],
main["temp_min"],
main["temp_max"],
main["pressure"],
main["humidity"],
data["wind"]["speed"],
data["wind"]["deg"],
data["wind"].get("gust"),
)
subprocess.run(["notify-send", "Weater", long_fmt, "-t", "30000"])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment