Created
April 14, 2025 17:49
-
-
Save artursapek/3ebc5a5fd442a32dbc79a543454c449a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
CACHE_FILE="$HOME/.config/outside.json" | |
CACHE_DIR="$(dirname "$CACHE_FILE")" | |
CACHE_MAX_AGE=600 # 10 minutes in seconds | |
# Create cache directory if it doesn't exist | |
mkdir -p "$CACHE_DIR" | |
current_time=$(date +%s) | |
# Check if cache file exists and is not older than 10 minutes | |
if [ -f "$CACHE_FILE" ]; then | |
cache_timestamp=$(jq -r '.timestamp // 0' "$CACHE_FILE") | |
cache_age=$((current_time - cache_timestamp)) | |
if [ $cache_age -lt $CACHE_MAX_AGE ]; then | |
# Cache is still valid, use cached temperature | |
temp=$(jq -r '.temperature' "$CACHE_FILE") | |
echo "${temp}°F" | |
exit 0 | |
fi | |
fi | |
# Cache is invalid or doesn't exist, fetch new data | |
latlng=$(curl "https://ipinfo.io/json" --silent | jq ".loc" | sed 's/"//g') | |
weather=$(curl "https://api.openweathermap.org/data/2.5/weather?units=imperial&lat=$(echo $latlng | cut -d "," -f 1)&lon=$(echo $latlng | cut -d "," -f 2)&appid=$API_KEY" --silent) | |
temp=$(echo "$weather" | jq ".main.feels_like" | cut -d "." -f 1) | |
# Save to cache with timestamp | |
jq -n --arg temp "$temp" --arg timestamp "$current_time" '{"temperature": $temp, "timestamp": $timestamp|tonumber}' > "$CACHE_FILE" | |
echo "${temp}°F" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment