代码拉取完成,页面将自动刷新
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import json
import datetime
import schedule
from playsound import playsound
# 读取配置文件并转为json
def readConfigFile(path):
try:
with open(path, 'r+') as f:
try:
json_data = json.load(f)
except Exception as e:
print('json数据格式不正确: %s', f)
return json_data
except Exception as e:
print("文件不存在")
# 读取当天的配置
def getCurrentDayConfig(config):
current_day = time.strftime("%Y-%m-%d", time.localtime())
config_items = []
for config_item in config.get("schedule"):
if config_item.get("day") == current_day:
config_items.append(config_item)
print(config_items)
return config_items
# 工作
def doTask(config):
if None == config:
return
for config_item in config:
for time_item in config_item.get("time"):
date_time_str = config_item.get("day") + " " + time_item
ti = time.strptime(date_time_str, "%Y-%m-%d %H:%M:%S")
time_stamp = int(time.mktime(ti))
current_time_stamp = int(time.time())
print("current time: %d, time_stamp: %d" %
(current_time_stamp, time_stamp))
if current_time_stamp <= time_stamp and current_time_stamp + 120 > time_stamp:
writeLog()
print("play music start")
playsound('ccc.m4a')
print("play music end")
# 写日志
def writeLog():
current_time_str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
fo = open("log.file", "a+")
fo.write(current_time_str)
fo.write('\n')
fo.close()
def start():
jsonobj = readConfigFile("/Users/yjq/Documents/python/config.txt")
current_config = getCurrentDayConfig(jsonobj)
doTask(current_config)
schedule.every(1).minutes.do(start)
while True:
schedule.run_pending()
time.sleep(1)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。