-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
gitfiti.py
executable file
·509 lines (408 loc) · 12.6 KB
/
gitfiti.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#!/usr/bin/env python
#
# Copyright (c) 2013 Eric Romano (@gelstudios)
# released under The MIT license (MIT) http://opensource.org/licenses/MIT
#
"""
gitfiti
noun : Carefully crafted graffiti in a GitHub commit history calendar
"""
from datetime import datetime, timedelta
import itertools
import json
import math
import os
try:
# Python 3+
from urllib.error import HTTPError, URLError
from urllib.request import urlopen
except ImportError:
# Python 2
from urllib2 import HTTPError, URLError, urlopen
try:
# Python 2
raw_input
except NameError:
# Python 3 (Python 2's `raw_input` was renamed to `input`)
raw_input = input
GITHUB_BASE_URL = 'https://github.com/'
FALLBACK_IMAGE = 'kitty'
TITLE = '''
_ __ _____ __ _
____ _(_) /_/ __(_) /_(_)
/ __ `/ / __/ /_/ / __/ /
/ /_/ / / /_/ __/ / /_/ /
\__, /_/\__/_/ /_/\__/_/
/____/
'''
KITTY = [
[0,0,0,4,0,0,0,0,4,0,0,0],
[0,0,4,2,4,4,4,4,2,4,0,0],
[0,0,4,2,2,2,2,2,2,4,0,0],
[2,2,4,2,4,2,2,4,2,4,2,2],
[0,0,4,2,2,3,3,2,2,4,0,0],
[2,2,4,2,2,2,2,2,2,4,2,2],
[0,0,0,3,4,4,4,4,3,0,0,0],
]
ONEUP = [
[0,4,4,4,4,4,4,4,0],
[4,3,2,2,1,2,2,3,4],
[4,2,2,1,1,1,2,2,4],
[4,3,4,4,4,4,4,3,4],
[4,4,1,4,1,4,1,4,4],
[0,4,1,1,1,1,1,4,0],
[0,0,4,4,4,4,4,0,0],
]
ONEUP2 = [
[0,0,4,4,4,4,4,4,4,0,0],
[0,4,2,2,1,1,1,2,2,4,0],
[4,3,2,2,1,1,1,2,2,3,4],
[4,3,3,4,4,4,4,4,3,3,4],
[0,4,4,1,4,1,4,1,4,4,0],
[0,0,4,1,1,1,1,1,4,0,0],
[0,0,0,4,4,4,4,4,0,0,0],
]
HACKERSCHOOL = [
[4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,2,2,4,0],
[0,0,4,2,4,0,0],
[0,0,0,4,0,0,0],
]
ASCII_TO_NUMBER = {
'_': 0,
'_': 1,
'~': 2,
'=': 3,
'*': 4,
}
def str_to_sprite(content):
# Break out lines and filter any excess
lines = content.split('\n')
def is_empty_line(line):
return len(line) != 0
lines = filter(is_empty_line, lines)
# Break up lines into each character
split_lines = [list(line) for line in lines]
# Replace each character with its numeric equivalent
for line in split_lines:
for index, char in enumerate(line):
line[index] = ASCII_TO_NUMBER.get(char, 0)
# Return the formatted str
return split_lines
ONEUP_STR = str_to_sprite('''
*******
*=~~-~~=*
*~~---~~*
*=*****=*
**-*-*-**
*-----*
*****
''')
IMAGES = {
'kitty': KITTY,
'oneup': ONEUP,
'oneup2': ONEUP2,
'hackerschool': HACKERSCHOOL,
'octocat': OCTOCAT,
'octocat2': OCTOCAT2,
'hello': HELLO,
'heart1': HEART1,
'heart2': HEART2,
'hireme': HIREME,
'oneup_str': ONEUP_STR,
'beer': BEER,
'gliders': GLIDERS,
'heart' : HEART,
'heart_shiny' : HEART_SHINY,
}
SHELLS = {
'bash': 'sh',
'powershell': 'ps1',
}
def load_images(img_names):
"""loads user images from given file(s)"""
if img_names[0] == '':
return {}
for image_name in img_names:
with open(image_name) as img:
loaded_imgs = {}
img_list = ''
img_line = ' '
name = img.readline().replace('\n', '')
name = name[1:]
while True:
img_line = img.readline()
if img_line == '':
break
img_line.replace('\n', '')
if img_line[0] == ':':
loaded_imgs[name] = json.loads(img_list)
name = img_line[1:]
img_list = ''
else:
img_list += img_line
loaded_imgs[name] = json.loads(img_list)
return loaded_imgs
def retrieve_contributions_calendar(username, base_url):
"""retrieves the GitHub commit calendar data for a username"""
base_url = base_url + 'users/' + username
try:
url = base_url + '/contributions'
page = urlopen(url)
except (HTTPError, URLError) as e:
print('There was a problem fetching data from {0}'.format(url))
print(e)
raise SystemExit
return page.read().decode('utf-8')
def parse_contributions_calendar(contributions_calendar):
"""Yield daily counts extracted from the embedded contributions SVG."""
for line in contributions_calendar.splitlines():
# a valid line looks like this:
# <rect width="11" height="11" x="-31" y="0" class="ContributionCalendar-day" data-date="2023-02-26" data-level="3" rx="2" ry="2">23 contributions on Sunday, February 26, 2023</rect>
if 'data-date=' in line:
commit = line.split('>')[1].split()[0] # yuck
if commit.isnumeric():
yield int(commit)
def find_max_daily_commits(contributions_calendar):
"""finds the highest number of commits in one day"""
daily_counts = parse_contributions_calendar(contributions_calendar)
return max(daily_counts, default=0)
def calculate_multiplier(max_commits):
"""calculates a multiplier to scale GitHub colors to commit history"""
m = max_commits / 4.0
if m == 0:
return 1
m = math.ceil(m)
m = int(m)
return m
def get_start_date():
"""returns a datetime object for the first sunday after one year ago today
at 12:00 noon"""
today = datetime.today()
date = datetime(today.year - 1, today.month, today.day, 12)
weekday = datetime.weekday(date)
while weekday < 6:
date = date + timedelta(1)
weekday = datetime.weekday(date)
return date
def generate_next_dates(start_date, offset=0):
"""generator that returns the next date, requires a datetime object as
input. The offset is in weeks"""
start = offset * 7
for i in itertools.count(start):
yield start_date + timedelta(i)
def generate_values_in_date_order(image, multiplier=1):
height = 7
width = len(image[0])
for w in range(width):
for h in range(height):
yield image[h][w] * multiplier
def commit(commitdate, shell):
template_bash = (
'''GIT_AUTHOR_DATE={0} GIT_COMMITTER_DATE={1} '''
'''git commit --allow-empty -m "gitfiti" > /dev/null\n'''
)
template_powershell = (
'''$Env:GIT_AUTHOR_DATE="{0}"\n$Env:GIT_COMMITTER_DATE="{1}"\n'''
'''git commit --allow-empty -m "gitfiti" | Out-Null\n'''
)
template = template_bash if shell == 'bash' else template_powershell
return template.format(commitdate.isoformat(), commitdate.isoformat())
def fake_it(image, start_date, username, repo, git_url, shell, offset=0, multiplier=1):
template_bash = (
'#!/usr/bin/env bash\n'
'REPO={0}\n'
'git init $REPO\n'
'cd $REPO\n'
'touch README.md\n'
'git add README.md\n'
'touch gitfiti\n'
'git add gitfiti\n'
'{1}\n'
'git branch -M main\n'
'git remote add origin {2}:{3}/$REPO.git\n'
'git pull origin main\n'
'git push -u origin main\n'
)
template_powershell = (
'cd $PSScriptRoot\n'
'$REPO="{0}"\n'
'git init $REPO\n'
'cd $REPO\n'
'New-Item README.md -ItemType file | Out-Null\n'
'git add README.md\n'
'New-Item gitfiti -ItemType file | Out-Null\n'
'git add gitfiti\n'
'{1}\n'
'git branch -M main\n'
'git remote add origin {2}:{3}/$REPO.git\n'
'git pull origin main\n'
'git push -u origin main\n'
)
template = template_bash if shell == 'bash' else template_powershell
strings = []
for value, date in zip(generate_values_in_date_order(image, multiplier),
generate_next_dates(start_date, offset)):
for _ in range(value):
strings.append(commit(date, shell))
return template.format(repo, ''.join(strings), git_url, username)
def save(output, filename):
"""Saves the list to a given filename"""
with open(filename, 'w') as f:
f.write(output)
os.chmod(filename, 0o755) # add execute permissions
def request_user_input(prompt='> '):
"""Request input from the user and return what has been entered."""
return raw_input(prompt)
def main():
print(TITLE)
ghe = request_user_input(
'Enter GitHub URL (leave blank to use {}): '.format(GITHUB_BASE_URL))
username = request_user_input('Enter your GitHub username: ')
git_base = ghe if ghe else GITHUB_BASE_URL
contributions_calendar = retrieve_contributions_calendar(username, git_base)
max_daily_commits = find_max_daily_commits(contributions_calendar)
m = calculate_multiplier(max_daily_commits)
repo = request_user_input(
'Enter the name of the repository to use by gitfiti: ')
offset = request_user_input(
'Enter the number of weeks to offset the image (from the left): ')
offset = int(offset) if offset.strip() else 0
print((
'By default gitfiti.py matches the darkest pixel to the highest\n'
'number of commits found in your GitHub commit/activity calendar,\n'
'\n'
'Currently this is: {0} commits\n'
'\n'
'Enter the word "gitfiti" to exceed your max\n'
'(this option generates WAY more commits)\n'
'Any other input will cause the default matching behavior'
).format(max_daily_commits))
match = request_user_input()
match = m if (match == 'gitfiti') else 1
print('Enter file(s) to load images from (blank if not applicable)')
img_names = request_user_input().split(' ')
loaded_images = load_images(img_names)
images = dict(IMAGES, **loaded_images)
print('Enter the image name to gitfiti')
print('Images: ' + ', '.join(images.keys()))
image = request_user_input()
image_name_fallback = FALLBACK_IMAGE
if not image:
image = IMAGES[image_name_fallback]
else:
try:
image = images[image]
except:
image = IMAGES[image_name_fallback]
start_date = get_start_date()
fake_it_multiplier = m * match
if not ghe:
git_url = 'git@github.com'
else:
git_url = request_user_input('Enter Git URL like git@site.github.com: ')
shell = ''
while shell not in SHELLS.keys():
shell = request_user_input(
'Enter the target shell ({}): '.format(' or '.join(SHELLS.keys())))
output = fake_it(image, start_date, username, repo, git_url, shell, offset,
fake_it_multiplier)
output_filename = 'gitfiti.{}'.format(SHELLS[shell])
save(output, output_filename)
print('{} saved.'.format(output_filename))
print('Create a new(!) repo named {0} at {1} and run the script'.format(repo, git_base))
if __name__ == '__main__':
main()