#!/bin/bash
# 看一看+小程序 朋友圈榜单
# sh ./weixinzhishu_main/weixinzhishu_main.sh ./kanyikan/kanyikan_main/run_kanyikan_moment.py --log_type="moment" --crawler="kanyikan" --strategy="kanyikan_moment" --our_uid="kanyikan_moment" --oss_endpoint="out" --env="dev" ./kanyikan/nohup.log local

crawler_dir=$1  # 爬虫执行路径，如: ./youtube/youtube_main/run_youtube_follow.py
log_type=$2     # 日志命名格式，如: follow，则在 youtube/logs/目录下，生成 2023-02-08-follow.log
crawler=$3      # 哪款爬虫，如: youtube / kanyikan / weixinzhishu
strategy=$4     # 爬虫策略，如: 定向爬虫策略 / 小时榜爬虫策略 / 热榜爬虫策略
oss_endpoint=$5 # OSS网关，内网: inner / 外网: out / 香港: hk
env=$6          # 爬虫运行环境，正式环境: prod / 测试环境: dev
machine=$7      # 爬虫运行机器，阿里云服务器: aliyun_hk / aliyun / macpro / macair / local
nohup_dir=$8    # nohup日志存储路径，如: ./youtube/nohup.log

echo "开始"

if [ ${machine} = "--machine=macpro" ];then
  piaoquan_crawler_dir=/Users/lieyunye/Desktop/piaoquan_crawler/
  profile_path=.bash_profile
  node_path=/usr/local/bin/node
  python=python3
elif [ ${machine} = "--machine=macair" ];then
  piaoquan_crawler_dir=/Users/piaoquan/Desktop/piaoquan_crawler/
  profile_path=./base_profile
  node_path=/usr/local/bin/node
  python=python3
elif [ ${machine} = "--machine=aliyun_hk" ];then
  piaoquan_crawler_dir=/root/piaoquan_crawler/
  profile_path=/etc/profile
  python=python3
elif [ ${machine} = "--machine=aliyun" ];then
  piaoquan_crawler_dir=/data5/piaoquan_crawler/
  profile_path=/etc/profile
  python=python
elif [ ${machine} = "--machine=local" ];then
  piaoquan_crawler_dir=/Users/wangkun/Desktop/crawler/piaoquan_crawler/
  profile_path=/etc/profile
  node_path=/opt/homebrew/bin/node
  python=python3
fi

echo "$(date "+%Y-%m-%d %H:%M:%S") 更新环境变量..."
cd ~ && source ${profile_path}
echo "$(date "+%Y-%m-%d %H:%M:%S") 更新环境变量完成！"

echo "$(date "+%Y-%m-%d %H:%M:%S") 正在杀进程..."
grep_str=run_${crawler##*=}
ps aux | grep ${grep_str} | grep Python | grep -v grep | awk '{print $2}' | xargs kill -9
echo "$(date "+%Y-%m-%d %H:%M:%S") 进程已杀死！"

if [ ${machine} = "--machine=aliyun_hk" ];then
  echo "升级yt-dlp"
  pip3 install yt-dlp -U
else
  echo "$(date "+%Y-%m-%d %H:%M:%S") 正在更新代码..."
  cd ${piaoquan_crawler_dir} && git pull origin master --force && rm -f ${piaoquan_crawler_dir}main/nohup.log && rm -f ${piaoquan_crawler_dir}${nohup_dir}
  echo "$(date "+%Y-%m-%d %H:%M:%S") 代码更新完成！"
fi

if [ ${machine} = "--machine=aliyun_hk" ];then
  echo "无需重启Appium及adb服务"
elif [ ${machine} = "--machine=aliyun" ];then
  echo "无需重启Appium及adb服务"
else
  echo "$(date "+%Y-%m-%d %H:%M:%S") 正在重启Appium..."
  ps aux | grep Appium.app | grep -v grep | awk '{print $2}' | xargs kill -9
  nohup ${node_path} /Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js >>./nohup.log 2>&1 &
  echo "$(date "+%Y-%m-%d %H:%M:%S") 重启Appium完毕!"

  echo "$(date "+%Y-%m-%d %H:%M:%S") 正在重启adb..."
  adb kill-server
  adb start-server
  echo "$(date "+%Y-%m-%d %H:%M:%S") 重启adb完毕!"
fi

echo "$(date "+%Y-%m-%d %H:%M:%S") 正在重启服务..."
cd ${piaoquan_crawler_dir}
nohup ${python} -u ${crawler_dir} ${log_type} ${crawler} ${strategy} ${oss_endpoint} ${env} ${machine} >>${nohup_dir} 2>&1 &
echo "$(date "+%Y-%m-%d %H:%M:%S") 服务重启完毕!"

exit 0