mirror of
https://github.com/gensokyo-zone/infrastructure.git
synced 2026-02-09 12:29:19 -08:00
Added weather module.
This commit is contained in:
parent
30572115a9
commit
a7833d0d81
5 changed files with 78 additions and 3 deletions
18
scripts/weather.nix
Normal file
18
scripts/weather.nix
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
{ pkgs ? import <nixpkgs> {} }:
|
||||
|
||||
with pkgs;
|
||||
|
||||
mkShell {
|
||||
buildInputs = [
|
||||
pkgs.python3
|
||||
pkgs.python3.pkgs.requests
|
||||
];
|
||||
shellHook = ''
|
||||
# Tells pip to put packages into $PIP_PREFIX instead of the usual locations.
|
||||
# See https://pip.pypa.io/en/stable/user_guide/#environment-variables.
|
||||
export PIP_PREFIX=$(pwd)/_build/pip_packages
|
||||
export PYTHONPATH="$PIP_PREFIX/${pkgs.python3.sitePackages}:$PYTHONPATH"
|
||||
export PATH="$PIP_PREFIX/bin:$PATH"
|
||||
unset SOURCE_DATE_EPOCH
|
||||
'';
|
||||
}
|
||||
48
scripts/weather.py
Normal file
48
scripts/weather.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import requests
|
||||
import json
|
||||
import sys
|
||||
|
||||
#city = "London"
|
||||
#api_key = "08588263e133e79c17cef6d01a5c6da8"
|
||||
city = sys.argv[1]
|
||||
api_key = sys.argv[2]
|
||||
|
||||
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
|
||||
|
||||
weather_icons = {
|
||||
"Thunderstorm": "摒",
|
||||
"Clouds": "摒",
|
||||
"Drizzle": "",
|
||||
"Rain": "",
|
||||
"Snow": "流",
|
||||
"Clear": "滛",
|
||||
"Mist": "",
|
||||
"Smoke": "",
|
||||
"Haze": "",
|
||||
"Dust": "",
|
||||
"Fog": "",
|
||||
"Sand": "",
|
||||
"Ash": "",
|
||||
"Squall": "",
|
||||
"Tornado": ""
|
||||
}
|
||||
|
||||
def degrees_to_cardinal(d):
|
||||
dirs = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
|
||||
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"]
|
||||
ix = int((d + 11.25)/22.5)
|
||||
return dirs[ix % 16]
|
||||
|
||||
response = requests.get(url)
|
||||
data = json.loads(response.text)
|
||||
#print(data)
|
||||
|
||||
condition = data["weather"][0]["main"]
|
||||
weather_icon = weather_icons[condition]
|
||||
temperature = f"{data['main']['temp']}°C"
|
||||
feels_like_temperature = f"{data['main']['feels_like']}°C"
|
||||
humidity = f"{data['main']['humidity']}%"
|
||||
wind = f"{data['wind']['speed']}m/s {degrees_to_cardinal(data['wind']['deg'])}"
|
||||
|
||||
end_string = f"{weather_icon} {temperature} {feels_like_temperature} {humidity} {wind}"
|
||||
print(end_string)
|
||||
Loading…
Add table
Add a link
Reference in a new issue