代码拉取完成,页面将自动刷新
#!/usr/bin/python3
import os
import random
import subprocess
import time
import cv2
import datetime
import schedule
import numpy as np
def get_latest_image(data_dir):
files = os.listdir(data_dir)
if not files:
return None
files.sort(key=lambda x: os.path.getmtime(os.path.join(data_dir, x)), reverse=True)
return os.path.join(data_dir, files[0])
def compare_images(img1_path, img2_path, threshold=0.9):
image1 = cv2.imread(img1_path)
image2 = cv2.imread(img2_path)
if image1 is None and image2 is None:
return False
result = cv2.matchTemplate(image1, image2, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
if max_val >= threshold:
print("相似度"+str(max_val))
return True
else:
print("相似度"+str(max_val))
return False
def main(img1_path, img2_path, data_dir, command, max_attempts=10):
attempts = 0
while attempts < max_attempts:
latest_image = get_latest_image(data_dir)
if not latest_image:
print("目录中没有照片。")
subprocess.run(command, shell=True)
break
subprocess.run(command, shell=True)
latest_image_after_command = get_latest_image(data_dir)
if latest_image == latest_image_after_command:
print("执行后照片没有变化,重新执行指令。")
else:
if compare_images(img1_path, latest_image_after_command) or compare_images(img2_path,
latest_image_after_command):
print("照片内容一致,任务完成。")
break
else:
print("照片内容不一致,重新执行指令。")
time.sleep(3)
attempts += 1
if attempts == max_attempts:
print("达到最大尝试次数,任务失败。")
def start():
current_script_path = os.path.abspath(__file__)
path_img1 = os.path.join(os.path.dirname(current_script_path), "data", "image1.png")
path_img2 = os.path.join(os.path.dirname(current_script_path), "data", "image2.png")
data_dir = "/volume/scripts_data/duty"
command = "/volume/scripts/duty.sh"
main(path_img1, path_img2, data_dir, command)
def run1():
current_time = datetime.datetime.now()
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
print("开始时间:", formatted_time)
random_delay = random.randint(1, 1800)
print(f"等待时间:{random_delay}")
time.sleep(random_delay)
current_time = datetime.datetime.now()
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
print("结束时间:", formatted_time)
start()
def run2():
# 获取当前日期和时间
current_time = datetime.datetime.now()
# 将时间格式化为字符串并打印
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
print("开始时间:", formatted_time)
random_delay = random.randint(1, 20)
print(f"等待时间:{random_delay}")
time.sleep(random_delay)
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
print("结束时间:", formatted_time)
start()
if __name__ == "__main__":
os.environ["PATH"] = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin"
schedule.every().sunday.at("07:00").do(run1)
schedule.every().monday.at("07:00").do(run1)
schedule.every().tuesday.at("07:00").do(run1)
schedule.every().wednesday.at("07:00").do(run1)
schedule.every().thursday.at("07:00").do(run1)
schedule.every().friday.at("07:00").do(run1)
schedule.every().sunday.at("18:00").do(run2)
schedule.every().monday.at("18:00").do(run2)
schedule.every().tuesday.at("18:00").do(run2)
schedule.every().wednesday.at("18:00").do(run2)
schedule.every().thursday.at("18:00").do(run2)
schedule.every().friday.at("18:00").do(run2)
schedule.run_pending()
while True:
time.sleep(0.1)
schedule.run_pending()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。