· 2 years ago · Sep 05, 2023, 10:35 AM
1#!/usr/bin/env sh
2### Openweather script [can save anywhere in your home dir OR /usr/local/bin if you wish to have executable]
3### USAGE: openweather OR openweather -i if you wish to have weather icons]
4## save this script and change permissions [chmod a+x /path/to/script OR sudo chmod a+x /usr/local/bin/openweather]
5
6
7# I take this script from Anachron's i3blocks
8# I only slightly modify this script to add an option to show icon, useful for my tint2 executor
9# 'weather -i' = with icon, 'weather' = text only
10# Cheers!
11# Addy
12# credit to contributors as listed above; i have just done mod to check connection before function openweather runs..
13
14# check if internet is available and server has access
15connection()
16{
17# using ping to check if net is active
18server="8.8.8.8" # Google dns
19checkping="ping -c 1 $server > /dev/null 2>&1" # ping Google to check if iNet is live
20
21$checkping
22while [ $? -ne 0 ]; do
23# notify-send "\e[1A\e[K $(date): Connecting - ${serverAdr}"
24 notify-send "Connecting - ${server}";
25 sleep 1;
26 $checkping > /dev/null 2>&1;
27done
28echo "live" > /tmp/inet && notify-send " iNet is reachable
29Weather is updating... "
30}
31
32weather() {
33# Open Weather Map API code, register to http://openweathermap.org/api to get one ;)
34
35API_KEY="ENTER YOUR API KEY HERE" # user API KEY
36
37# Check on http://openweathermap.org/find & edit in the CITY_ID"
38CITY_ID="XXXXXXX"
39
40# i have used nerd fonts for glyphs ; Use Meteocons for weather glyphs for default & reverse comments
41
42URGENT_LOWER=9
43URGENT_HIGHER=38
44
45#ICON_SUNNY="B Clear"
46ICON_SUNNY=" Clear"
47#ICON_CLOUDY="Y Cloudy"
48ICON_CLOUDY=" Cloudy"
49#ICON_RAINY="X Rainy"
50ICON_RAINY=" Rainy"
51#ICON_STORM="F Storm"
52ICON_STORM="Storm"
53#ICON_SNOW="L Snow"
54ICON_SNOW=" Snow"
55#ICON_FOG="E Fog"
56ICON_FOG=" Fog"
57#ICON_MISC="H "
58ICON_MISC=" "
59#ICON_MISC="⛅️"
60
61TEXT_RAINY="Rainy"
62TEXT_STORM="Storm"
63TEXT_SNOW="Snow"
64TEXT_FOG="Fog"
65
66SYMBOL_CELSIUS="˚C"
67
68WEATHER_URL="https://api.openweathermap.org/data/2.5/weather?id=7279741&appid=${API_KEY}&units=metric"
69
70WEATHER_INFO=$(wget -qO- "${WEATHER_URL}")
71WEATHER_MAIN=$(echo "${WEATHER_INFO}" | grep -o -e '"main":"[a-Z]*"' | awk -F ':' '{print $2}' | tr -d '"')
72WEATHER_TEMP=$(echo "${WEATHER_INFO}" | grep -o -e '"temp":-\?[0-9]*' | awk -F ':' '{print $2}' | tr -d '"')
73
74if [[ "${WEATHER_MAIN}" = *Snow* ]]; then
75if [[ $1 = "-i" ]]; then
76 echo "${ICON_SNOW} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
77else
78 echo "${TEXT_SNOW} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
79fi
80elif [[ "${WEATHER_MAIN}" = *Rain* ]] || [[ "${WEATHER_MAIN}" = *Drizzle* ]]; then
81if [[ $1 = "-i" ]]; then
82 echo "${ICON_RAINY} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
83else
84 echo "${TEXT_RAINY} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
85fi
86elif [[ "${WEATHER_MAIN}" = *Cloud* ]]; then
87if [[ $1 = "-i" ]]; then
88 echo "${ICON_CLOUDY} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
89else
90 echo "${TEXT_CLOUDY} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
91fi
92elif [[ "${WEATHER_MAIN}" = *Clear* ]]; then
93if [[ $1 = "-i" ]]; then
94 echo "${ICON_SUNNY} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
95else
96 echo "${TEXT_SUNNY} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
97fi
98elif [[ "${WEATHER_MAIN}" = *Fog* ]] || [[ "${WEATHER_MAIN}" = *Mist* ]]; then
99if [[ $1 = "-i" ]]; then
100 echo "${ICON_FOG} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
101else
102 echo "${TEXT_FOG} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
103fi
104else
105if [[ $1 = "-i" ]]; then
106 echo "${ICON_MISC} ${WEATHER_MAIN} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
107else
108 echo "${WEATHER_MAIN} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
109fi
110fi
111
112if [[ "${WEATHER_TEMP}" -lt "${URGENT_LOWER}" ]] || [[ "${WEATHER_TEMP}" -gt "${URGENT_HIGHER}" ]]; then
113 exit 33
114fi
115}
116connection &&
117weather