-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
111 lines (84 loc) · 3.2 KB
/
main.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
import cv2
import glob
import image2image as im
import imageio
import os
import requests
import urllib.request
# install dependencies with pip3 (pip3 install -r requirements.txt)
url = 'https://api.github.com/repos/milaabl/readme-mosaic/stargazers'
def get_stargazers_from_api(api_url, start_page):
# Without an API key in the params, you are limited to 50 API calls per hour
params = {'format': 'json', 'page': start_page, 'per_page': 100}
response = requests.get(url=api_url, params=params)
return response.json()
def get_all_users():
page = 1
avatars = []
has_results = get_stargazers_from_api(url, page)
while has_results:
for result in has_results:
avatars.append({
'avatar' : result['avatar_url'] + '&s=15',
'username' : result['login']
})
page += 1
has_results = get_stargazers_from_api(url, page)
return avatars
def download_avatars():
counter = 1
for user in get_all_users():
response = urllib.request.urlopen(user['avatar'])
image = response.read()
with open('./tmp/avatars/' + str(user['username']) + '.png', "wb") as file:
file.write(image)
filename = 'tmp/steps/step' + format_counter_for_sort(counter) + '.jpg'
filename_rsz = 'tmp/steps_resize/step' + format_counter_for_sort(counter) + '.jpg'
# create temporary mosaic for GIF
im.main(im.get_args(filename))
# resize picture
image = cv2.imread(filename, cv2.IMREAD_COLOR)
image = cv2.resize(image, (702, 318), 0, 0, cv2.IMREAD_COLOR)
cv2.imwrite(filename_rsz, image)
counter += 1
def format_counter_for_sort(number):
if number < 10:
return '00' + str(number)
elif number < 100:
return '0' + str(number)
else:
return str(number)
def generate_gif():
# fetch all steps
frames = []
images = glob.glob('{}/*.jpg'.format('tmp/steps_resize'))
images.sort()
for image in images:
frames.append(imageio.imread(image))
# generate GIF
imageio.mimsave('./data/output.gif', frames, format='GIF', duration=0.1)
def clean_data():
for image in glob.glob('{}/*.jpg'.format('tmp/steps')) + glob.glob('{}/*.jpg'.format('tmp/steps_resize')) + glob.glob('{}/*.png'.format('tmp/avatars')):
os.remove(image)
def write_statistics(statistics):
output = '# Statistics 📊\n\n'
# write header
output = output + '|Rank|Username|Times used|\n:--------:|--------|:--------:|\n'
for index, statistic in enumerate(statistics, start=1):
output = output + '|#' + str(index) + '|[@' + statistic[0] + '](https://github.com/' + statistic[0] + ')|' + str(statistic[1]) + '|\n'
# write on README.md
file = open('STATISTICS.md', 'w+', encoding='utf-8')
file.write(output)
file.close()
if __name__ == '__main__':
print('Clean data')
clean_data()
print('Starting script')
download_avatars()
print('Avatars have been downloaded')
statistics = im.main(im.get_args())
print('Output picture has been created')
write_statistics(statistics)
print('Statistics have been created')
generate_gif()
print('Output GIF has been created')