Created
December 29, 2020 03:10
-
-
Save satanas/c6688221a757ed97c2eef336c9b0f500 to your computer and use it in GitHub Desktop.
All Time Github Contributions
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
import sys | |
import requests | |
CONTRIBUTIONS_BASE_URL = 'https://api.github.com/repos/{}/{}/stats/contributors' | |
CONTRIBUTORS_BASE_URL = 'https://api.github.com/repos/{}/{}/contributors' | |
ORG_REPOS_BASE_URL = 'https://api.github.com/orgs/{}/repos' | |
AUTH_USER_REPOS = 'https://api.github.com/users/walvarezrodrigu/repos' | |
USER_REPOS = 'https://api.github.com/user/repos' | |
TGREEN = '\033[32m' | |
TYELLOW = '\033[33m' | |
TRED = '\033[31m' | |
TWHITE = '\033[37m' | |
ENDC = '\033[m' | |
ME = 'satanas' | |
if len(sys.argv) <= 1: | |
print('Error: Auth token missing') | |
sys.exit(-1) | |
HEADERS = { | |
'Authorization': f'token {sys.argv[1]}', | |
} | |
orgs = [ | |
'org1', | |
'org2' | |
] | |
repos = {} | |
def get(url, headers=None): | |
print(f'Fetching URL: {url}', end="\r") | |
response = requests.get(url, headers=headers) | |
message = f'Fetching URL: {url} [{response.status_code} {response.reason}]' | |
color = ENDC | |
if response.status_code == 202: | |
color = TYELLOW | |
elif response.status_code == 404: | |
color = TRED | |
print(color + message + ENDC) | |
return response | |
def title_log(text): | |
print(f'{TGREEN}{text}{ENDC}') | |
def result_log(text, value): | |
print(f'{TGREEN}{text}: {TWHITE}{value}{ENDC}') | |
def add_to_repos(org, repo): | |
if org not in repos: | |
repos[org] = [] | |
if repo not in repos[org]: | |
repos[org].append(repo) | |
total_contributions = 0 | |
additions = 0 | |
deletions = 0 | |
commits = 0 | |
title_log(f'* Fetching {ME} repos...') | |
resp = get(AUTH_USER_REPOS, HEADERS) | |
if resp.status_code >= 200 and resp.status_code < 300: | |
for r in resp.json(): | |
org = r['owner']['login'] | |
repo = r['name'] | |
add_to_repos(org, repo) | |
resp = get(USER_REPOS, HEADERS) | |
if resp.status_code >= 200 and resp.status_code < 300: | |
for r in resp.json(): | |
org = r['owner']['login'] | |
repo = r['name'] | |
add_to_repos(org, repo) | |
title_log(f'* Fetching all repos from organizations...') | |
all_repos = {} | |
for o in orgs: | |
response = get(ORG_REPOS_BASE_URL.format(o)) | |
if response.status_code == 200: | |
for r in response.json(): | |
if o not in all_repos: | |
all_repos[o] = [] | |
if r['name'] not in all_repos[o]: | |
all_repos[o].append(r['name']) | |
title_log(f'* Checking contributors from all repos...') | |
for org in all_repos.keys(): | |
for repo in all_repos[org]: | |
response = get(CONTRIBUTORS_BASE_URL.format(org, repo)) | |
if response.status_code == 200: | |
for c in response.json(): | |
if c['login'] == ME: | |
print(f'Found contributions for {org}/{repo}') | |
add_to_repos(org, repo) | |
break | |
title_log(f'* Getting stats for contributions...') | |
for org in repos.keys(): | |
for repo in repos[org]: | |
response = get(CONTRIBUTIONS_BASE_URL.format(org, repo), HEADERS) | |
if response.status_code == 200: | |
for e in response.json(): | |
if e['author']['login'] == ME: | |
total_contributions += int(e['total']) | |
for w in e['weeks']: | |
additions += int(w['a']) | |
deletions += int(w['d']) | |
commits += int(w['c']) | |
break | |
result_log('* All-time contributions', f'{total_contributions} commits') | |
result_log('* Total lines added (+)', additions) | |
result_log('* Total lines deleted (-)', deletions) | |
result_log('* Total lines changed', additions + deletions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment