-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit_commit_art.py
136 lines (120 loc) · 4.3 KB
/
git_commit_art.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import png
import sys
import os
import json
from datetime import datetime, timedelta
import urllib.request
def processflatpixels(flatpixels, pixelqty):
""" Take in flat list of pixels and convert it to a list of r,g,b tuples """
groupedpixels = []
pixelsprocessed = 0
subpixelsprocessed = 0
while pixelsprocessed < pixelqty:
r = flatpixels[subpixelsprocessed]
subpixelsprocessed += 1
g = flatpixels[subpixelsprocessed]
subpixelsprocessed += 1
b = flatpixels[subpixelsprocessed]
subpixelsprocessed += 1
groupedpixels.append((r, g, b))
pixelsprocessed += 1
return groupedpixels
def grouppixelsbyrow(pixels):
""" Convert a list of RGB tuples to a list of rows """
pixelrows = []
for row in range(artheight):
pixelrow = tuple(pixels[row*artwidth:row*artwidth+artwidth])
pixelrows.append(pixelrow)
return pixelrows
def rowstocolumns(rows):
""" Convert a group of pixel rows to a group of columns """
pixcolumns = []
for columnnumber in range(artwidth):
pixcolumns.append([])
for row in rows:
pixcolumns[columnnumber].append(row[columnnumber])
return pixcolumns
def printunicode(pixelrows):
"""
Print out a rough unicode representation of the pixels.
Designed for display on a dark background.
"""
for row in pixelrows:
for pix in row:
if pix == (214, 230, 133):
char = '\u2593'
elif pix == (30, 104, 35):
char = '\u2005\u2005\u2005'
elif pix == (238, 238, 238):
char = '\u2588'
elif pix == (68, 163, 64):
char = '\u2591'
elif pix == (140, 198, 101):
char = '\u2592'
else:
char = ''
sys.stdout.write(char)
sys.stdout.write('\n')
def colortocommitcount(pix):
"""
Convert the git commit colors to numbers used to represent
the number of daily commits which will create that color.
"""
if pix == (214, 230, 133):
commitcount = 1
elif pix == (30, 104, 35):
commitcount = 17
elif pix == (238, 238, 238):
commitcount = 0
elif pix == (68, 163, 64):
commitcount = 12
elif pix == (140, 198, 101):
commitcount = 6
else:
raise ValueError
return commitcount
with open('config.json') as f:
config = json.load(f)
artimage = png.Reader('art.png')
artwidth, artheight, flatartpixels = artimage.read_flat()[0:3]
artpixelrows = grouppixelsbyrow(processflatpixels(flatartpixels, artwidth * artheight))
pixelcolumns = rowstocolumns(artpixelrows)
# Initialize the project directory and file
try:
os.mkdir(config['gitprojectdir'])
except FileExistsError:
pass
os.chdir(config['gitprojectdir'])
open('projectfile', 'a').close()
with open('README.md', 'w') as f:
f.write('Contribution graph art generated by git-commit-art\r\n'
'https://github.com/ryman1/git-commit-art\r\n'
'https://twitter.com/ryman1\r\n')
os.system('git init')
os.system('git config user.email ' + config['gitemail'])
os.system('git add projectfile README.md')
# Determine the commit start date (53 weeks before next closest sunday).
# Github displays 53 up to weeks worth of commits on the contributions graph.
today = datetime.now()
# If today is not Sunday
if today.isoweekday() != 0:
sunday = datetime.now() + timedelta((0 - datetime.now().isoweekday()) % 7)
else:
sunday = datetime.now()
startdate = sunday - timedelta(371)
# Process each day in a column (week) and generate commits to produced the appropriate pixel colors.
for pixelcolumn in pixelcolumns:
response = urllib.request.urlopen('http://whatthecommit.com/index.txt')
commitmessage = response.read().decode('utf-8').rstrip()
for pixel in pixelcolumn:
commitnum = colortocommitcount(pixel)
for commit in range(commitnum):
with open('projectfile', 'a') as f:
f.write('*')
command = \
'git commit -am "' + commitmessage + '" --date ' +\
str(startdate.year) + '-' + str(startdate.month) + '-' + str(startdate.day) + 'T00:00:' + str(commit)
print(command)
os.system(command)
startdate += timedelta(1)
printunicode(artpixelrows)