-
Notifications
You must be signed in to change notification settings - Fork 26
/
export.py
118 lines (90 loc) · 8.06 KB
/
export.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
# Max Base
# 2021-03-21
# https://github.com/BaseMax/GitHub-Repository-Export-List
import sys
import time
import json
import math
import requests
def check_user(username):
url = "https://api.github.com/users/" + username
# Note: if you put a / at the end of url, this will not works!
response = requests.get(url)
json_data = json.loads(response.text)
return json_data
def get_repos(username, page):
url = "https://api.github.com/users/" + username + "/repos?per_page=100&page=" + str(page)
response = requests.get(url)
json_data = json.loads(response.text)
items = []
for item in json_data:
# {'id': 329726066, 'node_id': 'MDEwOlJlcG9zaXRvcnkzMjk3MjYwNjY=', 'name': 'GameNetSystemPHP', 'full_name': 'BaseMax/GameNetSystemPHP', 'private': False, 'owner': {'login': 'BaseMax', 'id': 2658040, 'node_id': 'MDQ6VXNlcjI2NTgwNDA=', 'avatar_url': 'https://avatars.githubusercontent.com/u/2658040?v=4', 'gravatar_id': '', 'url': 'https://api.github.com/users/BaseMax', 'html_url': 'https://github.com/BaseMax', 'followers_url': 'https://api.github.com/users/BaseMax/followers', 'following_url': 'https://api.github.com/users/BaseMax/following{/other_user}', 'gists_url': 'https://api.github.com/users/BaseMax/gists{/gist_id}', 'starred_url': 'https://api.github.com/users/BaseMax/starred{/owner}{/repo}', 'subscriptions_url': 'https://api.github.com/users/BaseMax/subscriptions', 'organizations_url': 'https://api.github.com/users/BaseMax/orgs', 'repos_url': 'https://api.github.com/users/BaseMax/repos', 'events_url': 'https://api.github.com/users/BaseMax/events{/privacy}', 'received_events_url': 'https://api.github.com/users/BaseMax/received_events', 'type': 'User', 'site_admin': False}, 'html_url': 'https://github.com/BaseMax/GameNetSystemPHP', 'description': 'A complete system for game nets that can manage the category of console devices and also have control over rent and receiving money. (PHP + Ajax javascript JSON)', 'fork': False, 'url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP', 'forks_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/forks', 'keys_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/keys{/key_id}', 'collaborators_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/collaborators{/collaborator}', 'teams_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/teams', 'hooks_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/hooks', 'issue_events_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/issues/events{/number}', 'events_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/events', 'assignees_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/assignees{/user}', 'branches_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/branches{/branch}', 'tags_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/tags', 'blobs_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/git/blobs{/sha}', 'git_tags_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/git/tags{/sha}', 'git_refs_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/git/refs{/sha}', 'trees_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/git/trees{/sha}', 'statuses_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/statuses/{sha}', 'languages_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/languages', 'stargazers_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/stargazers', 'contributors_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/contributors', 'subscribers_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/subscribers', 'subscription_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/subscription', 'commits_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/commits{/sha}', 'git_commits_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/git/commits{/sha}', 'comments_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/comments{/number}', 'issue_comment_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/issues/comments{/number}', 'contents_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/contents/{+path}', 'compare_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/compare/{base}...{head}', 'merges_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/merges', 'archive_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/{archive_format}{/ref}', 'downloads_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/downloads', 'issues_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/issues{/number}', 'pulls_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/pulls{/number}', 'milestones_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/milestones{/number}', 'notifications_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/notifications{?since,all,participating}', 'labels_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/labels{/name}', 'releases_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/releases{/id}', 'deployments_url': 'https://api.github.com/repos/BaseMax/GameNetSystemPHP/deployments', 'created_at': '2021-01-14T20:25:17Z', 'updated_at': '2021-03-06T11:49:23Z', 'pushed_at': '2021-03-06T11:49:20Z', 'git_url': 'git://github.com/BaseMax/GameNetSystemPHP.git', 'ssh_url': '[email protected]:BaseMax/GameNetSystemPHP.git', 'clone_url': 'https://github.com/BaseMax/GameNetSystemPHP.git', 'svn_url': 'https://github.com/BaseMax/GameNetSystemPHP', 'homepage': '', 'size': 1953, 'stargazers_count': 0, 'watchers_count': 0, 'language': 'PHP', 'has_issues': True, 'has_projects': True, 'has_downloads': True, 'has_wiki': True, 'has_pages': False, 'forks_count': 0, 'mirror_url': None, 'archived': False, 'disabled': False, 'open_issues_count': 0, 'license': {'key': 'gpl-3.0', 'name': 'GNU General Public License v3.0', 'spdx_id': 'GPL-3.0', 'url': 'https://api.github.com/licenses/gpl-3.0', 'node_id': 'MDc6TGljZW5zZTk='}, 'forks': 0, 'open_issues': 0, 'watchers': 0, 'default_branch': 'main'}
link = "https://github.com/" + item["full_name"]
description = item["description"]
language = item["language"]
name = item["name"]
items.append({
"name": name,
"language": language,
"description": description,
"link": link,
})
return items
def get_all_repos(username, public_repos):
pages = math.ceil(public_repos / 100)
items = []
for page in range(pages): # page starts from 0
res = get_repos(username, page+1)
items.extend(res)
time.sleep(3)
return items
def group_by_languages(repos):
languages = {}
for repo in repos:
if repo["language"] == "" or repo["language"] == None:
lang = "other"
else:
lang = str(repo["language"])
if lang not in languages:
languages[lang] = []
# TODO: remove language field from repo
languages[lang].append(repo)
return languages
def generate_html(profile, groups):
if profile["name"] == None:
profile["name"] = profile["login"]
print("<title>" + profile["name"] + " GitHub</title>")
print("<h1>" + profile["name"] + " GitHub</h1>\n")
for language, repos in groups.items():
print("<b>"+ language +"</b>") # TODO: upper-case first char of language name
print("<ul>")
for repo in repos:
print(" <li>")
print(" <a href=\"" + str(repo["link"]) +"\" alt=\"" + str(repo["name"]) + "\">" + str(repo["name"]) + "</a>: ")
print(" <span>" + str(repo["description"]) + "</span>")
print(" </li>")
print("</ul>\n")
if __name__ == "__main__":
# check args
if len(sys.argv) != 2:
username="basemax"
else:
username = sys.argv[1]
profile = check_user(username)
if profile == None or profile == "" or profile == {} or profile == []:
print("Error: User not found!")
exit()
elif "message" in profile and profile["message"] == "Not Found":
print("Error: User not found!")
exit()
try:
public_repos = profile["public_repos"]
except KeyError:
public_repos = 0
if public_repos:
all_repos = get_all_repos(username, public_repos)
groups = group_by_languages(all_repos)
# generate HTML output
generate_html(profile, groups)
else:
print("Error: No public repositories or maybe network problem!")