-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme_level.py
130 lines (94 loc) · 4.21 KB
/
readme_level.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""Module that contains all the logic about the levelsystem."""
from os import getenv
from datetime import datetime
from logging import exception, info
from requests import post
from graphql_query import QUERY
from readme_data import ReadmeLevelData
class ReadmeLevel:
"""Class that contains all the logic about the levelsystem."""
level_data: ReadmeLevelData = ReadmeLevelData(
contribution_ep=20, project_ep=5, discussion_ep=10, star_ep=40, follower_ep=25)
def __init__(self) -> None:
self.max_level: int = 99
self.ep_to_next_level: int = 100
self.current_ep: int = 0
self.current_level: int = 1
def fetch_user_data(self) -> dict[str, int] | None:
"""Fetches the user data from github api"""
if not getenv("INPUT_GITHUB_TOKEN"):
exception("an error with the github token occurred")
auth_header = {"Authorization": "Bearer " +
getenv("INPUT_GITHUB_TOKEN")}
response = post("https://api.github.com/graphql",
json={"query": QUERY}, headers=auth_header, timeout=2)
if response.status_code == 200:
info("request to github api was successfull")
response_data = response.json()
current_year = datetime.now().year
total_contribution = []
while current_year >= 2015:
contribution_count = (response_data["data"]["user"]
["_" + str(current_year)]["contributionCalendar"]["totalContributions"])
total_contribution.append(contribution_count)
current_year -= 1
user_data = {}
user_data["totalContributions"] = sum(total_contribution)
user_data["totalFollowers"] = (response_data["data"]["user"]
["followers"]["totalCount"])
user_data["totalRepositories"] = (response_data["data"]["user"]
["repositories"]["totalCount"])
return user_data
exception("request to github api failed")
return None
def calc_current_ep(self) -> int:
"""Calculates the current user experience points"""
# get user stats
user_stats: dict[str, int] = self.fetch_user_data()
# calc the current experience points
self.current_ep = (
user_stats["totalContributions"] * self.level_data.contribution_ep +
user_stats["totalFollowers"] * self.level_data.follower_ep +
user_stats["totalRepositories"] * self.level_data.project_ep)
return self.current_ep
# should be part of data class
@property
def get_contribution_ep(self) -> int:
"""gets the contribution ep"""
return self.level_data.contribution_ep
# should be part of data class
@property
def get_follower_ep(self) -> int:
"""gets the follower ep"""
return self.level_data.follower_ep
# should be part of data class
@property
def get_project_ep(self) -> int:
"""gets the project ep"""
return self.level_data.project_ep
@property
def get_current_level(self) -> dict[str, int]:
"""Calculates user level."""
# get current user experience points
current_ep: int = self.calc_current_ep()
while current_ep >= self.ep_to_next_level:
if self.current_level > self.max_level:
self.current_level = self.max_level
break
# increase user level
self.current_level += 1
current_ep -= self.ep_to_next_level
self.ep_to_next_level += 100
percentage_level = self.percentage_ep_level(
current_ep, self.ep_to_next_level)
# sync current ep
self.current_ep = current_ep
return {
"current_level": self.current_level,
"current_ep": self.current_ep,
"ep_to_next_level": self.ep_to_next_level,
"percentage_level": percentage_level
}
def percentage_ep_level(self, current_ep: int, ep_to_next_level: int) -> float:
"""Helper function that calcs the percentage value to the next level"""
return current_ep / ep_to_next_level * 100