summaryrefslogtreecommitdiff
path: root/mac/.config/sketchybar/plugins/weather.sh
blob: f4085088da4c87d1c4e299f9c6649b345b6b27a4 (plain)
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
#!/usr/bin/env bash

# Source colors for consistent theming
source "$CONFIG_DIR/colors.sh"

# Cache file for weather data
CACHE_FILE="/tmp/sketchybar_weather_cache"
CACHE_DURATION=1800 # 30 minutes

# Function to get weather data
get_weather() {
  # You can replace this with your preferred weather API
  # For now, using a simple placeholder
  local location="San Francisco" # Change to your location

  # Try to get weather from wttr.in (simple text-based weather)
  local weather_data=$(curl -s "wttr.in/$location?format=3" 2>/dev/null)

  if [ -n "$weather_data" ]; then
    echo "$weather_data"
  else
    echo "Weather unavailable"
  fi
}

# Check if cache is valid
if [ -f "$CACHE_FILE" ]; then
  cache_time=$(stat -f %m "$CACHE_FILE" 2>/dev/null || echo 0)
  current_time=$(date +%s)

  if [ $((current_time - cache_time)) -lt $CACHE_DURATION ]; then
    weather=$(cat "$CACHE_FILE")
  else
    weather=$(get_weather)
    echo "$weather" >"$CACHE_FILE"
  fi
else
  weather=$(get_weather)
  echo "$weather" >"$CACHE_FILE"
fi

# Extract temperature and condition
if [[ "$weather" =~ ([0-9.-]+°[CF]) ]]; then
  temp="${BASH_REMATCH[1]}"
  sketchybar --set "$NAME" label="$temp" \
    icon.color=$BLUE \
    label.color=$WHITE
else
  sketchybar --set "$NAME" label="N/A" \
    icon.color=$GREY \
    label.color=$GREY
fi