-
Notifications
You must be signed in to change notification settings - Fork 1
/
githubrepos.py
executable file
·86 lines (61 loc) · 1.98 KB
/
githubrepos.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
#!/usr/bin/env python3
"""A simple script retrieve & show all GitHub repos of a username
Usage:
$ python3 githubrepos.py username
For example, with username "duyhdb":
$ python3 githubrepos.py duyhdb
"""
import argparse
import requests
from typing import Generator, List
Resp = requests.models.Response
def get_pages(username: str) -> Generator[List[dict], None, None]:
"""Yield all pages containing repos from GitHub API
:param username: GitHub username
:yield: GitHub repos page
"""
client = requests.Session()
page: Resp = client.get(f"https://api.github.com/users/{username}/repos")
yield page.json()
# Paging when GitHub user has more than 30 repos
# https://docs.github.com/en/rest/reference/repos
if page.links.get('next'):
while 'next' in page.headers.get('link'):
page = client.get(page.links['next']['url'])
yield page.json()
client.close()
def extract_repos_name(username: str) -> List[str]:
"""Return the name of each repository from each page
:param username: GitHub username
:rtype list:
"""
repos_name = []
for page in get_pages(username):
for repo in page:
repos_name.append(repo['name'])
return repos_name
def solve(input_data: str) -> List[str]:
"""Function `solve` for `test` purpose
Return the same value in both `solve` and `extract_repos_name`
:param input_data: GitHub username
:rtype list:
"""
result = extract_repos_name(input_data)
return result
def main() -> None:
# Parse positional args
parser = argparse.ArgumentParser(
description="Find GitHub repos by username"
)
parser.add_argument(
'username',
type=str,
help="username of GitHub account you want to find.",
)
args = parser.parse_args()
user = args.username
# Show repos name
for index, name in enumerate(solve(user), start=1):
print(index, name)
if __name__ == "__main__":
main()