diff --git a/shell/api.sh b/shell/api.sh index 25cdb988..7be48f48 100755 --- a/shell/api.sh +++ b/shell/api.sh @@ -22,10 +22,10 @@ get_json_value() { } add_cron_api() { - local currentTimeStamp=$(date +%s%3) + local currentTimeStamp=$(date +%s) if [ $# -eq 1 ]; then - local schedule=$(echo "$1" | awk -F ": " '{print $1}') - local command=$(echo "$1" | awk -F ": " '{print $2}') + local schedule=$(echo "$1" | awk -F ":" '{print $1}') + local command=$(echo "$1" | awk -F ":" '{print $2}') local name=$(echo "$1" | awk -F ": " '{print $3}') else local schedule=$1 @@ -54,7 +54,7 @@ add_cron_api() { del_cron_api() { local id=$1 - local currentTimeStamp=$(date +%s%3) + local currentTimeStamp=$(date +%s) local api=$(curl "http://localhost:5678/api/crons/$id?t=$currentTimeStamp" \ -X 'DELETE' \ -H "Accept: application/json" \ diff --git a/shell/git_diy.sh b/shell/git_diy.sh index edda47a6..6bee1957 100755 --- a/shell/git_diy.sh +++ b/shell/git_diy.sh @@ -7,7 +7,7 @@ repo=$2 path=$3 blackword=$4 -if [[ $# -lt 2 ]] || [[ $# -gt 4 ]] ; then +if [[ $# -lt 2 ]] || [[ $# -gt 4 ]]; then echo 'Desc: 用户拉取指定用户的指定仓储' echo 'Usage: diy ' @@ -15,7 +15,7 @@ if [[ $# -lt 2 ]] || [[ $# -gt 4 ]] ; then echo 'repo 仓储名' echo 'path 需要下载脚本的指定目录,多个目录 | 分割 path1 | path2' echo 'blacklist 需要排除的脚本名,多个名称 | 分割 blacklist1 | blacklist2' - exit 0; + exit 0 fi diyscriptsdir=/ql/diyscripts @@ -23,70 +23,80 @@ mkdir -p ${diyscriptsdir} if [ ! -d "$diyscriptsdir/${author}_${repo}" ]; then echo -e "${author}本地仓库不存在,从gayhub拉取ing..." - cd ${diyscriptsdir} && git clone https://github.com.cnpmjs.org/${author}/${repo}.git ${author}_${repo} + cd ${diyscriptsdir} && git clone https://github.com.cnpmjs.org/${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` + 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)) +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 { +addnewcron() { addname="" cd ${diyscriptsdir}/${author}_${repo} express=$(find . -name "*.js") - if [ $path ];then + if [ $path ]; then express=$(find . -name "*.js" | egrep $path) fi - if [ $blackword ];then + if [ $blackword ]; then express=$(find . -name "*.js" | egrep -v $blackword | egrep $path) fi - for js in $express; - do - base=`basename $js` - croname=`echo "${author}_$base"|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" /ql/config/crontab.list) -eq 0 ] && sed -i "/hangup/a${script_date} js $croname" /ql/config/crontab.list && addname="${addname}\n${croname}" && echo -e "添加了新的脚本${croname}." - if [ ! -f "/ql/scripts/${author}_$base" ];then - \cp $js /ql/scripts/${author}_$base - else - change=$(diff $js /ql/scripts/${author}_$base) - [ -n "${change}" ] && \cp $js /ql/scripts/${author}_$base && echo -e "${author}_$base 脚本更新了." - fi + for js in $express; do + base=$(basename $js) + croname=$(echo "${author}_$base" | 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} * * *" + local oldCron=$(grep -c -w "$croname" "$ListCronCurrent") + if [[ oldCron -eq 0 ]]; then + add_cron_api "$script_date" "js $croname" "$croname" + addname="${addname}\n${croname}" + echo -e "添加了新的脚本${croname}." + fi + if [ ! -f "/ql/scripts/${author}_$base" ]; then + \cp $js /ql/scripts/${author}_$base + else + change=$(diff $js /ql/scripts/${author}_$base) + [ -n "${change}" ] && \cp $js /ql/scripts/${author}_$base && echo -e "${author}_$base 脚本更新了." + fi done [ "$addname" != "" ] && notify "新增 ${author} 自定义脚本" "${addname}" } -function delcron { +delcron() { delname="" - cronfiles=$(grep "$author" /ql/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 "/\/d" /ql/config/crontab.list && echo -e "删除失效脚本${filename}." - delname="${delname}\n${author}_${filename}" - fi + cronfiles=$(grep "$author" /ql/config/crontab.list | grep -v "^#" | perl -pe "s|.*ID=(.*) js (${author}_.*)\.*|\1:\2|") + for filename in $cronfiles; do + local id=$(echo "$1" | awk -F ":" '{print $1}') + local name=$(echo "$1" | awk -F ":" '{print $2}') + hasFile=$(cd ${diyscriptsdir}/${author}_${repo} && find . -name "$filename.js" | wc -l) + if [[ $hasFile != 0 ]]; then + del_cron_api "$id" + echo -e "删除失效脚本${name}." + delname="${delname}\n${author}_${filename}" + fi done - [ "$delname" != "" ] && notify "删除 ${author} 失效脚本" "${delname}" + [ "$delname" != "" ] && notify "删除 ${author} 失效脚本" "${delname}" } -if [[ ${gitpullstatus} -eq 0 ]] -then +. $ShellDir/shell/variables.sh +. $ShellDir/shell/api.sh +get_token + +if [[ ${gitpullstatus} -eq 0 ]]; then addnewcron delcron else diff --git a/shell/git_pull.sh b/shell/git_pull.sh index 7dae014c..f128395f 100755 --- a/shell/git_pull.sh +++ b/shell/git_pull.sh @@ -1,39 +1,5 @@ #!/usr/bin/env bash -## 常量 -ShellDir=${QL_DIR:-$( - cd $(dirname $0) - pwd -)} -[[ $QL_DIR ]] && ShellJs=js -LogDir=$ShellDir/log -[ ! -d $LogDir ] && mkdir -p $LogDir -DbDir=$ShellDir/db -[ ! -d $DbDir ] && mkdir -p $DbDir -ManualLogDir=$ShellDir/manual_log -[ ! -d $ManualLogDir ] && mkdir -p $ManualLogDir -ScriptsDir=$ShellDir/scripts -ConfigDir=$ShellDir/config -FileConf=$ConfigDir/config.sh -CookieConf=$ConfigDir/cookie.sh -AuthConf=$ConfigDir/auth.json -ExtraShell=$ConfigDir/extra.sh -FileConfSample=$ShellDir/sample/config.sh.sample -ListCronSample=$ShellDir/sample/crontab.list.sample -ListCronCurrent=$ConfigDir/crontab.list -ListCronRemote=$ScriptsDir/docker/crontab_list.sh -ListCurrentTask=$LogDir/task.list -ListRemoteTask=$LogDir/js.list -ListJsAdd=$LogDir/js-add.list -ListJsDrop=$LogDir/js-drop.list -ContentVersion=$ShellDir/version -ContentNewTask=$ShellDir/new_task -ContentDropTask=$ShellDir/drop_task -SendVersion=$ShellDir/send_version -isTermux=$ANDROID_RUNTIME_ROOT$ANDROID_ROOT -ShellURL=https://github.com.cnpmjs.org/whyour/qinglong -ScriptsURL=https://github.com.cnpmjs.org/gossh520/jd_scripts - Import_Conf() { if [ ! -s $FileConf ]; then echo -e "复制一份 $FileConfSample 示例配置文件\n\n" @@ -45,8 +11,6 @@ Import_Conf() { fi [ -f $CookieConf ] && . $CookieConf [ -f $FileConf ] && . $FileConf - - . $ShellDir/shell/api.sh } # 更新shell @@ -147,12 +111,12 @@ Random_Pull_Cron() { 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|.+(git_pull? .+git_pull\.log.*)|$RanMin $RanHour \* \* \* sleep $RanSleep && \1|" $ListCronCurrent crontab $ListCronCurrent fi @@ -201,7 +165,7 @@ Git_Pull_Scripts_Next() { Diff_Cron() { cat $ListCronRemote | grep -E "node.+j[drx]_\w+\.js" | perl -pe "s|.+(j[drx]_\w+)\.js.+|\1|" | sort -u >$ListRemoteTask - cat $ListCronCurrent | grep -E "$ShellJs j[drx]_\w+" | perl -pe "s|.*ID=(.*)$ShellJs (j[drx]_\w+)\.*|\1|" | sort -u >$ListCurrentTask + cat $ListCronCurrent | grep -E "$ShellJs j[drx]_\w+" | perl -pe "s|.*ID=(.*)$ShellJs (j[drx]_\w+)\.*|\1:\2|" | sort -u >$ListCurrentTask if [ -s $ListCurrentTask ]; then grep -vwf $ListCurrentTask $ListRemoteTask >$ListJsAdd else @@ -221,7 +185,9 @@ Del_Cron() { echo JsDrop=$(cat $ListJsDrop) for Cron in $JsDrop; do - del_cron_api "$Cron" + local id=$(echo "$1" | awk -F ":" '{print $1}') + local name=$(echo "$1" | awk -F ":" '{print $2}') + del_cron_api "$id" done crontab $ListCronCurrent echo -e "成功删除失效的脚本与定时任务\n" @@ -234,10 +200,12 @@ Add_Cron() { echo -e "开始尝试自动添加定时任务\n" JsAdd=$(cat $ListJsAdd) for Cron in $JsAdd; do - if [[ $Cron == jd_bean_sign ]]; then - echo "4 0,9 * * * $ShellJs $Cron" >> $ListCronCurrent + local id=$(echo "$1" | awk -F ":" '{print $1}') + local name=$(echo "$1" | awk -F ":" '{print $2}') + if [[ $name == jd_bean_sign ]]; then + echo "4 0,9 * * * $ShellJs $name" >>$ListCronCurrent else - param=$(cat $ListCronRemote | grep -E "\/$Cron\." | perl -pe "s|(^.+)node */scripts/(j[drx]_\w+)\.js.+|\1\: $ShellJs \2: \2|") + param=$(cat $ListCronRemote | grep -E "\/$name\." | perl -pe "s|(^.+)node */scripts/(j[drx]_\w+)\.js.+|\1\:$ShellJs \2:\2|") add_cron_api "$param" fi done @@ -275,11 +243,13 @@ fi echo -e "\nJS脚本目录:$ScriptsDir\n" echo -e "--------------------------------------------------------------\n" +. $ShellDir/shell/variables.sh +. $ShellDir/shell/api.sh +get_token + Import_Conf Random_Pull_Cron -get_token - # 更新shell [ -f $ShellDir/package.json ] && PanelDependOld=$(cat $ShellDir/package.json) Git_Pull_Shell diff --git a/shell/variables.sh b/shell/variables.sh new file mode 100755 index 00000000..37106043 --- /dev/null +++ b/shell/variables.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +ShellDir=${QL_DIR:-$( + cd $(dirname $0) + pwd +)} +[[ $QL_DIR ]] && ShellJs=js +LogDir=$ShellDir/log +[ ! -d $LogDir ] && mkdir -p $LogDir +DbDir=$ShellDir/db +[ ! -d $DbDir ] && mkdir -p $DbDir +ManualLogDir=$ShellDir/manual_log +[ ! -d $ManualLogDir ] && mkdir -p $ManualLogDir +ScriptsDir=$ShellDir/scripts +ConfigDir=$ShellDir/config +FileConf=$ConfigDir/config.sh +CookieConf=$ConfigDir/cookie.sh +AuthConf=$ConfigDir/auth.json +ExtraShell=$ConfigDir/extra.sh +FileConfSample=$ShellDir/sample/config.sh.sample +ListCronSample=$ShellDir/sample/crontab.list.sample +ListCronCurrent=$ConfigDir/crontab.list +ListCronRemote=$ScriptsDir/docker/crontab_list.sh +ListCurrentTask=$LogDir/task.list +ListRemoteTask=$LogDir/js.list +ListJsAdd=$LogDir/js-add.list +ListJsDrop=$LogDir/js-drop.list +ContentVersion=$ShellDir/version +ContentNewTask=$ShellDir/new_task +ContentDropTask=$ShellDir/drop_task +SendVersion=$ShellDir/send_version +isTermux=$ANDROID_RUNTIME_ROOT$ANDROID_ROOT +ShellURL=https://github.com.cnpmjs.org/whyour/qinglong +ScriptsURL=https://github.com.cnpmjs.org/gossh520/jd_scripts \ No newline at end of file