MihoyoBBSTools/docker.py
2022-04-03 13:54:16 +08:00

58 lines
1.4 KiB
Python

import datetime
import os
import logging
import signal
import time
#from settings import *
from crontab import CronTab
time_format = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%Y-%m-%dT%H:%M:%S')
log = logging
def stop_me(_signo, _stack):
log.info("Docker container has stoped....")
exit(-1)
def main():
signal.signal(signal.SIGINT, stop_me)
log.info("使用DOCKER运行米游社签到")
env = os.environ
cron_signin = env["CRON_SIGNIN"]
cron = CronTab(cron_signin, loop=True, random_seconds=True)
def next_run_time():
nt = datetime.datetime.now().strftime(time_format)
delayt = cron.next(default_utc=False)
nextrun = datetime.datetime.now() + datetime.timedelta(seconds=delayt)
nextruntime = nextrun.strftime(time_format)
log.info(f"Current running datetime: {nt}")
log.info(f"Next run datetime: {nextruntime}")
def sign():
log.info("Starting signing")
multi = env["MULTI"]
if multi:
os.system("python3 ./main_multi.py autorun")
else:
os.system("python3 ./main.py")
sign()
next_run_time()
while True:
ct = cron.next(default_utc=False)
time.sleep(ct)
sign()
next_run_time()
if __name__ == '__main__':
main()