-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.sh
More file actions
executable file
·116 lines (92 loc) · 3.33 KB
/
init.sh
File metadata and controls
executable file
·116 lines (92 loc) · 3.33 KB
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
#!/usr/bin/env bash
# --------------------------------------------------
# Conky Weather Initialization Script
# --------------------------------------------------
set -e
CONFIG_DIR="$HOME/.config/conky"
CACHE_DIR="$HOME/.cache"
SECRETS_FILE="$CONFIG_DIR/secrets.conf"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
mkdir -p "$CONFIG_DIR" "$CACHE_DIR"
echo "[Conky] Initializing weather cache..."
# --------------------------------------------------
# Check secrets file
# --------------------------------------------------
if [ ! -f "$SECRETS_FILE" ]; then
echo "[Conky] Missing secrets file: $SECRETS_FILE"
echo "[Conky] Creating example..."
cat > "$SECRETS_FILE" <<EOF
# OpenWeatherMap configuration
OWM_APPID=YOUR_API_KEY_HERE
OWM_CITY=Joinville,BR
EOF
chmod 600 "$SECRETS_FILE"
echo "[Conky] Please edit the file and insert your API key:"
echo " nano $SECRETS_FILE"
exit 2
fi
# --------------------------------------------------
# Load secrets
# --------------------------------------------------
# shellcheck disable=SC1090
. "$SECRETS_FILE"
# --------------------------------------------------
# Validate variables
# --------------------------------------------------
if [ -z "$OWM_APPID" ] || [ "$OWM_APPID" = "YOUR_API_KEY_HERE" ]; then
echo "[Conky] Weather not configured yet (missing API key)"
exit 2
fi
if [ -z "$OWM_CITY" ]; then
echo "[Conky] Missing OWM_CITY in $SECRETS_FILE"
exit 2
fi
# --------------------------------------------------
# Check dependencies
# --------------------------------------------------
if ! command -v xmllint >/dev/null; then
echo "[Conky] Missing xmllint (install libxml2-utils)"
exit 1
fi
if ! command -v curl >/dev/null; then
echo "[Conky] Missing curl"
exit 1
fi
# --------------------------------------------------
# Fetch weather data safely
# --------------------------------------------------
echo "[Conky] Fetching weather for: $OWM_CITY"
CURRENT_DATA=$(curl -s "https://api.openweathermap.org/data/2.5/weather?q=${OWM_CITY}&units=metric&lang=pt&mode=xml&appid=${OWM_APPID}")
FORECAST_DATA=$(curl -s "https://api.openweathermap.org/data/2.5/forecast?q=${OWM_CITY}&units=metric&lang=pt&mode=xml&appid=${OWM_APPID}")
if echo "$CURRENT_DATA" | grep -q "<current>"; then
echo "$CURRENT_DATA" | xmllint --format - > "$CACHE_DIR/weather_current.xml"
else
echo "[Conky] Failed to fetch current weather data."
echo "[Conky] Tip: Use City,CountryCode (e.g., Joinville,BR)"
exit 1
fi
if echo "$FORECAST_DATA" | grep -q "<weatherdata>"; then
echo "$FORECAST_DATA" | xmllint --format - > "$CACHE_DIR/weather.xml"
else
echo "[Conky] Failed to fetch forecast data."
exit 1
fi
echo "[Conky] Weather XML cache initialized successfully."
# --------------------------------------------------
# Generate initial assets
# --------------------------------------------------
echo "[Conky] Generating initial weather assets..."
for cmd in img weather-1 weather-2 weather-3; do
bash "$SCRIPT_DIR/time.sh" "$cmd" 2>/dev/null || true
done
# Moon (optional)
bash "$SCRIPT_DIR/GetMoon.sh" >/dev/null 2>&1 || true
# --------------------------------------------------
# Final check
# --------------------------------------------------
if [ -f "$CACHE_DIR/weather.png" ]; then
echo "[Conky] Initialization completed successfully."
else
echo "[Conky] Warning: weather images not generated yet."
fi
exit 0