added get_forecast on right click in statusbar for weather.py

This commit is contained in:
simon 2021-02-07 15:57:44 +07:00
parent d864b91341
commit 7a43294f7f
2 changed files with 39 additions and 3 deletions

View File

@ -55,6 +55,7 @@ A bunch of simple bash scripts to be called via i3blocks.
Standalone script that pulls weather data from [openweathermap.org](https://openweathermap.org/) and prints out Standalone script that pulls weather data from [openweathermap.org](https://openweathermap.org/) and prints out
current temperature and weather. Meant to be used from i3blocks. current temperature and weather. Meant to be used from i3blocks.
Icons used to display the current weather condition are from [fontawesome.com](https://fontawesome.com/). Icons used to display the current weather condition are from [fontawesome.com](https://fontawesome.com/).
* on left click: Get three days forecast, use `notify-send` to send message, works best with *dunst* setup.
### Config: ### Config:
Get your free openweathermap API key from [here](https://home.openweathermap.org/api_keys). Get your free openweathermap API key from [here](https://home.openweathermap.org/api_keys).

View File

@ -4,7 +4,9 @@
import os import os
import sys import sys
import configparser import configparser
import subprocess
from time import sleep from time import sleep
from datetime import datetime
import requests import requests
@ -69,11 +71,35 @@ def get_data(api_key, lat, lon):
return celsius_pretty, icon return celsius_pretty, icon
def get_forecast(api_key, lat, lon):
""" get next three days forecast """
# get data
url = "https://api.openweathermap.org/data/2.5/onecall?lat=" + lat + "&lon=" + lon \
+ "&units=metric&exclude=current,minutely,hourly,alerts&appid=" + api_key
response = requests.get(url, timeout=5)
json = response.json()
notify_list = []
# loop three days
for i in json['daily'][1:4]:
timestamp = i['dt']
date_clean = datetime.fromtimestamp(timestamp).strftime("%a %Y-%m-%d")
min_temp = i['temp']['min']
max_temp = i['temp']['max']
weather = i['weather'][0]['main']
weather_desc = i['weather'][0]['description']
icon_id = i['weather'][0]['icon']
icon = iconlist.get(icon_id)
first_line = f'{date_clean} {icon} {weather}'
second_line = f'min: {min_temp} max: {max_temp}, {weather_desc}\n'
notify_list.append(first_line)
notify_list.append(second_line)
# output with notify-send
message = "\n".join(notify_list)
subprocess.call(['notify-send', 'Three days forecast:', message])
def main(): def main():
""" main function to run """ """ main function to run """
# get config file path relative to script file
config_path = os.path.dirname(sys.argv[0]) + '/config'
api_key, lat, lon = get_config(config_path)
# make the call # make the call
celsius_pretty, icon = get_data(api_key, lat, lon) celsius_pretty, icon = get_data(api_key, lat, lon)
# print three lines for i3blocks # print three lines for i3blocks
@ -84,4 +110,13 @@ def main():
# start from here # start from here
if __name__ == '__main__': if __name__ == '__main__':
# get config file path relative to script file
config_path = os.path.dirname(sys.argv[0]) + '/config'
api_key, lat, lon = get_config(config_path)
# check for button clicked
env = os.environ.copy()
button = env.get('BLOCK_BUTTON', False)
if button == '1':
get_forecast(api_key, lat, lon)
# regular
main() main()