Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miku/8ba99da3fc0f5d824e95f60872fe9bf4 to your computer and use it in GitHub Desktop.
Save miku/8ba99da3fc0f5d824e95f60872fe9bf4 to your computer and use it in GitHub Desktop.
Download basic statistics for all repositories in list of GitHub organizations
#!/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