mirror of
https://github.com/kiddin9/op-packages.git
synced 2026-11-01 16:44:23 +08:00
59 lines
1.3 KiB
Bash
Executable File
59 lines
1.3 KiB
Bash
Executable File
#!/bin/sh
|
|
# author jjm2473, customized for fluent
|
|
|
|
WEB_PIC_SRC=$(uci -q get fluent.@global[0].login_bg || echo 'bing')
|
|
|
|
if [ "$WEB_PIC_SRC" != "bing" ]; then
|
|
exit 0
|
|
fi
|
|
|
|
CACHE=/var/run/fluent_${WEB_PIC_SRC}.url
|
|
WRLOCK=/var/lock/fluent_${WEB_PIC_SRC}.lock
|
|
|
|
fetch_pic_url() {
|
|
local picpath=$(curl -fks --max-time 3 \
|
|
"https://www.bing.com/HPImageArchive.aspx?format=js&n=1" |
|
|
jsonfilter -qe '@.images[0].url')
|
|
[ -n "${picpath}" ] && echo "https://www.bing.com${picpath}"
|
|
}
|
|
|
|
try_update() {
|
|
local lock="$WRLOCK"
|
|
exec 200>$lock
|
|
|
|
if flock -n 200 >/dev/null 2>&1; then
|
|
local picurl=$(fetch_pic_url)
|
|
if [ -n "$picurl" ]; then
|
|
echo "${picurl}" | tee "$CACHE"
|
|
else
|
|
if [ -s "$CACHE" ]; then
|
|
cat "$CACHE"
|
|
else
|
|
touch "$CACHE"
|
|
fi
|
|
fi
|
|
flock -u 200 >/dev/null 2>&1
|
|
elif [ -s "$CACHE" ]; then
|
|
cat "$CACHE"
|
|
fi
|
|
}
|
|
|
|
get_url() {
|
|
if [ -f "$CACHE" ]; then
|
|
local idle_t=$(($(date '+%s') - $(date -r "$CACHE" '+%s' 2>/dev/null || echo '0')))
|
|
if [ -s "$CACHE" ]; then
|
|
if [ $idle_t -le 43200 ]; then
|
|
cat "$CACHE"
|
|
return
|
|
fi
|
|
else
|
|
if [ $idle_t -le 120 ]; then
|
|
return
|
|
fi
|
|
fi
|
|
fi
|
|
try_update
|
|
}
|
|
|
|
get_url
|