加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
play.py 1.96 KB
一键复制 编辑 原始数据 按行查看 历史
yangjq1223 提交于 2021-01-29 08:55 . beat back
#!/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)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化