Created
January 5, 2020 09:55
-
-
Save s-m-e/a4c81345d1ed48bf07994862f2ffd822 to your computer and use it in GitHub Desktop.
Download basic statistics for all repositories in list of GitHub organizations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Python 3 | |
import datetime | |
import json | |
import os | |
from github import Github # pip install PyGithub | |
ORGANIZATIONS = ['my_organization', 'some_other_organization'] | |
TARGET = '/path/to/data' | |
TOKEN = 'githubtoken' | |
def _get_clones_traffic(repo): | |
data = repo.get_clones_traffic() | |
data['clones'] = [clone.raw_data for clone in data['clones']] | |
return data | |
def _get_views_traffic(repo): | |
data = repo.get_views_traffic() | |
data['views'] = [view.raw_data for view in data['views']] | |
return data | |
def _get_top_paths(repo): | |
return [path.raw_data for path in repo.get_top_paths()] | |
def _get_top_referrers(repo): | |
return [ref.raw_data for ref in repo.get_top_referrers()] | |
def _get_repository_stats(repo): | |
return { | |
'clones_traffic': _get_clones_traffic(repo), | |
'views_traffic': _get_views_traffic(repo), | |
'top_paths': _get_top_paths(repo), | |
'top_referrers': _get_top_referrers(repo), | |
'stargazers_count': repo.stargazers_count, | |
'watchers_count': repo.watchers_count, | |
'forks_count': repo.forks_count, | |
'network_count': repo.network_count, | |
'subscribers_count': repo.subscribers_count, | |
} | |
def _get_organization_stats(handle, orga_name): | |
return {repo.name: _get_repository_stats(repo) for repo in handle.get_organization(orga_name).get_repos()} | |
def main(): | |
handle = Github(TOKEN) | |
data = {orga: _get_organization_stats(handle, orga) for orga in ORGANIZATIONS} | |
with open( | |
os.path.join(TARGET, '%s.json' % datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')), | |
'w', encoding = 'utf-8' | |
) as f: | |
f.write(json.dumps(data, sort_keys = True, indent = '\t')) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment