[ [0, 1, 1, 0, 0, 0, 1, 1, 0, 0], [1, 1, 1, 1, 0, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0] ]
下面的代码是根据当前日期和模板对应的值来进行提交,用于定时任务每天执行。可以直接部署到服务器,通过后面的命令设置定时任务。
main.py#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import os import time import datetime def calculate_date(start, end): # 计算日期相差天数 start_sec = time.mktime(time.strptime(start, '%Y-%m-%d')) end_sec = time.mktime(time.strptime(end, '%Y-%m-%d')) days = int((end_sec - start_sec) / (24 * 60 * 60)) return days def commit(flag): if flag: for n in range(49): # 设置commit次数 with open('./record.txt', 'a') as record: record.write('.') record.close() os.system('git commit -a -m \"HeartBeat\"') else: # 每天推一条 with open('./record.txt', 'a') as record: record.write('.') record.close() os.system('git commit -a -m \"HeartBeat\"') os.system('git pull && git push origin master') with open('./model.json') as f: # 加载模型 PATTEN = json.loads(f.read()) f.close() PERIOD = len(PATTEN[0]) # 周期(图案列数) START_DATE = '2017-7-16' # 开始日期,很重要,左上角提一格的日期,自己手动修改 now = datetime.datetime.now().strftime('%Y-%m-%d') row = calculate_date(START_DATE, now) % 7 col = int(calculate_date(START_DATE, now) / 7) % PERIOD commit(PATTEN[row][col])
开启定时任务
crontab -e # 输入以下代码,前两个参数分别是分钟和小时,该任务为每天12:00定时执行 # 00 12 * * * cd /home/git_heart && git pull && /usr/bin/python main.py
2、定时任务只能帮我完成今天及以后的事情,之前的也需要写个脚本跑一下。
loop.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import os import time import datetime def calculate_date(start, end): # 计算日期相差天数 start_sec = time.mktime(time.strptime(start, '%Y-%m-%d')) end_sec = time.mktime(time.strptime(end, '%Y-%m-%d')) days = int((end_sec - start_sec) / (24 * 60 * 60)) return days def add_days(d, num): # 日期递增 sec = num * 24 * 60 * 60 now_sec = time.mktime(time.strptime(d, '%Y-%m-%d')) + sec return time.strftime("%Y-%m-%d", time.localtime(now_sec)) def commit(flag): if flag: for n in range(49): with open('./record.txt', 'a') as record: record.write('.') record.close() os.system('git commit -a -m \"HeartBeat\"') with open('./record.txt', 'a') as record: record.write('\n') record.close() os.system('git commit -a -m \"HeartBeat\"') else: with open('./record.txt', 'a') as record: record.write(now + '\n') record.close() os.system('git commit -a -m \"HeartBeat\"') with open('./model.json') as f: # 加载模型 PATTEN = json.loads(f.read()) f.close() PERIOD = len(PATTEN[0]) # 周期(图案列数) START_DATE = '2017-7-16' # 开始日期, 码云和git显示不一样, 建议从最左上角开始 now = datetime.datetime.now().strftime('%Y-%m-%d') os.system('timedatectl set-ntp false') # 关闭时间自动同步 while calculate_date(START_DATE, now) >= 0: row = calculate_date(START_DATE, now) % 7 col = int(calculate_date(START_DATE, now) / 7) % PERIOD commit(PATTEN[row][col]) now = add_days(now, -1) os.system('timedatectl set-time ' + now) # 复原时间 os.system('timedatectl set-ntp 1 && timedatectl set-local-rtc 1')
到这里基本就结束了,第三个目标实际上在第一步就已经完成了,下面上一下测试结果。
效果展示
1、小心心
[ [0, 1, 1, 0, 0, 0, 1, 1, 0, 0], [1, 1, 1, 1, 0, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0] ]
2、X
[ [0, 0, 0, 0, 0], [1, 0, 0, 0, 1], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1], [0, 0, 0, 0, 0] ]
想提高技术获取干货的朋友可以加下哦 Python技术交流群733736235