Skip to content

Commit

Permalink
Sync from master: Support wallpaper from Unsplash #395
Browse files Browse the repository at this point in the history
- thanks to @MkQtS @jjm2473
  • Loading branch information
SpeedPartner committed Apr 15, 2023
1 parent 49e3b77 commit 03c3972
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 79 deletions.
55 changes: 28 additions & 27 deletions luasrc/view/themes/argon/sysauth.htm
Expand Up @@ -23,15 +23,15 @@

<%+header_login%>
<%
local util = require "luci.util"
local fs = require "nixio.fs"
local nutil = require "nixio.util"
local sys = require "luci.sys"
local json = require "luci.jsonc"
local uci = require 'luci.model.uci'.cursor()
local util = require "luci.util"
local fs = require "nixio.fs"
local nutil = require "nixio.util"
local json = require "luci.jsonc"
local sys = require "luci.sys"
local uci = require 'luci.model.uci'.cursor()

-- Fetch Local Background Media

local function glob(...)
local iter, code, msg = fs.glob(...)
if iter then
Expand All @@ -45,9 +45,9 @@
local imageTypes = " jpg png gif webp "
local videoTypes = " mp4 webm "
local allTypes = imageTypes .. videoTypes
local function fetchMedia(path,themeDir)
local backgroundTable = {}
local backgroundCount = 0
local function fetchMedia(path, themeDir)
local backgroundTable = {}
local backgroundCount = 0
for i, f in ipairs(glob(path)) do
attr = fs.stat(f)
if attr then
Expand All @@ -59,43 +59,44 @@
local bg = {}
bg.type = ext
bg.url = themeDir .. fs.basename(f)
table.insert(backgroundTable,bg)
table.insert(backgroundTable, bg)
backgroundCount = backgroundCount + 1
end
end
end
return backgroundTable,backgroundCount
return backgroundTable, backgroundCount
end
local function selectBackground(themeDir)
local bgUrl = media .. "/img/bg1.jpg"
local backgroundType = "Image"
local mimeType = ""
local bgUrl = media .. "/img/bg1.jpg"
local backgroundType = "Image"
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') or (uci:get_first('argon', 'global', 'bing_background') == '1' and 'bing')
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

local backgroundTable, backgroundCount = fetchMedia("/www" .. themeDir .. "*",themeDir)
local backgroundTable, backgroundCount = fetchMedia("/www" .. themeDir .. "*", themeDir)
if ( backgroundCount > 0 ) then
local currentBg = backgroundTable[math.random(1,backgroundCount)]
bgUrl = currentBg.url
local currentBg = backgroundTable[math.random(1, backgroundCount)]
bgUrl = currentBg.url
if (string.match(videoTypes, " "..currentBg.type.." ") ~= nil) then
backgroundType = "Video"
mimeType = "video/" .. currentBg.type
backgroundType = "Video"
mimeType = "video/" .. currentBg.type
end
end

return bgUrl,backgroundType,mimeType
return bgUrl, backgroundType, mimeType
end

local boardinfo = util.ubus("system", "board")
local themeDir = media .. "/background/"
local bgUrl,backgroundType,mimeType = selectBackground(themeDir)
local bgUrl, backgroundType, mimeType = selectBackground(themeDir)
%>
<!-- Login Page Start -->
<div class="login-page">
Expand Down Expand Up @@ -129,7 +130,7 @@
<div class="login-form">
<!-- Logo Start -->
<a class="brand" href="/"><img src="<%=media%>/img/argon.svg" class="icon">
<span class="brand-text"><%=striptags( (boardinfo.hostname or "?") .. ( (node and node.title) and ' - ' .. translate(node.title) or '')) %></span>
<span class="brand-text"><%=striptags( (boardinfo.hostname or "?") ) %></span>
</a>
<!-- Logo End -->
<!-- Login Form Start -->
Expand Down
52 changes: 0 additions & 52 deletions root/usr/libexec/argon/bing_wallpaper

This file was deleted.

70 changes: 70 additions & 0 deletions root/usr/libexec/argon/online_wallpaper
@@ -0,0 +1,70 @@
#!/bin/sh
# author jjm2473

# the script will be excuted when `argon.@global[0].bing_background == '1'`
# defaults to 'bing' to be compatible with old config
WEB_PIC_SRC=$(uci -q get argon.@global[0].online_wallpaper || echo 'bing')
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 -fks --max-time 1 \
"https://www.bing.com/HPImageArchive.aspx?format=js&n=1" |
jsonfilter -q -e '@.images[0].url')
[ -n "${picpath}" ] && echo "//www.bing.com${picpath}"
;;
unsplash)
curl -fks --max-time 1 \
"https://source.unsplash.com/1920x1080/daily?wallpapers" |
sed -E 's#^.*href="([^?]+)\?.*$#\1?fm=jpg\&fit=crop\&w=1920\&h=1080#'
;;
unsplash_*)
local collection_id=${WEB_PIC_SRC#unsplash_}
curl -fks --max-time 1 \
"https://source.unsplash.com/collection/${collection_id}/1920x1080" |
sed -E 's#^.*href="([^?]+)\?.*$#\1?fm=jpg\&fit=crop\&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 03c3972

Please sign in to comment.