|
| 1 | +from get_config_value import get_config_value |
| 2 | +from github import Github |
| 3 | +import json |
| 4 | +from ratelimiter import RateLimiter |
| 5 | +from time import gmtime, strftime |
| 6 | + |
| 7 | +def get_user_repo_branches(): |
| 8 | + extract_json = { |
| 9 | + 'data_source_access_time': strftime("%Y%m%dT%H%M%SZ", gmtime()), |
| 10 | + 'data_source_name': 'GitHub API' |
| 11 | + } |
| 12 | + g = Github(get_config_value('github_access_token')) |
| 13 | + user = g.get_user() |
| 14 | + user_json = { |
| 15 | + 'login': user.login, |
| 16 | + 'public_repos': user.public_repos, |
| 17 | + 'repos': {} |
| 18 | + } |
| 19 | + repos = user.get_repos() |
| 20 | + rate_limiter = RateLimiter(max_calls=5000, period=3600) |
| 21 | + |
| 22 | + for i, repo in enumerate(repos): |
| 23 | + with rate_limiter: |
| 24 | + if i >= get_config_value('max_repos_to_download'): |
| 25 | + pass |
| 26 | + else: |
| 27 | + repo_name = repo.name |
| 28 | + repo_json = { |
| 29 | + 'name': repo_name, |
| 30 | + 'private': repo.private, |
| 31 | + 'forks_count': repo.forks_count, |
| 32 | + 'stargazers_count': repo.stargazers_count, |
| 33 | + 'watchers_count': repo.watchers_count, |
| 34 | + 'size': repo.size, |
| 35 | + 'open_issues_count': repo.open_issues_count, |
| 36 | + 'has_issues': repo.has_issues, |
| 37 | + 'has_projects': repo.has_projects, |
| 38 | + 'has_wiki': repo.has_wiki, |
| 39 | + 'has_downloads': repo.has_downloads, |
| 40 | + 'pushed_at': repo.pushed_at, |
| 41 | + 'created_at': repo.created_at, |
| 42 | + 'updated_at': repo.updated_at, |
| 43 | + 'branches': {} |
| 44 | + } |
| 45 | + branches = repo.get_branches() |
| 46 | + |
| 47 | + for branch in branches: |
| 48 | + branch_name = branch.name |
| 49 | + branch_commit = branch.commit |
| 50 | + branch_json = { |
| 51 | + 'name': branch_name, |
| 52 | + 'commit': { |
| 53 | + 'sha': branch_commit.sha, |
| 54 | + 'url': branch_commit.url |
| 55 | + }, |
| 56 | + 'protected': branch.protected, |
| 57 | + } |
| 58 | + repo_json['branches'][branch_name] = branch_json |
| 59 | + |
| 60 | + user_json['repos'][repo_name] = repo_json |
| 61 | + |
| 62 | + extract_json['data'] = user_json |
| 63 | + |
| 64 | + with open('data/user_repo_branches.json', 'w') as output_file: |
| 65 | + json.dump(extract_json, output_file, indent=4, default=str) |
| 66 | + |
| 67 | +def display_rate_limit(): |
| 68 | + g = Github(get_config_value('github_access_token')) |
| 69 | + print('Rate limit stats:\nCore limit: ', g.rate_limiting[0]) |
| 70 | + |
| 71 | +def prompt_user_to_continue(): |
| 72 | + user_input = input('Do you wish to proceed? (y/n): ') |
| 73 | + return user_input.upper() == 'Y' |
| 74 | + |
| 75 | +def main(): |
| 76 | + display_rate_limit() |
| 77 | + |
| 78 | + if prompt_user_to_continue(): |
| 79 | + get_user_repo_branches() |
| 80 | + |
| 81 | +main() |
0 commit comments