mirror of
https://github.com/whyour/qinglong.git
synced 2026-06-30 20:35:09 +08:00
增加git_diy脚本,支持自定义拉取仓库
This commit is contained in:
Executable
+143
@@ -0,0 +1,143 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
## 路径、环境判断
|
||||
ShellDir=${JD_DIR:-$(cd $(dirname $0); pwd)}
|
||||
LogDir=${ShellDir}/log
|
||||
ConfigDir=${ShellDir}/config
|
||||
FileConf=${ConfigDir}/config.sh
|
||||
[[ ${ANDROID_RUNTIME_ROOT}${ANDROID_ROOT} ]] && Opt="P" || Opt="E"
|
||||
Tips="从日志中未找到任何互助码"
|
||||
|
||||
## 所有有互助码的活动,只需要把脚本名称去掉前缀 jd_ 后列在 Name1 中,将其中文名称列在 Name2 中,对应 config.sh 中互助码后缀列在 Name3 中即可。
|
||||
## Name1、Name2 和 Name3 中的三个名称必须一一对应。
|
||||
Name1=(fruit pet plantBean dreamFactory jdfactory crazy_joy jdzz jxnc bookshop cash sgmh cfd global)
|
||||
Name2=(东东农场 东东萌宠 京东种豆得豆 京喜工厂 东东工厂 crazyJoy任务 京东赚赚 京喜农场 口袋书店 签到领现金 闪购盲盒 京喜财富岛 环球挑战赛)
|
||||
Name3=(Fruit Pet Bean DreamFactory JdFactory Joy Jdzz Jxnc BookShop Cash Sgmh Cfd Global)
|
||||
|
||||
|
||||
## 导入 config.sh
|
||||
function Import_Conf {
|
||||
if [ -f ${FileConf} ]
|
||||
then
|
||||
. ${FileConf}
|
||||
if [ -z "${Cookie1}" ]; then
|
||||
echo -e "请先在 config.sh 中配置好 Cookie\n"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo -e "配置文件 ${FileConf} 不存在,请先按教程配置好该文件\n"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
## 用户数量 UserSum
|
||||
function Count_UserSum {
|
||||
for ((i=1; i<=1000; i++)); do
|
||||
Tmp=Cookie$i
|
||||
CookieTmp=${!Tmp}
|
||||
[[ ${CookieTmp} ]] && UserSum=$i || break
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
## 导出互助码的通用程序
|
||||
function Cat_Scodes {
|
||||
if [ -d ${LogDir}/jd_$1 ] && [[ $(ls ${LogDir}/jd_$1) != "" ]]; then
|
||||
cd ${LogDir}/jd_$1
|
||||
|
||||
## 导出助力码变量(My)
|
||||
for log in $(ls -r); do
|
||||
case $# in
|
||||
2)
|
||||
codes=$(cat ${log} | grep -${Opt} "开始【京东账号|您的(好友)?助力码为" | uniq | perl -0777 -pe "{s|\*||g; s|开始||g; s|\n您的(好友)?助力码为(:)?:?|:|g; s|,.+||g}" | sed -r "s/【京东账号/My$2/;s/】.*?:/='/;s/】.*?/='/;s/$/'/;s/\(每次运行都变化,不影响\)//")
|
||||
;;
|
||||
3)
|
||||
codes=$(grep -${Opt} $3 ${log} | uniq | sed -r "s/【京东账号/My$2/;s/(.*?】/='/;s/$/'/")
|
||||
;;
|
||||
esac
|
||||
if [[ ${codes} ]]; then
|
||||
## 添加判断,若未找到该用户互助码,则设置为空值
|
||||
for ((user_num=1;user_num<=${UserSum};user_num++)); do
|
||||
echo -e "${codes}" | grep -${Opt}q "My$2${user_num}="
|
||||
if [ $? -eq 1 ]; then
|
||||
if [ $user_num == 1 ]; then
|
||||
codes=$(echo "${codes}" | sed -r "1i My${2}1=''")
|
||||
else
|
||||
codes=$(echo "${codes}" | sed -r "/My$2$(expr ${user_num} - 1)=/a\My$2${user_num}=''")
|
||||
fi
|
||||
fi
|
||||
done
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
## 导出为他人助力变量(ForOther)
|
||||
if [[ ${codes} ]]; then
|
||||
help_code=""
|
||||
for ((user_num=1;user_num<=${UserSum};user_num++)); do
|
||||
echo -e "${codes}" | grep -${Opt}q "My$2${user_num}=''"
|
||||
if [ $? -eq 1 ]; then
|
||||
help_code=${help_code}"\${My"$2${user_num}"}@"
|
||||
fi
|
||||
done
|
||||
## 生成互助规则模板
|
||||
for_other_codes=""
|
||||
case $HelpType in
|
||||
0) ### 统一优先级助力模板
|
||||
new_code=$(echo ${help_code} | sed "s/@$//")
|
||||
for ((user_num=1;user_num<=${UserSum};user_num++)); do
|
||||
if [ $user_num == 1 ]; then
|
||||
for_other_codes=${for_other_codes}"ForOther"$2${user_num}"=\""${new_code}"\"\n"
|
||||
else
|
||||
for_other_codes=${for_other_codes}"ForOther"$2${user_num}"=\"\${ForOther"${2}1"}\"\n"
|
||||
fi
|
||||
done
|
||||
;;
|
||||
1) ### 均匀助力模板
|
||||
for ((user_num=1;user_num<=${UserSum};user_num++)); do
|
||||
echo ${help_code} | grep "\${My"$2${user_num}"}@" > /dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
left_str=$(echo ${help_code} | sed "s/\${My$2${user_num}}@/ /g" | awk '{print $1}')
|
||||
right_str=$(echo ${help_code} | sed "s/\${My$2${user_num}}@/ /g" | awk '{print $2}')
|
||||
mark="\${My$2${user_num}}@"
|
||||
else
|
||||
left_str=$(echo ${help_code} | sed "s/${mark}/ /g" | awk '{print $1}')${mark}
|
||||
right_str=$(echo ${help_code} | sed "s/${mark}/ /g" | awk '{print $2}')
|
||||
fi
|
||||
new_code=$(echo ${right_str}${left_str} | sed "s/@$//")
|
||||
for_other_codes=${for_other_codes}"ForOther"$2${user_num}"=\""${new_code}"\"\n"
|
||||
done
|
||||
;;
|
||||
*) ### 普通优先级助力模板
|
||||
for ((user_num=1;user_num<=${UserSum};user_num++)); do
|
||||
new_code=$(echo ${help_code} | sed "s/\${My"$2${user_num}"}@//;s/@$//")
|
||||
for_other_codes=${for_other_codes}"ForOther"$2${user_num}"=\""${new_code}"\"\n"
|
||||
done
|
||||
;;
|
||||
esac
|
||||
echo -e "${codes}\n\n${for_other_codes}" | sed s/[[:space:]]//g
|
||||
else
|
||||
echo ${Tips}
|
||||
fi
|
||||
else
|
||||
echo "未运行过 jd_$1 脚本,未产生日志"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
## 汇总
|
||||
function Cat_All {
|
||||
echo -e "\n从最后一个日志提取互助码,受日志内容影响,仅供参考。"
|
||||
for ((i=0; i<${#Name1[*]}; i++)); do
|
||||
echo -e "\n${Name2[i]}:"
|
||||
[[ $(Cat_Scodes "${Name1[i]}" "${Name3[i]}" "的${Name2[i]}好友互助码") == ${Tips} ]] && Cat_Scodes "${Name1[i]}" "${Name3[i]}" || Cat_Scodes "${Name1[i]}" "${Name3[i]}" "的${Name2[i]}好友互助码"
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
## 执行并写入日志
|
||||
LogTime=$(date "+%Y-%m-%d-%H-%M-%S")
|
||||
LogFile="${LogDir}/export_sharecodes/${LogTime}.log"
|
||||
[ ! -d "${LogDir}/export_sharecodes" ] && mkdir -p ${LogDir}/export_sharecodes
|
||||
Import_Conf && Count_UserSum && Cat_All | perl -pe "{s|京东种豆|种豆|; s|crazyJoy任务|疯狂的JOY|}" | tee ${LogFile}
|
||||
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env bash
|
||||
#author:spark thanks to: https://github.com/sparkssssssss/scripts
|
||||
#需要docker环境,下载本文件到容器内任意位置,以下示例是放到了/jd/scripts
|
||||
#*/30 * * * * bash diy i-chenzhe qx
|
||||
|
||||
declare -A BlackListDict
|
||||
author=$1
|
||||
repo=$2
|
||||
#指定仓库屏蔽关键词,不添加计划任务,多个按照格式二
|
||||
BlackListDict['i-chenzhe']="_get"
|
||||
BlackListDict['sparkssssssss']="smzdm|tg|xxxxxxxx"
|
||||
|
||||
blackword=${BlackListDict["${author}"]}
|
||||
blackword=${blackword:-"wojiushigejimo"}
|
||||
|
||||
if [ $# != 2 ] ; then
|
||||
echo "USAGE: $0 author repo"
|
||||
exit 0;
|
||||
fi
|
||||
|
||||
diyscriptsdir=/jd/diyscripts
|
||||
mkdir -p ${diyscriptsdir}
|
||||
|
||||
if [ ! -d "$diyscriptsdir/${author}_${repo}" ]; then
|
||||
echo -e "${author}本地仓库不存在,从gayhub拉取ing..."
|
||||
cd ${diyscriptsdir} && git clone https://github.com/${author}/${repo}.git ${author}_${repo}
|
||||
gitpullstatus=$?
|
||||
[ $gitpullstatus -eq 0 ] && echo -e "${author}本地仓库拉取完毕"
|
||||
[ $gitpullstatus -ne 0 ] && echo -e "${author}本地仓库拉取失败,请检查!" && exit 0
|
||||
else
|
||||
cd ${diyscriptsdir}/${author}_${repo}
|
||||
branch=`git symbolic-ref --short -q HEAD`
|
||||
git fetch --all
|
||||
git reset --hard origin/$branch
|
||||
git pull
|
||||
gitpullstatus=$?
|
||||
fi
|
||||
|
||||
rand(){
|
||||
min=$1
|
||||
max=$(($2-$min+1))
|
||||
num=$(cat /proc/sys/kernel/random/uuid | cksum | awk -F ' ' '{print $1}')
|
||||
echo $(($num%$max+$min))
|
||||
}
|
||||
|
||||
function addnewcron {
|
||||
addname=""
|
||||
cd ${diyscriptsdir}/${author}_${repo}
|
||||
for js in `ls *.js|egrep -v $blackword`;
|
||||
do
|
||||
croname=`echo "${author}_$js"|awk -F\. '{print $1}'`
|
||||
script_date=`cat $js|grep ^[0-9]|awk '{print $1,$2,$3,$4,$5}'|egrep -v "[a-zA-Z]|:|\."|sort |uniq|head -n 1`
|
||||
[ -z "${script_date}" ] && script_date=`cat $js|grep -Eo "([0-9]+|\*|[0-9]+[,-].*) ([0-9]+|\*|[0-9]+[,-].*) ([0-9]+|\*|[0-9]+[,-].*) ([0-9]+|\*|[0-9]+[,-].*) ([0-9]+|\*|[0-9][,-].*)"|sort |uniq|head -n 1`
|
||||
[ -z "${script_date}" ] && cron_min=$(rand 1 59) && cron_hour=$(rand 7 9) && script_date="${cron_min} ${cron_hour} * * *"
|
||||
[ $(grep -c -w "$croname" /jd/config/crontab.list) -eq 0 ] && sed -i "/hangup/a${script_date} bash jd $croname" /jd/config/crontab.list && addname="${addname}\n${croname}" && echo -e "添加了新的脚本${croname}." && bash jd ${croname} now >/dev/null &
|
||||
if [ ! -f "/jd/scripts/${author}_$js" ];then
|
||||
\cp $js /jd/scripts/${author}_$js
|
||||
else
|
||||
change=$(diff $js /jd/scripts/${author}_$js)
|
||||
[ -n "${change}" ] && \cp $js /jd/scripts/${author}_$js && echo -e "${author}_$js 脚本更新了."
|
||||
fi
|
||||
done
|
||||
[ "$addname" != "" ] notify "新增 ${author} 自定义脚本" "${addname}"
|
||||
|
||||
}
|
||||
|
||||
function delcron {
|
||||
delname=""
|
||||
cronfiles=$(grep "$author" /jd/config/crontab.list|grep -v "^#"|awk '{print $8}'|awk -F"${author}_" '{print $2}')
|
||||
for filename in $cronfiles;
|
||||
do
|
||||
if [ ! -f "${diyscriptsdir}/${author}_${repo}/${filename}.js" ]; then
|
||||
sed -i "/\<bash jd ${author}_${filename}\>/d" /jd/config/crontab.list && echo -e "删除失效脚本${filename}."
|
||||
delname="${delname}\n${author}_${filename}"
|
||||
fi
|
||||
done
|
||||
[ "$delname" != "" ] && notify "删除 ${author} 失效脚本" "${delname}"
|
||||
}
|
||||
|
||||
if [[ ${gitpullstatus} -eq 0 ]]
|
||||
then
|
||||
addnewcron
|
||||
delcron
|
||||
else
|
||||
echo -e "$author 仓库更新失败了."
|
||||
notify "自定义仓库更新失败" "$author"
|
||||
fi
|
||||
|
||||
exit 0
|
||||
Executable
+357
@@ -0,0 +1,357 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
## 文件路径、脚本网址、文件版本以及各种环境的判断
|
||||
ShellDir=${JD_DIR:-$(cd $(dirname $0); pwd)}
|
||||
[[ ${JD_DIR} ]] && ShellJd=jd || ShellJd=${ShellDir}/shell/jd.sh
|
||||
LogDir=${ShellDir}/log
|
||||
[ ! -d ${LogDir} ] && mkdir -p ${LogDir}
|
||||
ScriptsDir=${ShellDir}/scripts
|
||||
ConfigDir=${ShellDir}/config
|
||||
FileConf=${ConfigDir}/config.sh
|
||||
FileDiy=${ConfigDir}/diy.sh
|
||||
FileConfSample=${ShellDir}/sample/config.sh.sample
|
||||
ListCron=${ConfigDir}/crontab.list
|
||||
ListCronLxk=${ScriptsDir}/docker/crontab_list.sh
|
||||
ListTask=${LogDir}/task.list
|
||||
ListJs=${LogDir}/js.list
|
||||
ListJsAdd=${LogDir}/js-add.list
|
||||
ListJsDrop=${LogDir}/js-drop.list
|
||||
ContentVersion=${ShellDir}/version
|
||||
ContentNewTask=${ShellDir}/new_task
|
||||
ContentDropTask=${ShellDir}/drop_task
|
||||
SendCount=${ShellDir}/send_count
|
||||
isTermux=${ANDROID_RUNTIME_ROOT}${ANDROID_ROOT}
|
||||
ShellURL=https://github.com.cnpmjs.org/whyour/qinglong
|
||||
ScriptsURL=https://github.com.cnpmjs.org/RikudouPatrickstar/jd_scripts
|
||||
|
||||
## 导入配置文件
|
||||
function Import_Conf {
|
||||
if [ -f ${FileConf} ]; then
|
||||
. ${FileConf}
|
||||
fi
|
||||
}
|
||||
|
||||
## 更新crontab,gitee服务器同一时间限制5个链接,因此每个人更新代码必须错开时间,每次执行git_pull随机生成。
|
||||
## 每天次数随机,更新时间随机,更新秒数随机,至少6次,至多12次,大部分为8-10次,符合正态分布。
|
||||
function Update_Cron {
|
||||
if [[ $(date "+%-H") -le 2 ]] && [ -f ${ListCron} ]; then
|
||||
RanMin=$((${RANDOM} % 60))
|
||||
RanSleep=$((${RANDOM} % 56))
|
||||
RanHourArray[0]=$((${RANDOM} % 3))
|
||||
for ((i=1; i<14; i++)); do
|
||||
j=$(($i - 1))
|
||||
tmp=$((${RANDOM} % 3 + ${RanHourArray[j]} + 2))
|
||||
[[ ${tmp} -lt 24 ]] && RanHourArray[i]=${tmp} || break
|
||||
done
|
||||
RanHour=${RanHourArray[0]}
|
||||
for ((i=1; i<${#RanHourArray[*]}; i++)); do
|
||||
RanHour="${RanHour},${RanHourArray[i]}"
|
||||
done
|
||||
perl -i -pe "s|.+(bash.+git_pull.+log.*)|${RanMin} ${RanHour} \* \* \* sleep ${RanSleep} && \1|" ${ListCron}
|
||||
crontab ${ListCron}
|
||||
fi
|
||||
}
|
||||
function Git_PullShell {
|
||||
echo -e "更新shell...\n"
|
||||
cd ${ShellDir}
|
||||
git fetch --all
|
||||
git stash
|
||||
git pull
|
||||
ExitStatusShell=$?
|
||||
echo
|
||||
git stash pop
|
||||
git reset --mixed
|
||||
}
|
||||
|
||||
## 更新shell成功后的操作
|
||||
function Git_PullShellNext {
|
||||
if [[ ${ExitStatusShell} -eq 0 ]]; then
|
||||
echo -e "更新shell成功...\n"
|
||||
Update_Entrypoint
|
||||
[[ "${PanelDependOld}" != "${PanelDependNew}" ]] && cd ${ShellDir}/panel && Npm_Install panel
|
||||
cp -f ${FileConfSample} ${ConfigDir}/config.sh.sample
|
||||
[ -d ${ScriptsDir}/node_modules ] && Notify_Version
|
||||
else
|
||||
echo -e "更新shell失败,请检查原因...\n"
|
||||
fi
|
||||
}
|
||||
|
||||
## 克隆scripts
|
||||
function Git_CloneScripts {
|
||||
git clone -b master ${ScriptsURL} ${ScriptsDir}
|
||||
ExitStatusScripts=$?
|
||||
echo
|
||||
}
|
||||
|
||||
## 更新scripts
|
||||
function Git_PullScripts {
|
||||
echo -e "更新scripts...\n"
|
||||
cd ${ScriptsDir}
|
||||
git fetch --all
|
||||
git rm -f i-chenzhe*
|
||||
git rm -f moposmall*
|
||||
git rm -f qq34347476*
|
||||
git rm -f whyour*
|
||||
git stash
|
||||
git pull
|
||||
ExitStatusScripts=$?
|
||||
echo
|
||||
git stash pop
|
||||
git reset --mixed
|
||||
}
|
||||
|
||||
## 更新docker-entrypoint
|
||||
function Update_Entrypoint {
|
||||
if [[ ${JD_DIR} ]] && [[ $(cat ${ShellDir}/docker/docker-entrypoint.sh) != $(cat /usr/local/bin/docker-entrypoint.sh) ]]; then
|
||||
cp -f ${ShellDir}/docker/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
|
||||
chmod 777 /usr/local/bin/docker-entrypoint.sh
|
||||
fi
|
||||
}
|
||||
|
||||
## 用户数量UserSum
|
||||
function Count_UserSum {
|
||||
i=1
|
||||
while [ $i -le 1000 ]; do
|
||||
Tmp=Cookie$i
|
||||
CookieTmp=${!Tmp}
|
||||
[[ ${CookieTmp} ]] && UserSum=$i || break
|
||||
let i++
|
||||
done
|
||||
}
|
||||
|
||||
## 检测文件:LXK9301/jd_scripts 仓库中的 docker/crontab_list.sh
|
||||
## 检测定时任务是否有变化,此函数会在Log文件夹下生成四个文件,分别为:
|
||||
## task.list crontab.list中的所有任务清单,仅保留脚本名
|
||||
## js.list 上述检测文件中用来运行js脚本的清单(去掉后缀.js,非运行脚本的不会包括在内)
|
||||
## js-add.list 如果上述检测文件增加了定时任务,这个文件内容将不为空
|
||||
## js-drop.list 如果上述检测文件删除了定时任务,这个文件内容将不为空
|
||||
function Diff_Cron {
|
||||
if [ -f ${ListCron} ]; then
|
||||
if [ -n "${JD_DIR}" ]
|
||||
then
|
||||
grep -E " j[drx]_\w+" ${ListCron} | perl -pe "s|.+ (j[drx]_\w+).*|\1|" | sort -u > ${ListTask}
|
||||
else
|
||||
grep "${ShellDir}/" ${ListCron} | grep -E " j[drx]_\w+" | perl -pe "s|.+ (j[drx]_\w+).*|\1|" | sort -u > ${ListTask}
|
||||
fi
|
||||
cat ${ListCronLxk} | grep -E "j[drx]_\w+\.js" | perl -pe "s|.+(j[drx]_\w+)\.js.+|\1|" | sort -u > ${ListJs}
|
||||
grep -vwf ${ListTask} ${ListJs} > ${ListJsAdd}
|
||||
grep -vwf ${ListJs} ${ListTask} > ${ListJsDrop}
|
||||
else
|
||||
echo -e "${ListCron} 文件不存在,请先定义你自己的crontab.list...\n"
|
||||
fi
|
||||
}
|
||||
|
||||
## 发送删除失效定时任务的消息
|
||||
function Notify_DropTask {
|
||||
cd ${ShellDir}
|
||||
node update.js
|
||||
[ -f ${ContentDropTask} ] && rm -f ${ContentDropTask}
|
||||
}
|
||||
|
||||
## 发送新的定时任务消息
|
||||
function Notify_NewTask {
|
||||
cd ${ShellDir}
|
||||
node update.js
|
||||
[ -f ${ContentNewTask} ] && rm -f ${ContentNewTask}
|
||||
}
|
||||
|
||||
## 检测配置文件版本
|
||||
function Notify_Version {
|
||||
## 识别出两个文件的版本号
|
||||
VerConfSample=$(grep " Version: " ${FileConfSample} | perl -pe "s|.+v((\d+\.?){3})|\1|")
|
||||
[ -f ${FileConf} ] && VerConf=$(grep " Version: " ${FileConf} | perl -pe "s|.+v((\d+\.?){3})|\1|")
|
||||
|
||||
## 删除旧的发送记录文件
|
||||
[ -f "${SendCount}" ] && [[ $(cat ${SendCount}) != ${VerConfSample} ]] && rm -f ${SendCount}
|
||||
|
||||
## 识别出更新日期和更新内容
|
||||
UpdateDate=$(grep " Date: " ${FileConfSample} | awk -F ": " '{print $2}')
|
||||
UpdateContent=$(grep " Update Content: " ${FileConfSample} | awk -F ": " '{print $2}')
|
||||
|
||||
## 如果是今天,并且版本号不一致,则发送通知
|
||||
if [ -f ${FileConf} ] && [[ "${VerConf}" != "${VerConfSample}" ]] && [[ ${UpdateDate} == $(date "+%Y-%m-%d") ]]
|
||||
then
|
||||
if [ ! -f ${SendCount} ]; then
|
||||
echo -e "检测到配置文件config.sh.sample有更新\n\n更新日期: ${UpdateDate}\n当前版本: ${VerConf}\n新的版本: ${VerConfSample}\n更新内容: ${UpdateContent}\n更新说明: 如需使用新功能请对照config.sh.sample,将相关新参数手动增加到你自己的config.sh中,否则请无视本消息。本消息只在该新版本配置文件更新当天发送一次。" | tee ${ContentVersion}
|
||||
cd ${ShellDir}
|
||||
node update.js
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "${VerConfSample}" > ${SendCount}
|
||||
[ -f ${ContentVersion} ] && rm -f ${ContentVersion}
|
||||
fi
|
||||
fi
|
||||
else
|
||||
[ -f ${ContentVersion} ] && rm -f ${ContentVersion}
|
||||
[ -f ${SendCount} ] && rm -f ${SendCount}
|
||||
fi
|
||||
}
|
||||
|
||||
## npm install 子程序,判断是否为安卓,判断是否安装有yarn
|
||||
function Npm_InstallSub {
|
||||
if [ -n "${isTermux}" ]
|
||||
then
|
||||
npm install --no-bin-links || npm install --no-bin-links --registry=https://registry.npm.taobao.org
|
||||
elif ! type yarn >/dev/null 2>&1
|
||||
then
|
||||
npm install || npm install --registry=https://registry.npm.taobao.org
|
||||
else
|
||||
echo -e "检测到本机安装了 yarn,使用 yarn 替代 npm...\n"
|
||||
yarn install || yarn install --registry=https://registry.npm.taobao.org
|
||||
fi
|
||||
}
|
||||
|
||||
## npm install
|
||||
function Npm_Install {
|
||||
echo -e "检测到 $1 的依赖包有变化,运行 npm install...\n"
|
||||
Npm_InstallSub
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "\nnpm install 运行不成功,自动删除 $1/node_modules 后再次尝试一遍..."
|
||||
rm -rf node_modules
|
||||
fi
|
||||
echo
|
||||
|
||||
if [ ! -d node_modules ]; then
|
||||
echo -e "运行 npm install...\n"
|
||||
Npm_InstallSub
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "\nnpm install 运行不成功,自动删除 $1/node_modules...\n"
|
||||
echo -e "请进入 $1 目录后手动运行 npm install...\n"
|
||||
echo -e "3...\n"
|
||||
sleep 1
|
||||
echo -e "2...\n"
|
||||
sleep 1
|
||||
echo -e "1...\n"
|
||||
sleep 1
|
||||
rm -rf node_modules
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
## 输出是否有新的定时任务
|
||||
function Output_ListJsAdd {
|
||||
if [ -s ${ListJsAdd} ]; then
|
||||
echo -e "检测到有新的定时任务:\n"
|
||||
cat ${ListJsAdd}
|
||||
echo
|
||||
fi
|
||||
}
|
||||
|
||||
## 输出是否有失效的定时任务
|
||||
function Output_ListJsDrop {
|
||||
if [ ${ExitStatusScripts} -eq 0 ] && [ -s ${ListJsDrop} ]; then
|
||||
echo -e "检测到有失效的定时任务:\n"
|
||||
cat ${ListJsDrop}
|
||||
echo
|
||||
fi
|
||||
}
|
||||
|
||||
## 自动删除失效的脚本与定时任务,需要5个条件:1.AutoDelCron 设置为 true;2.正常更新js脚本,没有报错;3.js-drop.list不为空;4.crontab.list存在并且不为空;5.已经正常运行过npm install
|
||||
## 检测文件:LXK9301/jd_scripts 仓库中的 docker/crontab_list.sh
|
||||
## 如果检测到某个定时任务在上述检测文件中已删除,那么在本地也删除对应定时任务
|
||||
function Del_Cron {
|
||||
if [ "${AutoDelCron}" = "true" ] && [ -s ${ListJsDrop} ] && [ -s ${ListCron} ] && [ -d ${ScriptsDir}/node_modules ]; then
|
||||
echo -e "开始尝试自动删除定时任务如下:\n"
|
||||
cat ${ListJsDrop}
|
||||
echo
|
||||
JsDrop=$(cat ${ListJsDrop})
|
||||
for Cron in ${JsDrop}
|
||||
do
|
||||
perl -i -ne "{print unless / ${Cron}( |$)/}" ${ListCron}
|
||||
done
|
||||
crontab ${ListCron}
|
||||
echo -e "成功删除失效的脚本与定时任务,当前的定时任务清单如下:\n\n--------------------------------------------------------------\n"
|
||||
crontab -l
|
||||
echo -e "\n--------------------------------------------------------------\n"
|
||||
if [ -d ${ScriptsDir}/node_modules ]; then
|
||||
notify "删除 lxk0301 失效脚本" "${JsDrop}"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
## 自动增加新的定时任务,需要5个条件:1.AutoAddCron 设置为 true;2.正常更新js脚本,没有报错;3.js-add.list不为空;4.crontab.list存在并且不为空;5.已经正常运行过npm install
|
||||
## 检测文件:LXK9301/jd_scripts 仓库中的 docker/crontab_list.sh
|
||||
## 如果检测到检测文件中增加新的定时任务,那么在本地也增加
|
||||
## 本功能生效时,会自动从检测文件新增加的任务中读取时间,该时间为北京时间
|
||||
function Add_Cron {
|
||||
if [ "${AutoAddCron}" = "true" ] && [ -s ${ListJsAdd} ] && [ -s ${ListCron} ] && [ -d ${ScriptsDir}/node_modules ]; then
|
||||
echo -e "开始尝试自动添加定时任务如下:\n"
|
||||
cat ${ListJsAdd}
|
||||
echo
|
||||
JsAdd=$(cat ${ListJsAdd})
|
||||
|
||||
for Cron in ${JsAdd}
|
||||
do
|
||||
if [[ ${Cron} == jd_bean_sign ]]
|
||||
then
|
||||
echo "4 0,9 * * * bash ${ShellJd} ${Cron}" >> ${ListCron}
|
||||
else
|
||||
cat ${ListCronLxk} | grep -E "\/${Cron}\." | perl -pe "s|(^.+)node */scripts/(j[drx]_\w+)\.js.+|\1bash ${ShellJd} \2|" >> ${ListCron}
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $? -eq 0 ]
|
||||
then
|
||||
crontab ${ListCron}
|
||||
echo -e "成功添加新的定时任务,当前的定时任务清单如下:\n\n--------------------------------------------------------------\n"
|
||||
crontab -l
|
||||
echo -e "\n--------------------------------------------------------------\n"
|
||||
if [ -d ${ScriptsDir}/node_modules ]; then
|
||||
notify "新增 lxk0301 自定义脚本" "${JsAdd}"
|
||||
fi
|
||||
else
|
||||
echo -e "添加新的定时任务出错,请手动添加...\n"
|
||||
if [ -d ${ScriptsDir}/node_modules ]; then
|
||||
notify "尝试自动添加 lxk0301 以下新的定时任务出错,请手动添加:" "${JsAdd}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
## 在日志中记录时间与路径
|
||||
echo -e "\n--------------------------------------------------------------\n"
|
||||
echo -n "系统时间:"
|
||||
echo $(date "+%Y-%m-%d %H:%M:%S")
|
||||
if [ "${TZ}" = "UTC" ]; then
|
||||
echo
|
||||
echo -n "北京时间:"
|
||||
echo $(date -d "8 hour" "+%Y-%m-%d %H:%M:%S")
|
||||
fi
|
||||
echo -e "\nJS脚本目录:${ScriptsDir}\n"
|
||||
echo -e "--------------------------------------------------------------\n"
|
||||
|
||||
## 导入配置,更新cron,设置url,更新shell,复制sample,复制entrypoint,发送新配置通知
|
||||
Import_Conf "git_pull"
|
||||
Update_Cron
|
||||
[ -f ${ShellDir}/panel/package.json ] && PanelDependOld=$(cat ${ShellDir}/panel/package.json)
|
||||
Git_PullShell
|
||||
[ -f ${ShellDir}/panel/package.json ] && PanelDependNew=$(cat ${ShellDir}/panel/package.json)
|
||||
Git_PullShellNext
|
||||
|
||||
## 克隆或更新js脚本
|
||||
[ -f ${ScriptsDir}/package.json ] && ScriptsDependOld=$(cat ${ScriptsDir}/package.json)
|
||||
[ -d ${ScriptsDir}/.git ] && Git_PullScripts || Git_CloneScripts
|
||||
[ -f ${ScriptsDir}/package.json ] && ScriptsDependNew=$(cat ${ScriptsDir}/package.json)
|
||||
|
||||
## 执行各函数
|
||||
if [[ ${ExitStatusScripts} -eq 0 ]]
|
||||
then
|
||||
echo -e "更新scripts成功...\n"
|
||||
Diff_Cron
|
||||
[[ "${ScriptsDependOld}" != "${ScriptsDependNew}" ]] && cd ${ScriptsDir} && Npm_Install scripts
|
||||
Output_ListJsAdd
|
||||
Output_ListJsDrop
|
||||
Del_Cron
|
||||
Add_Cron
|
||||
else
|
||||
echo -e "更新scripts失败,请检查原因...\n"
|
||||
fi
|
||||
|
||||
## 调用用户自定义的diy.sh
|
||||
if [[ ${EnableExtraShell} == true ]]; then
|
||||
if [ -f ${FileDiy} ]
|
||||
then
|
||||
. ${FileDiy}
|
||||
else
|
||||
echo -e "${FileDiy} 文件不存在,跳过执行DIY脚本...\n"
|
||||
fi
|
||||
fi
|
||||
Executable
+281
@@ -0,0 +1,281 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
## 路径
|
||||
ShellDir=${JD_DIR:-$(cd $(dirname $0); pwd)}
|
||||
[[ ${JD_DIR} ]] && HelpJd=jd || HelpJd=jd.sh
|
||||
[[ ${JD_DIR} ]] && ShellJd=jd || ShellJd=${ShellDir}/shell/jd.sh
|
||||
ScriptsDir=${ShellDir}/scripts
|
||||
ConfigDir=${ShellDir}/config
|
||||
FileConf=${ConfigDir}/config.sh
|
||||
FileConfSample=${ShellDir}/sample/config.sh.sample
|
||||
LogDir=${ShellDir}/log
|
||||
ListScripts=($(cd ${ScriptsDir}; ls *.js | grep -E "j[drx]_"))
|
||||
ListCron=${ConfigDir}/crontab.list
|
||||
ListCronLxk=${ScriptsDir}/docker/crontab_list.sh
|
||||
ListJs=${LogDir}/js.list
|
||||
|
||||
## 导入config.sh
|
||||
function Import_Conf {
|
||||
if [ -f ${FileConf} ]
|
||||
then
|
||||
. ${FileConf}
|
||||
if [ -z "${Cookie1}" ]; then
|
||||
echo -e "请先在config.sh中配置好Cookie...\n"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo -e "配置文件 ${FileConf} 不存在,请先按教程配置好该文件...\n"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
## 更新crontab
|
||||
function Detect_Cron {
|
||||
if [[ $(cat ${ListCron}) != $(crontab -l) ]]; then
|
||||
crontab ${ListCron}
|
||||
fi
|
||||
}
|
||||
|
||||
## 用户数量UserSum
|
||||
function Count_UserSum {
|
||||
for ((i=1; i<=1000; i++)); do
|
||||
Tmp=Cookie$i
|
||||
CookieTmp=${!Tmp}
|
||||
[[ ${CookieTmp} ]] && UserSum=$i || break
|
||||
done
|
||||
}
|
||||
|
||||
## 组合Cookie和互助码子程序
|
||||
function Combin_Sub {
|
||||
CombinAll=""
|
||||
if [[ ${AutoHelpOther} == true ]] && [[ $1 == ForOther* ]]; then
|
||||
|
||||
ForOtherAll=""
|
||||
MyName=$(echo $1 | perl -pe "s|ForOther|My|")
|
||||
|
||||
for ((m=1; m<=${UserSum}; m++)); do
|
||||
TmpA=${MyName}$m
|
||||
TmpB=${!TmpA}
|
||||
ForOtherAll="${ForOtherAll}@${TmpB}"
|
||||
done
|
||||
|
||||
for ((n=1; n<=${UserSum}; n++)); do
|
||||
for num in ${TempBlockCookie}; do
|
||||
[[ $n -eq $num ]] && continue 2
|
||||
done
|
||||
CombinAll="${CombinAll}&${ForOtherAll}"
|
||||
done
|
||||
|
||||
else
|
||||
for ((i=1; i<=${UserSum}; i++)); do
|
||||
for num in ${TempBlockCookie}; do
|
||||
[[ $i -eq $num ]] && continue 2
|
||||
done
|
||||
Tmp1=$1$i
|
||||
Tmp2=${!Tmp1}
|
||||
CombinAll="${CombinAll}&${Tmp2}"
|
||||
done
|
||||
fi
|
||||
|
||||
echo ${CombinAll} | perl -pe "{s|^&||; s|^@+||; s|&@|&|g; s|@+&|&|g; s|@+|@|g; s|@+$||}"
|
||||
}
|
||||
|
||||
## 组合Cookie、Token与互助码
|
||||
function Combin_All {
|
||||
export JD_COOKIE=$(Combin_Sub Cookie)
|
||||
export FRUITSHARECODES=$(Combin_Sub ForOtherFruit)
|
||||
export PETSHARECODES=$(Combin_Sub ForOtherPet)
|
||||
export PLANT_BEAN_SHARECODES=$(Combin_Sub ForOtherBean)
|
||||
export DREAM_FACTORY_SHARE_CODES=$(Combin_Sub ForOtherDreamFactory)
|
||||
export DDFACTORY_SHARECODES=$(Combin_Sub ForOtherJdFactory)
|
||||
export JDZZ_SHARECODES=$(Combin_Sub ForOtherJdzz)
|
||||
export JDJOY_SHARECODES=$(Combin_Sub ForOtherJoy)
|
||||
export JXNC_SHARECODES=$(Combin_Sub ForOtherJxnc)
|
||||
export JXNCTOKENS=$(Combin_Sub TokenJxnc)
|
||||
export BOOKSHOP_SHARECODES=$(Combin_Sub ForOtherBookShop)
|
||||
export JD_CASH_SHARECODES=$(Combin_Sub ForOtherCash)
|
||||
export JDSGMH_SHARECODES=$(Combin_Sub ForOtherSgmh)
|
||||
export JDCFD_SHARECODES=$(Combin_Sub ForOtherCfd)
|
||||
export JDGLOBAL_SHARECODES=$(Combin_Sub ForOtherGlobal)
|
||||
}
|
||||
|
||||
## 转换JD_BEAN_SIGN_STOP_NOTIFY或JD_BEAN_SIGN_NOTIFY_SIMPLE
|
||||
function Trans_JD_BEAN_SIGN_NOTIFY {
|
||||
case ${NotifyBeanSign} in
|
||||
0)
|
||||
export JD_BEAN_SIGN_STOP_NOTIFY="true"
|
||||
;;
|
||||
1)
|
||||
export JD_BEAN_SIGN_NOTIFY_SIMPLE="true"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
## 转换UN_SUBSCRIBES
|
||||
function Trans_UN_SUBSCRIBES {
|
||||
export UN_SUBSCRIBES="${goodPageSize}\n${shopPageSize}\n${jdUnsubscribeStopGoods}\n${jdUnsubscribeStopShop}"
|
||||
}
|
||||
|
||||
## 申明全部变量
|
||||
function Set_Env {
|
||||
Count_UserSum
|
||||
Combin_All
|
||||
Trans_JD_BEAN_SIGN_NOTIFY
|
||||
Trans_UN_SUBSCRIBES
|
||||
}
|
||||
|
||||
## 随机延迟
|
||||
function Random_Delay {
|
||||
if [[ -n ${RandomDelay} ]] && [[ ${RandomDelay} -gt 0 ]]; then
|
||||
CurMin=$(date "+%-M")
|
||||
if [[ ${CurMin} -gt 2 && ${CurMin} -lt 30 ]] || [[ ${CurMin} -gt 31 && ${CurMin} -lt 59 ]]; then
|
||||
CurDelay=$((${RANDOM} % ${RandomDelay} + 1))
|
||||
echo -e "\n命令未添加 \"now\",随机延迟 ${CurDelay} 秒后再执行任务,如需立即终止,请按 CTRL+C...\n"
|
||||
sleep ${CurDelay}
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
## 使用说明
|
||||
function Help {
|
||||
echo -e "本脚本的用法为:"
|
||||
echo -e "1. bash ${HelpJd} xxx # 如果设置了随机延迟并且当时时间不在0-2、30-31、59分内,将随机延迟一定秒数"
|
||||
echo -e "2. bash ${HelpJd} xxx now # 无论是否设置了随机延迟,均立即运行"
|
||||
echo -e "3. bash ${HelpJd} runall # 运行所有非挂机脚本,非常耗时"
|
||||
echo -e "4. bash ${HelpJd} hangup # 重启挂机程序"
|
||||
echo -e "5. bash ${HelpJd} resetpwd # 重置控制面板用户名和密码"
|
||||
echo -e "\n针对用法1、用法2中的\"xxx\",可以不输入后缀\".js\",另外,如果前缀是\"jd_\"的话前缀也可以省略。"
|
||||
echo -e "当前有以下脚本可以运行(仅列出以jd_、jr_、jx_开头的脚本):"
|
||||
cd ${ScriptsDir}
|
||||
for ((i=0; i<${#ListScripts[*]}; i++)); do
|
||||
Name=$(grep "new Env" ${ListScripts[i]} | awk -F "'|\"" '{print $2}')
|
||||
echo -e "$(($i + 1)).${Name}:${ListScripts[i]}"
|
||||
done
|
||||
}
|
||||
|
||||
## nohup
|
||||
function Run_Nohup {
|
||||
if [[ $(ps -ef | grep "${js}" | grep -v "grep") != "" ]]; then
|
||||
ps -ef | grep "${js}" | grep -v "grep" | awk '{print $2}' | xargs kill -9
|
||||
fi
|
||||
[ ! -d ${LogDir}/${js} ] && mkdir -p ${LogDir}/${js}
|
||||
LogTime=$(date "+%Y-%m-%d-%H-%M-%S")
|
||||
LogFile="${LogDir}/${js}/${LogTime}.log"
|
||||
nohup node ${js}.js > ${LogFile} &
|
||||
}
|
||||
|
||||
## 运行挂机脚本
|
||||
function Run_HangUp {
|
||||
HangUpJs="jd_crazy_joy_coin"
|
||||
cd ${ScriptsDir}
|
||||
for js in ${HangUpJs}; do
|
||||
Import_Conf ${js} && Set_Env
|
||||
if type pm2 >/dev/null 2>&1; then
|
||||
pm2 stop ${js}.js 2>/dev/null
|
||||
pm2 flush
|
||||
pm2 start -a ${js}.js --watch "${ScriptsDir}/${js}.js" --name="${js}"
|
||||
else
|
||||
Run_Nohup >/dev/null 2>&1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
## 重置密码
|
||||
function Reset_Pwd {
|
||||
cp -f ${ShellDir}/sample/auth.json ${ConfigDir}/auth.json
|
||||
echo -e "控制面板重置成功,用户名:admin,密码:adminadmin\n"
|
||||
}
|
||||
|
||||
## 一次性运行所有脚本
|
||||
function Run_All {
|
||||
if [ ! -f ${ListJs} ]; then
|
||||
cat ${ListCronLxk} | grep -E "j[drx]_\w+\.js" | perl -pe "s|.+(j[drx]_\w+)\.js.+|\1|" | sort -u > ${ListJs}
|
||||
fi
|
||||
echo -e "\n==================== 开始运行所有非挂机脚本 ====================\n"
|
||||
echo -e "请注意:本过程将非常非常耗时,一个账号可能长达几小时,账号越多耗时越长,如果是手动运行,退出终端也将终止运行。\n"
|
||||
echo -e "倒计时5秒...\n"
|
||||
for ((sec=5; sec>0; sec--)); do
|
||||
echo -e "$sec...\n"
|
||||
sleep 1
|
||||
done
|
||||
for file in $(cat ${ListJs}); do
|
||||
echo -e "==================== 运行 $file.js 脚本 ====================\n"
|
||||
bash ${ShellJd} $file now
|
||||
done
|
||||
}
|
||||
|
||||
## 正常运行单个脚本
|
||||
function Run_Normal {
|
||||
Import_Conf $1 && Detect_Cron && Set_Env
|
||||
|
||||
FileNameTmp1=$(echo $1 | perl -pe "s|\.js||")
|
||||
FileNameTmp2=$(echo $1 | perl -pe "{s|jd_||; s|\.js||; s|^|jd_|}")
|
||||
SeekDir="${ScriptsDir} ${ScriptsDir}/backUp ${ConfigDir}"
|
||||
FileName=""
|
||||
WhichDir=""
|
||||
|
||||
for dir in ${SeekDir}
|
||||
do
|
||||
if [ -f ${dir}/${FileNameTmp1}.js ]; then
|
||||
FileName=${FileNameTmp1}
|
||||
WhichDir=${dir}
|
||||
break
|
||||
elif [ -f ${dir}/${FileNameTmp2}.js ]; then
|
||||
FileName=${FileNameTmp2}
|
||||
WhichDir=${dir}
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -n "${FileName}" ] && [ -n "${WhichDir}" ]
|
||||
then
|
||||
[ $# -eq 1 ] && Random_Delay
|
||||
LogTime=$(date "+%Y-%m-%d-%H-%M-%S")
|
||||
LogFile="${LogDir}/${FileName}/${LogTime}.log"
|
||||
[ ! -d ${LogDir}/${FileName} ] && mkdir -p ${LogDir}/${FileName}
|
||||
cd ${WhichDir}
|
||||
node ${FileName}.js 2>&1 | tee ${LogFile}
|
||||
else
|
||||
echo -e "\n在${ScriptsDir}、${ScriptsDir}/backUp、${ConfigDir}三个目录下均未检测到 $1 脚本的存在,请确认...\n"
|
||||
Help
|
||||
fi
|
||||
}
|
||||
|
||||
## 命令检测
|
||||
case $# in
|
||||
0)
|
||||
echo
|
||||
Help
|
||||
;;
|
||||
1)
|
||||
case $1 in
|
||||
hangup)
|
||||
Run_HangUp
|
||||
;;
|
||||
resetpwd)
|
||||
Reset_Pwd
|
||||
;;
|
||||
runall)
|
||||
Run_All
|
||||
;;
|
||||
*)
|
||||
Run_Normal $1
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
2)
|
||||
case $2 in
|
||||
now)
|
||||
Run_Normal $1 $2
|
||||
;;
|
||||
*)
|
||||
echo -e "\n命令输入错误...\n"
|
||||
Help
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
*)
|
||||
echo -e "\n命令过多...\n"
|
||||
Help
|
||||
;;
|
||||
esac
|
||||
@@ -0,0 +1,5 @@
|
||||
const notify = require('../scripts/sendNotify');
|
||||
const title = process.argv[2];
|
||||
const content = process.argv[3];
|
||||
|
||||
notify.sendNotify(`${title}`, `${content}`);
|
||||
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
#author:spark thanks to: https://github.com/sparkssssssss/scripts
|
||||
|
||||
. /jd/config/config.sh
|
||||
title=$(echo $1|sed 's/-/_/g')
|
||||
msg=$(echo -e $2)
|
||||
|
||||
node /jd/shell/sendinfo.js "$title" "$msg"
|
||||
Executable
+57
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
## 判断环境
|
||||
ShellDir=${JD_DIR:-$(cd $(dirname $0); pwd)}
|
||||
LogDir=${ShellDir}/log
|
||||
|
||||
## 导入配置文件
|
||||
. ${ShellDir}/config/config.sh
|
||||
|
||||
## 删除运行js脚本的旧日志
|
||||
function Rm_JsLog {
|
||||
LogFileList=$(ls -l ${LogDir}/*/*.log | awk '{print $9}')
|
||||
for log in ${LogFileList}
|
||||
do
|
||||
LogDate=$(echo ${log} | awk -F "/" '{print $NF}' | cut -c1-10) #文件名比文件属性获得的日期要可靠
|
||||
if [[ $(uname -s) == Darwin ]]
|
||||
then
|
||||
DiffTime=$(($(date +%s) - $(date -j -f "%Y-%m-%d" "${LogDate}" +%s)))
|
||||
else
|
||||
DiffTime=$(($(date +%s) - $(date +%s -d "${LogDate}")))
|
||||
fi
|
||||
[ ${DiffTime} -gt $((${RmLogDaysAgo} * 86400)) ] && rm -vf ${log}
|
||||
done
|
||||
}
|
||||
|
||||
## 删除git_pull.sh的运行日志
|
||||
function Rm_GitPullLog {
|
||||
if [[ $(uname -s) == Darwin ]]
|
||||
then
|
||||
DateDelLog=$(date -v-${RmLogDaysAgo}d "+%Y-%m-%d")
|
||||
else
|
||||
Stmp=$(($(date "+%s") - 86400 * ${RmLogDaysAgo}))
|
||||
DateDelLog=$(date -d "@${Stmp}" "+%Y-%m-%d")
|
||||
fi
|
||||
LineEndGitPull=$[$(cat ${LogDir}/git_pull.log | grep -n "${DateDelLog} " | head -1 | awk -F ":" '{print $1}') - 3]
|
||||
[ ${LineEndGitPull} -gt 0 ] && perl -i -ne "{print unless 1 .. ${LineEndGitPull} }" ${LogDir}/git_pull.log
|
||||
}
|
||||
|
||||
## 删除空文件夹
|
||||
function Rm_EmptyDir {
|
||||
cd ${LogDir}
|
||||
for dir in $(ls)
|
||||
do
|
||||
if [ -d ${dir} ] && [[ $(ls ${dir}) == "" ]]; then
|
||||
rm -rf ${dir}
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
## 运行
|
||||
if [ -n "${RmLogDaysAgo}" ]; then
|
||||
echo -e "查找旧日志文件中...\n"
|
||||
Rm_JsLog
|
||||
Rm_GitPullLog
|
||||
Rm_EmptyDir
|
||||
echo -e "删除旧日志执行完毕\n"
|
||||
fi
|
||||
Executable
+447
@@ -0,0 +1,447 @@
|
||||
/*
|
||||
* @Author: lxk0301 https://github.com/lxk0301
|
||||
* @Date: 2020-12-20 13:50:34
|
||||
* @Last Modified by: lxk0301
|
||||
* @Last Modified time: 2020-12-20 13:51:02
|
||||
*/
|
||||
const $ = new Env('通知');
|
||||
const notify = require('./scripts/sendNotify');
|
||||
const fs = require('fs');
|
||||
!(async () => {
|
||||
await update();
|
||||
})()
|
||||
.catch((e) => $.logErr(e))
|
||||
.finally(() => $.done());
|
||||
|
||||
async function update() {
|
||||
try {
|
||||
if (fs.existsSync('new_task')) {
|
||||
const newTaskContent = await fs.readFileSync('./new_task', 'utf8');
|
||||
if (newTaskContent) {
|
||||
await notify.sendNotify('新增薅羊毛任务通知', newTaskContent);
|
||||
}
|
||||
}
|
||||
|
||||
if (fs.existsSync('drop_task')) {
|
||||
const dropTaskContent = await fs.readFileSync('./drop_task', 'utf8');
|
||||
if (dropTaskContent) {
|
||||
await notify.sendNotify('删除失效任务通知', dropTaskContent);
|
||||
}
|
||||
}
|
||||
|
||||
if (fs.existsSync('version')) {
|
||||
const versionContent = await fs.readFileSync('./version', 'utf8');
|
||||
if (versionContent) {
|
||||
await notify.sendNotify('配置文件更新通知', versionContent);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
function Env(t, e) {
|
||||
class s {
|
||||
constructor(t) {
|
||||
this.env = t;
|
||||
}
|
||||
send(t, e = 'GET') {
|
||||
t = 'string' == typeof t ? { url: t } : t;
|
||||
let s = this.get;
|
||||
return (
|
||||
'POST' === e && (s = this.post),
|
||||
new Promise((e, i) => {
|
||||
s.call(this, t, (t, s, r) => {
|
||||
t ? i(t) : e(s);
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
get(t) {
|
||||
return this.send.call(this.env, t);
|
||||
}
|
||||
post(t) {
|
||||
return this.send.call(this.env, t, 'POST');
|
||||
}
|
||||
}
|
||||
return new (class {
|
||||
constructor(t, e) {
|
||||
(this.name = t),
|
||||
(this.http = new s(this)),
|
||||
(this.data = null),
|
||||
(this.dataFile = 'box.dat'),
|
||||
(this.logs = []),
|
||||
(this.isMute = !1),
|
||||
(this.isNeedRewrite = !1),
|
||||
(this.logSeparator = '\n'),
|
||||
(this.startTime = new Date().getTime()),
|
||||
Object.assign(this, e),
|
||||
this.log('', `\ud83d\udd14${this.name}, \u5f00\u59cb!`);
|
||||
}
|
||||
isNode() {
|
||||
return 'undefined' != typeof module && !!module.exports;
|
||||
}
|
||||
isQuanX() {
|
||||
return 'undefined' != typeof $task;
|
||||
}
|
||||
isSurge() {
|
||||
return 'undefined' != typeof $httpClient && 'undefined' == typeof $loon;
|
||||
}
|
||||
isLoon() {
|
||||
return 'undefined' != typeof $loon;
|
||||
}
|
||||
toObj(t, e = null) {
|
||||
try {
|
||||
return JSON.parse(t);
|
||||
} catch {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
toStr(t, e = null) {
|
||||
try {
|
||||
return JSON.stringify(t);
|
||||
} catch {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
getjson(t, e) {
|
||||
let s = e;
|
||||
const i = this.getdata(t);
|
||||
if (i)
|
||||
try {
|
||||
s = JSON.parse(this.getdata(t));
|
||||
} catch {}
|
||||
return s;
|
||||
}
|
||||
setjson(t, e) {
|
||||
try {
|
||||
return this.setdata(JSON.stringify(t), e);
|
||||
} catch {
|
||||
return !1;
|
||||
}
|
||||
}
|
||||
getScript(t) {
|
||||
return new Promise((e) => {
|
||||
this.get({ url: t }, (t, s, i) => e(i));
|
||||
});
|
||||
}
|
||||
runScript(t, e) {
|
||||
return new Promise((s) => {
|
||||
let i = this.getdata('@chavy_boxjs_userCfgs.httpapi');
|
||||
i = i ? i.replace(/\n/g, '').trim() : i;
|
||||
let r = this.getdata('@chavy_boxjs_userCfgs.httpapi_timeout');
|
||||
(r = r ? 1 * r : 20), (r = e && e.timeout ? e.timeout : r);
|
||||
const [o, h] = i.split('@'),
|
||||
a = {
|
||||
url: `http://${h}/v1/scripting/evaluate`,
|
||||
body: { script_text: t, mock_type: 'cron', timeout: r },
|
||||
headers: { 'X-Key': o, Accept: '*/*' },
|
||||
};
|
||||
this.post(a, (t, e, i) => s(i));
|
||||
}).catch((t) => this.logErr(t));
|
||||
}
|
||||
loaddata() {
|
||||
if (!this.isNode()) return {};
|
||||
{
|
||||
(this.fs = this.fs ? this.fs : require('fs')),
|
||||
(this.path = this.path ? this.path : require('path'));
|
||||
const t = this.path.resolve(this.dataFile),
|
||||
e = this.path.resolve(process.cwd(), this.dataFile),
|
||||
s = this.fs.existsSync(t),
|
||||
i = !s && this.fs.existsSync(e);
|
||||
if (!s && !i) return {};
|
||||
{
|
||||
const i = s ? t : e;
|
||||
try {
|
||||
return JSON.parse(this.fs.readFileSync(i));
|
||||
} catch (t) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
writedata() {
|
||||
if (this.isNode()) {
|
||||
(this.fs = this.fs ? this.fs : require('fs')),
|
||||
(this.path = this.path ? this.path : require('path'));
|
||||
const t = this.path.resolve(this.dataFile),
|
||||
e = this.path.resolve(process.cwd(), this.dataFile),
|
||||
s = this.fs.existsSync(t),
|
||||
i = !s && this.fs.existsSync(e),
|
||||
r = JSON.stringify(this.data);
|
||||
s
|
||||
? this.fs.writeFileSync(t, r)
|
||||
: i
|
||||
? this.fs.writeFileSync(e, r)
|
||||
: this.fs.writeFileSync(t, r);
|
||||
}
|
||||
}
|
||||
lodash_get(t, e, s) {
|
||||
const i = e.replace(/\[(\d+)\]/g, '.$1').split('.');
|
||||
let r = t;
|
||||
for (const t of i) if (((r = Object(r)[t]), void 0 === r)) return s;
|
||||
return r;
|
||||
}
|
||||
lodash_set(t, e, s) {
|
||||
return Object(t) !== t
|
||||
? t
|
||||
: (Array.isArray(e) || (e = e.toString().match(/[^.[\]]+/g) || []),
|
||||
(e
|
||||
.slice(0, -1)
|
||||
.reduce(
|
||||
(t, s, i) =>
|
||||
Object(t[s]) === t[s]
|
||||
? t[s]
|
||||
: (t[s] = Math.abs(e[i + 1]) >> 0 == +e[i + 1] ? [] : {}),
|
||||
t,
|
||||
)[e[e.length - 1]] = s),
|
||||
t);
|
||||
}
|
||||
getdata(t) {
|
||||
let e = this.getval(t);
|
||||
if (/^@/.test(t)) {
|
||||
const [, s, i] = /^@(.*?)\.(.*?)$/.exec(t),
|
||||
r = s ? this.getval(s) : '';
|
||||
if (r)
|
||||
try {
|
||||
const t = JSON.parse(r);
|
||||
e = t ? this.lodash_get(t, i, '') : e;
|
||||
} catch (t) {
|
||||
e = '';
|
||||
}
|
||||
}
|
||||
return e;
|
||||
}
|
||||
setdata(t, e) {
|
||||
let s = !1;
|
||||
if (/^@/.test(e)) {
|
||||
const [, i, r] = /^@(.*?)\.(.*?)$/.exec(e),
|
||||
o = this.getval(i),
|
||||
h = i ? ('null' === o ? null : o || '{}') : '{}';
|
||||
try {
|
||||
const e = JSON.parse(h);
|
||||
this.lodash_set(e, r, t), (s = this.setval(JSON.stringify(e), i));
|
||||
} catch (e) {
|
||||
const o = {};
|
||||
this.lodash_set(o, r, t), (s = this.setval(JSON.stringify(o), i));
|
||||
}
|
||||
} else s = this.setval(t, e);
|
||||
return s;
|
||||
}
|
||||
getval(t) {
|
||||
return this.isSurge() || this.isLoon()
|
||||
? $persistentStore.read(t)
|
||||
: this.isQuanX()
|
||||
? $prefs.valueForKey(t)
|
||||
: this.isNode()
|
||||
? ((this.data = this.loaddata()), this.data[t])
|
||||
: (this.data && this.data[t]) || null;
|
||||
}
|
||||
setval(t, e) {
|
||||
return this.isSurge() || this.isLoon()
|
||||
? $persistentStore.write(t, e)
|
||||
: this.isQuanX()
|
||||
? $prefs.setValueForKey(t, e)
|
||||
: this.isNode()
|
||||
? ((this.data = this.loaddata()),
|
||||
(this.data[e] = t),
|
||||
this.writedata(),
|
||||
!0)
|
||||
: (this.data && this.data[e]) || null;
|
||||
}
|
||||
initGotEnv(t) {
|
||||
(this.got = this.got ? this.got : require('got')),
|
||||
(this.cktough = this.cktough ? this.cktough : require('tough-cookie')),
|
||||
(this.ckjar = this.ckjar ? this.ckjar : new this.cktough.CookieJar()),
|
||||
t &&
|
||||
((t.headers = t.headers ? t.headers : {}),
|
||||
void 0 === t.headers.Cookie &&
|
||||
void 0 === t.cookieJar &&
|
||||
(t.cookieJar = this.ckjar));
|
||||
}
|
||||
get(t, e = () => {}) {
|
||||
t.headers &&
|
||||
(delete t.headers['Content-Type'], delete t.headers['Content-Length']),
|
||||
this.isSurge() || this.isLoon()
|
||||
? (this.isSurge() &&
|
||||
this.isNeedRewrite &&
|
||||
((t.headers = t.headers || {}),
|
||||
Object.assign(t.headers, { 'X-Surge-Skip-Scripting': !1 })),
|
||||
$httpClient.get(t, (t, s, i) => {
|
||||
!t && s && ((s.body = i), (s.statusCode = s.status)), e(t, s, i);
|
||||
}))
|
||||
: this.isQuanX()
|
||||
? (this.isNeedRewrite &&
|
||||
((t.opts = t.opts || {}), Object.assign(t.opts, { hints: !1 })),
|
||||
$task.fetch(t).then(
|
||||
(t) => {
|
||||
const { statusCode: s, statusCode: i, headers: r, body: o } = t;
|
||||
e(null, { status: s, statusCode: i, headers: r, body: o }, o);
|
||||
},
|
||||
(t) => e(t),
|
||||
))
|
||||
: this.isNode() &&
|
||||
(this.initGotEnv(t),
|
||||
this.got(t)
|
||||
.on('redirect', (t, e) => {
|
||||
try {
|
||||
if (t.headers['set-cookie']) {
|
||||
const s = t.headers['set-cookie']
|
||||
.map(this.cktough.Cookie.parse)
|
||||
.toString();
|
||||
this.ckjar.setCookieSync(s, null),
|
||||
(e.cookieJar = this.ckjar);
|
||||
}
|
||||
} catch (t) {
|
||||
this.logErr(t);
|
||||
}
|
||||
})
|
||||
.then(
|
||||
(t) => {
|
||||
const {
|
||||
statusCode: s,
|
||||
statusCode: i,
|
||||
headers: r,
|
||||
body: o,
|
||||
} = t;
|
||||
e(null, { status: s, statusCode: i, headers: r, body: o }, o);
|
||||
},
|
||||
(t) => {
|
||||
const { message: s, response: i } = t;
|
||||
e(s, i, i && i.body);
|
||||
},
|
||||
));
|
||||
}
|
||||
post(t, e = () => {}) {
|
||||
if (
|
||||
(t.body &&
|
||||
t.headers &&
|
||||
!t.headers['Content-Type'] &&
|
||||
(t.headers['Content-Type'] = 'application/x-www-form-urlencoded'),
|
||||
t.headers && delete t.headers['Content-Length'],
|
||||
this.isSurge() || this.isLoon())
|
||||
)
|
||||
this.isSurge() &&
|
||||
this.isNeedRewrite &&
|
||||
((t.headers = t.headers || {}),
|
||||
Object.assign(t.headers, { 'X-Surge-Skip-Scripting': !1 })),
|
||||
$httpClient.post(t, (t, s, i) => {
|
||||
!t && s && ((s.body = i), (s.statusCode = s.status)), e(t, s, i);
|
||||
});
|
||||
else if (this.isQuanX())
|
||||
(t.method = 'POST'),
|
||||
this.isNeedRewrite &&
|
||||
((t.opts = t.opts || {}), Object.assign(t.opts, { hints: !1 })),
|
||||
$task.fetch(t).then(
|
||||
(t) => {
|
||||
const { statusCode: s, statusCode: i, headers: r, body: o } = t;
|
||||
e(null, { status: s, statusCode: i, headers: r, body: o }, o);
|
||||
},
|
||||
(t) => e(t),
|
||||
);
|
||||
else if (this.isNode()) {
|
||||
this.initGotEnv(t);
|
||||
const { url: s, ...i } = t;
|
||||
this.got.post(s, i).then(
|
||||
(t) => {
|
||||
const { statusCode: s, statusCode: i, headers: r, body: o } = t;
|
||||
e(null, { status: s, statusCode: i, headers: r, body: o }, o);
|
||||
},
|
||||
(t) => {
|
||||
const { message: s, response: i } = t;
|
||||
e(s, i, i && i.body);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
time(t) {
|
||||
let e = {
|
||||
'M+': new Date().getMonth() + 1,
|
||||
'd+': new Date().getDate(),
|
||||
'H+': new Date().getHours(),
|
||||
'm+': new Date().getMinutes(),
|
||||
's+': new Date().getSeconds(),
|
||||
'q+': Math.floor((new Date().getMonth() + 3) / 3),
|
||||
S: new Date().getMilliseconds(),
|
||||
};
|
||||
/(y+)/.test(t) &&
|
||||
(t = t.replace(
|
||||
RegExp.$1,
|
||||
(new Date().getFullYear() + '').substr(4 - RegExp.$1.length),
|
||||
));
|
||||
for (let s in e)
|
||||
new RegExp('(' + s + ')').test(t) &&
|
||||
(t = t.replace(
|
||||
RegExp.$1,
|
||||
1 == RegExp.$1.length
|
||||
? e[s]
|
||||
: ('00' + e[s]).substr(('' + e[s]).length),
|
||||
));
|
||||
return t;
|
||||
}
|
||||
msg(e = t, s = '', i = '', r) {
|
||||
const o = (t) => {
|
||||
if (!t) return t;
|
||||
if ('string' == typeof t)
|
||||
return this.isLoon()
|
||||
? t
|
||||
: this.isQuanX()
|
||||
? { 'open-url': t }
|
||||
: this.isSurge()
|
||||
? { url: t }
|
||||
: void 0;
|
||||
if ('object' == typeof t) {
|
||||
if (this.isLoon()) {
|
||||
let e = t.openUrl || t.url || t['open-url'],
|
||||
s = t.mediaUrl || t['media-url'];
|
||||
return { openUrl: e, mediaUrl: s };
|
||||
}
|
||||
if (this.isQuanX()) {
|
||||
let e = t['open-url'] || t.url || t.openUrl,
|
||||
s = t['media-url'] || t.mediaUrl;
|
||||
return { 'open-url': e, 'media-url': s };
|
||||
}
|
||||
if (this.isSurge()) {
|
||||
let e = t.url || t.openUrl || t['open-url'];
|
||||
return { url: e };
|
||||
}
|
||||
}
|
||||
};
|
||||
this.isMute ||
|
||||
(this.isSurge() || this.isLoon()
|
||||
? $notification.post(e, s, i, o(r))
|
||||
: this.isQuanX() && $notify(e, s, i, o(r)));
|
||||
let h = [
|
||||
'',
|
||||
'==============\ud83d\udce3\u7cfb\u7edf\u901a\u77e5\ud83d\udce3==============',
|
||||
];
|
||||
h.push(e),
|
||||
s && h.push(s),
|
||||
i && h.push(i),
|
||||
console.log(h.join('\n')),
|
||||
(this.logs = this.logs.concat(h));
|
||||
}
|
||||
log(...t) {
|
||||
t.length > 0 && (this.logs = [...this.logs, ...t]),
|
||||
console.log(t.join(this.logSeparator));
|
||||
}
|
||||
logErr(t, e) {
|
||||
const s = !this.isSurge() && !this.isQuanX() && !this.isLoon();
|
||||
s
|
||||
? this.log('', `\u2757\ufe0f${this.name}, \u9519\u8bef!`, t.stack)
|
||||
: this.log('', `\u2757\ufe0f${this.name}, \u9519\u8bef!`, t);
|
||||
}
|
||||
wait(t) {
|
||||
return new Promise((e) => setTimeout(e, t));
|
||||
}
|
||||
done(t = {}) {
|
||||
const e = new Date().getTime(),
|
||||
s = (e - this.startTime) / 1e3;
|
||||
this.log(
|
||||
'',
|
||||
`\ud83d\udd14${this.name}, \u7ed3\u675f! \ud83d\udd5b ${s} \u79d2`,
|
||||
),
|
||||
this.log(),
|
||||
(this.isSurge() || this.isQuanX() || this.isLoon()) && $done(t);
|
||||
}
|
||||
})(t, e);
|
||||
}
|
||||
Reference in New Issue
Block a user