Skip to content

Commit

Permalink
Support wallpaper from Unsplash
Browse files Browse the repository at this point in the history
  • Loading branch information
MkQtS authored Apr 10, 2023
1 parent 260dd91 commit 1096675
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 56 deletions.
9 changes: 5 additions & 4 deletions luasrc/view/themes/argon/sysauth.htm
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@
local mimeType = ""

if fs.access("/etc/config/argon") then
if uci:get_first('argon', 'global', 'bing_background') == "1" then
local bing = sys.exec("/usr/libexec/argon/bing_wallpaper")
if (bing and bing ~= '') then
return bing, "Image", ""
local online_wallpaper = uci:get_first('argon', 'global', 'online_wallpaper')
if (online_wallpaper and online_wallpaper ~= "none") then
local picurl = sys.exec("/usr/libexec/argon/online_wallpaper")
if (picurl and picurl ~= '') then
return picurl, "Image", ""
end
end
end
Expand Down
52 changes: 0 additions & 52 deletions root/usr/libexec/argon/bing_wallpaper

This file was deleted.

68 changes: 68 additions & 0 deletions root/usr/libexec/argon/online_wallpaper
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/sh
# author jjm2473

WEB_PIC_SRC=$(uci -q get argon.@global[0].online_wallpaper)
CACHE=/var/run/argon_${WEB_PIC_SRC}.url
WRLOCK=/var/lock/argon_${WEB_PIC_SRC}.lock

fetch_pic_url() {
case $WEB_PIC_SRC in
bing)
local picpath=$(curl --fail --show-error --max-time 1 \
"http://www.bing.com/HPImageArchive.aspx?format=js&n=1" 2>/dev/null |
jsonfilter -q -e '@.images[0].url')
echo "//www.bing.com${picpath}"
;;
unsplash)
curl --fail --show-error --max-time 1 \
"https://source.unsplash.com/random/1920x1080" 2>/dev/null |
sed -E 's#^.*href="([^?]+)\?.*$#\1?fm=jpg\&w=1920\&h=1080#'
;;
unsplash_*)
local collection_id=${WEB_PIC_SRC#unsplash_}
curl --fail --show-error --max-time 1 \
"https://source.unsplash.com/collection/${collection_id}/1920x1080" 2>/dev/null |
sed -E 's#^.*href="([^?]+)\?.*$#\1?fm=jpg\&w=1920\&h=1080#'
;;
esac
}

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}" >"$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

0 comments on commit 1096675

Please sign in to comment.