
Add Weather Data With Caching To Neofetch
- Adam Douglas
The popular and powerful command-line system information BASH script “Neofetch” seems to be everywhere yet for some reason I’ve never used it nor any other variants available today. So I decided one day to play with Neofetch to see what I could do with it. For the most part I’ve kept with what is built in, but I wanted to display weather data by limiting the impact of Neofetch’s load time and not wasting unnecessary bandwidth. You see most weather data providers that I know of only update the forecast weather data every hour. Therefore, I figured I could cache the weather data to avoid fetching weather data every time Neofetch is run.
Environment
Tested with the following…
- Arch Linux x86_64
- curl v7.80.0
- Fish v3.3.1
- GNU bash v5.1.8
- Neofetch v7.1.0
Fetching Weather Data
I first began playing around with obtaining the weather data with cURL using the service wttr.in and shortly figured out how to do a simple one-line output as I desired.
$ curl -s "https://wttr.in/Example?format=%l:+%t+(%f)+%c+%C+%w+%h+%T"
Example: +22°C (+25°C) ☁️ Smoke →7km/h 88% 02:19:37+0500
%l Location name
%t Temperature (actual)
%f Temperature (feels like)
%c Weather condition
%C Weather condition textual name
%w Wind
%h Humidity
%T Current time
Script
With fetching weather data figured out I then moved on to learning how I would implement this into Neofetch’s configuration file. The configuration file by default is located at “~/.config/neofetch/config.conf”. Looking at the configuration file I soon noticed that this file is written in BASH and is imported into Neofetch’s BASH script using the built-in command “.” (source). So after some testing I quickly came up with the below BASH script code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
getWeather() {
weather_params="Example?format=%l:+%t+(%f)+%c+%C+%w+%h+%T"
data_path="$HOME/.config/neofetch/data-weather.txt"
duration=3600
if [[ -e "$data_path" ]]; then
weather_date="$(stat -c '%Y' $data_path)"
current_date="$(date +%s)"
date_diff=$(($current_date - $weather_date))
if [ $date_diff -ge $duration ]; then
getWeatherData
setWeatherData
else
setWeatherData
fi
else
getWeatherData
setWeatherData
fi
showWeatherData
}
getWeatherData() {
curl -s "https://wttr.in/$weather_params" > "$data_path"
}
setWeatherData() {
weather_data="$(cat $data_path)"
}
showWeatherData() {
prin "Weather" "$weather_data"
}
The settings are easily controlled by alternating 3 variables.
- weather_params
- wttr.in parameters.
- Variable value should be enclosed in double quotes.
- data_path
- The output weather data directory and filename.
- Variable value should be enclosed in double quotes.
- duration
- Interval to fetch weather data.
- Represents a measurement in seconds (integer).
- Do not enclose the variable value in quotes.
Example Output
Neofetch Config File
Here the complete setup done within the Neofetch configuration file “~/.config/neofetch/config.conf”.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
print_info() {
info title
info underline
info "OS" distro
info "Host" model
info "Kernel" kernel
info "Uptime" uptime
info "Packages" packages
info "Shell" shell
info "Resolution" resolution
info "DE" de
info "WM" wm
info "WM Theme" wm_theme
info "Theme" theme
info "Icons" icons
info "Terminal" term
info "Terminal Font" term_font
info "CPU" cpu
info "GPU" gpu
info "Memory" memory
info "GPU Driver" gpu_driver # Linux/macOS only
info "CPU Usage" cpu_usage
info "Disk" disk
info "Local IP" local_ip
info "Public IP" public_ip
info "Locale" locale # This only works on glibc systems.
getWeather
info cols
}
getWeather() {
weather_params="Example?format=%l:+%t+(%f)+%c+%C+%w+%h+%T"
data_path="$HOME/.config/neofetch/data-weather.txt"
duration=3600
if [[ -e "$data_path" ]]; then
weather_date="$(stat -c '%Y' $data_path)"
current_date="$(date +%s)"
date_diff=$(($current_date - $weather_date))
if [ $date_diff -ge $duration ]; then
getWeatherData
setWeatherData
else
setWeatherData
fi
else
getWeatherData
setWeatherData
fi
showWeatherData
}
getWeatherData() {
curl -s "https://wttr.in/$weather_params" > "$data_path"
}
setWeatherData() {
weather_data="$(cat $data_path)"
}
showWeatherData() {
prin "Weather" "$weather_data"
}
title_fqdn="off"
kernel_shorthand="on"
distro_shorthand="off"
os_arch="on"
uptime_shorthand="on"
memory_percent="on"
memory_unit="gib"
package_managers="on"
shell_path="off"
shell_version="on"
speed_type="bios_limit"
speed_shorthand="off"
cpu_brand="on"
cpu_speed="on"
cpu_cores="logical"
cpu_temp="off"
gpu_brand="on"
gpu_type="all"
refresh_rate="on"
gtk_shorthand="off"
gtk2="on"
gtk3="on"
public_ip_host="https://ident.me"
public_ip_timeout=2
de_version="on"
disk_show=('/' '/boot' '/home' '/srv' '/mnt/data')
disk_subtitle="mount"
disk_percent="on"
music_player="auto"
song_format="%artist% - %album% - %title%"
song_shorthand="off"
mpc_args=()
colors=(distro)
bold="on"
underline_enabled="on"
underline_char="-"
separator=":"
block_range=(0 15)
color_blocks="on"
block_width=3
block_height=1
col_offset="auto"
bar_char_elapsed="-"
bar_char_total="="
bar_border="on"
bar_length=15
bar_color_elapsed="distro"
bar_color_total="distro"
cpu_display="off"
memory_display="off"
battery_display="off"
disk_display="off"
image_backend="kitty"
image_source="ascii"
ascii_distro="auto"
ascii_colors=(distro)
ascii_bold="on"
image_loop="off"
thumbnail_dir="${XDG_CACHE_HOME:-${HOME}/.cache}/thumbnails/neofetch"
crop_mode="normal"
crop_offset="center"
image_size="auto"
gap=3
yoffset=0
xoffset=0
background_color=
stdout="off"
Tip for BASH
Call Neofetch each time a terminal/console is started by adding the command "neofetch" without double quotes to "~/.bashrc".
Tip for FiSH
Call Neofetch each time a terminal/console is started by adding the command "neofetch" without double quotes to "~/.config/fish/config.fish".
I’m publishing this as part of 100 Days To Offload. You can join in yourself by visiting 100DaysToOffload.com.
References
Changelog
-
- change topic from linux to neofetch
-
- adjust title for clarity
- adjust description for clarity
- add old link redirection
-
- Fix link to use https
-
- change topic
- remove tags cache, weather
-
- change topic
-
- fix meta description too short
- fix punctuation and grammar