-
Notifications
You must be signed in to change notification settings - Fork 0
/
repostats.py
66 lines (53 loc) · 2.15 KB
/
repostats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import requests
import json
import matplotlib.pyplot as plt
with open('vars.json') as f:
secret_vars = json.load(f)
USERNAME = secret_vars['GH_USERNAME']
TOKEN = secret_vars['GH_API_KEY']
def get_repo_languages():
try:
url = f"https://api.github.com/users/{USERNAME}/repos"
headers = {"Authorization": f"token {TOKEN}"}
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an exception if the request was unsuccessful
repos = response.json()
languages = {}
for repo in repos:
repo_languages_url = repo["languages_url"]
response = requests.get(repo_languages_url, headers=headers)
response.raise_for_status() # Raise an exception if the request was unsuccessful
repo_languages = response.json()
for language, bytes_count in repo_languages.items():
languages[language] = languages.get(language, 0) + bytes_count
return languages
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None
def save_languages_to_json(languages):
try:
with open("languages.json", "w") as file:
json.dump(languages, file, indent=4)
except IOError as e:
print(f"An error occurred while saving the languages to JSON: {e}")
def create_language_chart():
try:
with open("languages.json", "r") as file:
languages = json.load(file)
labels = list(languages.keys())
values = list(languages.values())
plt.bar(labels, values)
plt.xlabel("Languages")
plt.ylabel("Bytes Count")
plt.title("Language Distribution")
plt.xticks(rotation=45) # Rotate x-axis labels for better readability
plt.tight_layout() # Adjust layout to prevent overlap
plt.savefig("langchart.png")
plt.show()
except IOError as e:
print(f"An error occurred while creating the language chart: {e}")
if __name__ == "__main__":
languages = get_repo_languages()
if languages is not None:
save_languages_to_json(languages)
create_language_chart()