需要安装的软件
脚本内容
#!/bin/sh
debug=false
baseDir='/data'
MIRROR='https://mirror7.sgkoi.dev'
# 创建目录
if [ ! -d "$baseDir" ];then
mkdir "$baseDir"
fi
cd "$baseDir"
while :
do
version=$(curl -s "$MIRROR/ModLoader/GetModLoader?name=version")
echo "Current tModLoader version: $version"
echo "$version" > version
if [ "$debug" = true ]; then
echo "curl '$MIRROR/tModLoader/listmods.php?uncompressed=true&modloaderversion=tModLoader%20v$version'"
read
fi
modListStr=$(curl -s "$MIRROR/tModLoader/listmods.php?uncompressed=true&modloaderversion=tModLoader%20v$version")
# 获取上次更新时间
if [ -f "$baseDir/lastSync" ];then
dateStr=$(cat "$baseDir/lastSync")
lastSync=$(date -d "$dateStr" '+%s')
else
lastSync=$(date '+%s')
fi
if [ "$debug" = true ]; then
echo "last: $lastSync"
read
fi
# 解析模组列表
list=$(echo "$modListStr" | jq -r '[.modlist[] | {name: .name, updateTime: .updateTimeStamp}]')
if [ "$debug" = true ]; then
echo "list: $list"
read
fi
size=$(echo "$list" | jq 'length')
index=0
until [ ! $index -lt $size ]; do
name=$(echo $list | jq -r ".[$index].name")
updateTimeStr=$(echo $list | jq -r ".[$index].updateTime")
updateTime=$(date -d "$updateTimeStr" '+%s')
fileName="$baseDir/$name.tmod"
shouldUpdate=false
# 计算距离上一次更新的间隔时间
duration=`expr $lastSync - $updateTime`
# 超过10分钟,标记为需要更新
if [ $duration -lt 600 ];then
echo "[$name] updated!"
shouldUpdate=true
fi
# 如果文件不存在,也标记为需要更新
if [ -f "$fileName" ];then
# file exist
sleep 1
else
echo "[$name] not exist!"
shouldUpdate=true
fi
if [ "$shouldUpdate" = true ]; then
if [ "$debug" = true ]; then
echo "wget -q -O $fileName '$MIRROR/tModLoader/download.php?Down=mods/$name'"
read
fi
wget -q -O "$fileName" "$MIRROR/tModLoader/download.php?Down=mods/$name"
fi
let "index++"
done
now=$(date '+%Y-%m-%d %H:%M:%S')
echo "$now" > "$baseDir/lastSync"
echo "Sync done at $now"
sleep 300
done