Files
weather-bg/mood
teitoku d71ffb714f Yikes
2025-07-28 16:31:36 +09:00

73 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
MUSIC_PATH="music"
urlencode() {
echo $1 \
| sed 's#/#\%2F#g' \
| sed 's# #\%20#g' #\
#| tr -d '"'
}
if [[ -z $MOOD_ipinfo ]]; then
export MOOD_ipinfo=$(curl ipinfo.io/json 2>/dev/null)
fi
jqurl() {
echo $(urlencode $(echo $MOOD_ipinfo | jq -r $1))
}
gethour() {
echo $RESP \
| jq -r $1 \
| cut -dT -f2
}
location=$(jqurl '.loc')
timezone=$(jqurl '.timezone')
URL="https://api.open-meteo.com/v1/forecast?current=weather_code&forecast_days=1&daily=sunrise,sunset"
URL="$URL&timezone=$timezone"
URL="$URL&latitude=$(echo $location | cut -d, -f1)"
URL="$URL&longitude=$(echo $location | cut -d, -f2)"
RESP=$(curl $URL 2>/dev/null)
CODE=$(echo $RESP | jq -r '.current.weather_code')
TIME="$(gethour '.current.time')"
SUNRISE="$(gethour '.daily.sunrise[0]')"
SUNSET="$(gethour '.daily.sunset[0]')"
if [[ ( $TIME > $SUNRISE ) && ( $TIME < $SUNSET ) ]]; then
TIME='day'
else
TIME='night'
fi
echo "Obtained weather code: $CODE"
T=""
[ $CODE -ge 0 ] && T=$TIME
[ $CODE -ge 40 ] && T='fog'
[ $CODE -ge 50 ] && T='rain'
[ $CODE -ge 70 ] && T='snow'
[ $CODE -ge 80 ] && T='rain'
[ $CODE -ge 85 ] && T='snow'
[ $CODE -ge 90 ] && T='storm'
CODE=$T
echo "Playing playlist for: $CODE"
echo ""
FILES=$(find $MUSIC_PATH -regex "$MUSIC_PATH/${CODE}_.*" \
| sort -R)
setopt shwordsplit
for line in $FILES; do echo "${line%.*}" | sed "s#^${MUSIC_PATH}/${CODE}_##"; done
unsetopt shwordsplit
killall mpv &>/dev/null 2>/dev/null
$(echo $FILES | mpv --playlist=- </dev/null &>/dev/null 2>/dev/null &)
# disown