用 Python 玩转 GitHub 的贡献板

    细心的人都会发现GitHub我的主页有一个记录天天贡献次数的面板,我暂且称之为贡献面板。就像下图那个样子。只要当天在GitHub有提交记录,对应的小格子就会变成绿色,当天提交次数越多,颜色也会越深。所以我就有了一个大胆的想法。细心的你应该也发现了,我就是要讲如何搞出这个小❤️❤️来。学习地址: Python全栈/爬虫/运维/Flask/Openstack Django【达亚学院】https://ke.qq.com/course/397896?flowToken=1009458 基本原理前面已经讲过,咱们只须要控制项目提交的日期和次数,就能在贡献面板中填充出花样来。可能有朋友会问,前面的部分怎么办?拿到我得等一年?No☝️,通过研究发现,GitHub的贡献面板是活得,什么意思呢?GitHub是根据项目的提交记录时时生成的贡献面板,因此只要在本地把时间改为过去,进行提交操做,再push到GitHub,就实现了穿越。了解了这些以后,就能够动手了。最后若是效果不尽人意或者像换个图样换个心情,只须要删了对应的仓库就好了。 坑1:码云不像GitHub,码云的贡献面板是一次性的,推上去以后就不会变,因此谨慎操做 程序设计 目标 一、设计一个模板,能够经过修改模板来改变图样 二、将过去时间的记录自动所有填充 三、天天进行自动COMMIT/PUSH操做 实现 一、模板设计成一个json二维数组,由0和1组成,分别到表有/无提交记录。行数最好固定是7(周一到周日),列数能够自已随意设置。下面是基于python的实现。 model.json 

     [ [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] ] python

     下面的代码是根据当前日期和模板对应的值来进行提交,用于定时任务天天执行。能够直接部署到服务器,经过后面的命令设置定时任务。git

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])json

      开启定时任务数组

crontab -e # 输入如下代码,前两个参数分别是分钟和小时,该任务为天天12:00定时执行 # 00 12 * * * cd /home/git_heart && git pull && /usr/bin/python main.py
服务器

     二、定时任务只能帮我完成今天及之后的事情,以前的也须要写个脚本跑一下。
运维

loop.pyoop

     #!/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')学习

      到这里基本就结束了,第三个目标实际上在第一步就已经完成了,下面上一下测试结果。测试

效果展现
一、当心心设计

[ [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] ]

 

二、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   

相关文章
相关标签/搜索