|
18 | 18 | GIT_EXTRACT_CMD = "git log --pretty='{}' --all".format(LOG_FORMAT)
|
19 | 19 | GIT_CLONE_CMD = "git clone {}"
|
20 | 20 |
|
21 |
| -GITHUB_USER_REPOS = 'https://api.github.com/users/{}/repos' |
22 |
| -GITHUB_ORGS_REPOS = 'https://api.github.com/orgs/{}/repos' |
| 21 | +GITHUB_USER_STATS = 'https://api.github.com/users/{}' |
| 22 | +GITHUB_USER_REPOS = 'https://api.github.com/users/{}/repos?per_page=100&page={}' |
| 23 | +GITHUB_PER_PAGE_LIMIT = 100 |
23 | 24 |
|
24 | 25 | SYSTEM_EMAILS = [
|
25 | 26 |
|
26 | 27 | ]
|
27 | 28 |
|
28 |
| -def get_github_repos(nickname, only_forks=True): |
| 29 | + |
| 30 | +def get_public_repos_count(nickname): |
| 31 | + url = GITHUB_USER_STATS |
| 32 | + req_url = url.format(nickname) |
| 33 | + req = urllib.request.Request(req_url) |
| 34 | + try: |
| 35 | + response = urllib.request.urlopen(req) |
| 36 | + except Exception as e: |
| 37 | + logging.debug(e) |
| 38 | + else: |
| 39 | + stats = json.loads((response.read().decode('utf8'))) |
| 40 | + repos_count = stats["public_repos"] |
| 41 | + if repos_count: |
| 42 | + return repos_count |
| 43 | + |
| 44 | + |
| 45 | +def get_github_repos(nickname, only_forks=True, repos_count=GITHUB_PER_PAGE_LIMIT): |
29 | 46 | repos_links = set()
|
30 |
| - for url in [GITHUB_ORGS_REPOS, GITHUB_USER_REPOS]: |
31 |
| - req_url = url.format(nickname) |
| 47 | + if not repos_count: |
| 48 | + return repos_links |
| 49 | + url = GITHUB_USER_REPOS |
| 50 | + last_page = int(repos_count / GITHUB_PER_PAGE_LIMIT) + (repos_count % GITHUB_PER_PAGE_LIMIT > 0) |
| 51 | + for page_num in range(1, last_page + 1): |
| 52 | + req_url = url.format(nickname, page_num) |
32 | 53 | req = urllib.request.Request(req_url)
|
33 | 54 | try:
|
34 | 55 | response = urllib.request.urlopen(req)
|
@@ -312,7 +333,10 @@ def main():
|
312 | 333 | repos += dirs
|
313 | 334 |
|
314 | 335 | if args.nickname:
|
315 |
| - repos += get_github_repos(args.nickname) |
| 336 | + repos_count = get_public_repos_count(args.nickname) |
| 337 | + if repos_count: |
| 338 | + print('found', repos_count, 'repos') |
| 339 | + repos += get_github_repos(args.nickname, repos_count=repos_count) |
316 | 340 |
|
317 | 341 | for repo in repos:
|
318 | 342 | analyst.append(source=repo)
|
|
0 commit comments