-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
151 lines (103 loc) · 4.54 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
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
import requests
import math
import json
import re
GITHUB_USERNAME = "Zeyu-Li"
FILES = False
DUMP_GIST = False
REPOS_FILE = "data.json"
GISTS_FILE = "gists.json"
def getReposAPI(file=f"{REPOS_FILE}"):
r = requests.get(f'https://api.github.com/users/{GITHUB_USERNAME}')
try:
count = r.json()['public_repos']
except:
raise Exception("Exceeded API limit")
return
repos = []
# TODO: requests all await?
# tricky because github api limit is small
for i in range(math.ceil(count/2)):
r = requests.get(
f'https://api.github.com/users/{GITHUB_USERNAME}/repos?per_page=100&page={i+1}')
repos += r.json()
# clean up
repos = list(filter(lambda x: not (
x == "message" or x == "documentation_url"), repos))
# dumps in json just in case
with open(file, "w") as fp:
json.dump(repos, fp)
return repos
def getGistsAPI(file=f"{GISTS_FILE}"):
r = requests.get(f'https://api.github.com/users/{GITHUB_USERNAME}/gists')
gists = r.json()
# dumps in json just in case
if DUMP_GIST:
with open(file, "w") as fp:
json.dump(gists, fp)
return gists
def getRepoFile(file=f"{REPOS_FILE}"):
with open(file, "r", encoding="utf-8") as fp:
return list(filter(lambda x: not (x == "message" or x == "documentation_url"), json.loads(fp.read())))
def getGistFile(file=f"{GISTS_FILE}"):
with open(file, "r", encoding="utf-8") as fp:
return json.loads(fp.read())
def getReposText(repos):
returnText = ""
for repo in repos:
# homepage = repo['homepage'].replace('https://','') if repo['homepage'] else ''
# print(returnText)
returnText += ("* [" + ' '.join(name.title() for name in re.split('; |, |\*|_|-', repo['name'])) +
f"]({repo['html_url']}) - {repo['description']}" + (f" [Website @ [{repo['homepage'].replace('https://','') if repo['homepage'] else ''}]({repo['homepage']})]" if repo['homepage'] else '') + '\n')
return returnText
def getGistsText(gists):
returnText = ""
gists.sort(key=lambda a: list(a['files'].keys())[0].lower())
for gist in gists:
returnText += (
f"* [{list(gist['files'].keys())[0]}]({gist['html_url']}) - {gist['description']}\n")
return returnText
def getLanguages(repos):
# sort by language
languages = dict()
for repo in repos:
returnText = ("* [" + ' '.join(name.title() for name in re.split('; |, |\*|_|-', repo['name'])) +
f"]({repo['html_url']}) - {repo['description']}" + (f" [Website @ [{repo['homepage'].replace('https://','') if repo['homepage'] else ''}]({repo['homepage']})]" if repo['homepage'] else '') + '\n')
if repo['language'] in languages or (repo['language'] == None and "Misc" in languages):
languages['Misc' if repo['language'] ==
None else repo['language']] += returnText
else:
languages['Misc' if repo['language'] ==
None else repo['language']] = returnText
new_line = '\n'
return ''.join(f"### ({languages[language].count(new_line)}) {language}{new_line + new_line + languages[language] + new_line + new_line}" for language in sorted(languages))
def main():
FILES = not input("Use API to get repos y/N: ")[0].lower() == 'y'
repos = getRepoFile() if FILES else getReposAPI()
gist_override = False
gists = getGistFile() if FILES and not gist_override else getGistsAPI()
if (not FILES and gists['documentation_url'] == 'https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting'):
# if API limit reaches, use json file
gists = getGistFile()
template = f"""
# GitHub Index
<a name="top"></a>
## About
This is a index to all my **GitHub Repositories**. It acts as a quick-link to all my projects as well as a description to each repo
<a href="#all">Jump to **All Repositories**</a> | <a href="#gist">Jump to **Gists**</a> | <a href="#languages">Jump to **By Languages**</a>
<a name="all_r"></a>
## ({len(repos)}) All GitHub Repos
{getReposText(repos)}
<a name="gist"></a>
## ({len(gists)}) All Gists
{getGistsText(gists)}
<a name="languages"></a>
## By Languages
{getLanguages(repos)}
**<a href="#top">🔝 Back to Top</a>**
"""
with open("README.md", "w", encoding="utf-8") as fp:
fp.writelines(template)
return 0
if __name__ == "__main__":
main()