Created
March 23, 2024 22:03
-
-
Save lanbugs/d013f608ddda79b56aca62de2baf61de to your computer and use it in GitHub Desktop.
Analyze TOP 5 programming languages of a given userlist @github - Single result pie charts + total summary pie chart
This file contains 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 matplotlib.pyplot as plt | |
import requests | |
import os | |
# Analyze TOP 5 programming languages of a given userlist @GITHUB | |
# Single result pie charts + total summary | |
# Written by Maximilian Thoma 2024 | |
# Required libs: requests, matplotlib | |
# Usernames to analyze | |
USERNAMES = ['CiscoDevNet', 'meraki', 'extremenetworks', 'PaloAltoNetworks', 'aruba', 'CheckPointSW', 'fortinetsolutions', 'aristanetworks', 'Juniper', 'F5Networks'] | |
# Your user + PAT token to make more API calls against github | |
MYUSERNAME='your_github_username' | |
TOKEN = 'xxxxxxxxxxxx-your-PAT-token-from-GITHUB' | |
RESULT_DIR = './results' | |
BASE_URL = 'https://api.github.com' | |
os.makedirs(RESULT_DIR, exist_ok=True) | |
def get_repos(username): | |
url = f"{BASE_URL}/users/{username}/repos" | |
response = requests.get(url, auth=(MYUSERNAME, TOKEN)) | |
return response.json() | |
def get_repo_languages(username, repo_name): | |
url = f"{BASE_URL}/repos/{username}/{repo_name}/languages" | |
response = requests.get(url, auth=(MYUSERNAME, TOKEN)) | |
return response.json() | |
def plot_languages(languages, title, filename): | |
labels = list(languages.keys()) | |
sizes = list(languages.values()) | |
total = sum(sizes) | |
percentages = [x / total * 100 for x in sizes] | |
plt.figure(figsize=(10, 7)) | |
plt.pie(percentages, labels=labels, autopct='%1.1f%%', startangle=140) | |
plt.title(title) | |
plt.savefig(filename) | |
plt.close() | |
def main(): | |
total_languages = {} | |
for username in USERNAMES: | |
user_languages = {} | |
repos = get_repos(username) | |
for repo in repos: | |
repo_name = repo['name'] | |
languages = get_repo_languages(username, repo_name) | |
for lang, bytes in languages.items(): | |
user_languages[lang] = user_languages.get(lang, 0) + bytes | |
total_languages[lang] = total_languages.get(lang, 0) + bytes | |
sorted_languages = dict(sorted(user_languages.items(), key=lambda item: item[1], reverse=True)[:5]) | |
plot_languages(sorted_languages, f"Top 5 program languages {username}", | |
f"{RESULT_DIR}/{username}_top_languages.png") | |
sorted_total_languages = dict(sorted(total_languages.items(), key=lambda item: item[1], reverse=True)[:5]) | |
plot_languages(sorted_total_languages, "Top 5 program languages over all users", | |
f"{RESULT_DIR}/total_top_languages.png") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment