You've already forked wakapi-readme-stats
feat: calculate loc using commit data
Signed-off-by: Aadit Kamat <aadit.k12@gmail.com>
This commit is contained in:
34
loc.py
34
loc.py
@@ -29,23 +29,25 @@ class LinesOfCode:
|
|||||||
for repo in result['data']['user']['repositories']['edges']:
|
for repo in result['data']['user']['repositories']['edges']:
|
||||||
self.getCommitStat(repo['node'], yearly_data)
|
self.getCommitStat(repo['node'], yearly_data)
|
||||||
time.sleep(0.7)
|
time.sleep(0.7)
|
||||||
|
return yearly_data
|
||||||
|
|
||||||
|
def plotLoc(self, yearly_data):
|
||||||
graph = BarGraph(yearly_data)
|
graph = BarGraph(yearly_data)
|
||||||
graph_file = graph.build_graph()
|
graph.build_graph()
|
||||||
self.pushChart()
|
self.pushChart()
|
||||||
|
|
||||||
def run_query_v3(self, nameWithOwner):
|
def run_query_v3(self, endPoint):
|
||||||
endPoint = 'https://api.github.com/repos/' + nameWithOwner + '/stats/code_frequency'
|
|
||||||
# print(endPoint)
|
# print(endPoint)
|
||||||
request = requests.get(endPoint, headers=self.headers)
|
request = requests.get(endPoint, headers=self.headers)
|
||||||
if request.status_code == 401:
|
if request.status_code == 401:
|
||||||
raise Exception("Invalid token {}. {}".format(request.status_code, nameWithOwner))
|
raise Exception("Invalid token {}.".format(request.status_code))
|
||||||
elif request.status_code == 204:
|
elif request.status_code == 204:
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
return request.json()
|
return request.json()
|
||||||
|
|
||||||
def getQuarter(self, timeStamp):
|
def getQuarter(self, timeStamp):
|
||||||
month = datetime.datetime.fromtimestamp(timeStamp).month
|
month = datetime.datetime.fromisoformat(timeStamp).month
|
||||||
if month >= 1 and month <= 3:
|
if month >= 1 and month <= 3:
|
||||||
return 1
|
return 1
|
||||||
elif month >= 4 and month <= 6:
|
elif month >= 4 and month <= 6:
|
||||||
@@ -56,13 +58,25 @@ class LinesOfCode:
|
|||||||
return 4
|
return 4
|
||||||
|
|
||||||
def getCommitStat(self, repoDetails, yearly_data):
|
def getCommitStat(self, repoDetails, yearly_data):
|
||||||
result = self.run_query_v3(repoDetails['nameWithOwner'])
|
allCommitsEndPoint = 'https://api.github.com/repos/' + repoDetails['nameWithOwner'] + '/commits'
|
||||||
|
allCommitsResult = self.run_query_v3(allCommitsEndPoint)
|
||||||
|
# This ignores the error message you get when you try to list commits for an empty repository
|
||||||
|
if not type(allCommitsResult) == list:
|
||||||
|
return
|
||||||
this_year = datetime.datetime.utcnow().year
|
this_year = datetime.datetime.utcnow().year
|
||||||
|
|
||||||
for i in range(len(result)):
|
for i in range(len(allCommitsResult)):
|
||||||
curr_year = datetime.datetime.fromtimestamp(result[i][0]).year
|
author = allCommitsResult[i]["commit"]["author"]
|
||||||
|
if author["name"] != self.username:
|
||||||
|
continue
|
||||||
|
date = re.search(r'\d+-\d+-\d+', author["date"]).group(0)
|
||||||
|
curr_year = datetime.datetime.fromisoformat(date).year
|
||||||
# if curr_year != this_year:
|
# if curr_year != this_year:
|
||||||
quarter = self.getQuarter(result[i][0])
|
|
||||||
|
individualCommitEndPoint = allCommitsResult[i]["url"]
|
||||||
|
individualCommitResult = self.run_query_v3(individualCommitEndPoint)
|
||||||
|
|
||||||
|
quarter = self.getQuarter(date)
|
||||||
if repoDetails['primaryLanguage'] is not None:
|
if repoDetails['primaryLanguage'] is not None:
|
||||||
|
|
||||||
if curr_year not in yearly_data:
|
if curr_year not in yearly_data:
|
||||||
@@ -71,7 +85,7 @@ class LinesOfCode:
|
|||||||
yearly_data[curr_year][quarter] = {}
|
yearly_data[curr_year][quarter] = {}
|
||||||
if repoDetails['primaryLanguage']['name'] not in yearly_data[curr_year][quarter]:
|
if repoDetails['primaryLanguage']['name'] not in yearly_data[curr_year][quarter]:
|
||||||
yearly_data[curr_year][quarter][repoDetails['primaryLanguage']['name']] = 0
|
yearly_data[curr_year][quarter][repoDetails['primaryLanguage']['name']] = 0
|
||||||
yearly_data[curr_year][quarter][repoDetails['primaryLanguage']['name']] += (result[i][1])
|
yearly_data[curr_year][quarter][repoDetails['primaryLanguage']['name']] += individualCommitResult["stats"]["additions"]
|
||||||
|
|
||||||
# to find total
|
# to find total
|
||||||
|
|
||||||
|
|||||||
19
main.py
19
main.py
@@ -383,18 +383,10 @@ def generate_language_per_repo(result):
|
|||||||
|
|
||||||
|
|
||||||
def get_line_of_code():
|
def get_line_of_code():
|
||||||
result = run_query(createContributedRepoQuery.substitute(username=username))
|
repositoryList = run_query(repositoryListQuery.substitute(username=username, id=id))
|
||||||
nodes = result["data"]["user"]["repositoriesContributedTo"]["nodes"]
|
loc = LinesOfCode(id, username, ghtoken, repositoryList)
|
||||||
repos = [d for d in nodes if d['isFork'] is False]
|
yearly_data = loc.calculateLoc()
|
||||||
total_loc = 0
|
total_loc = sum([yearly_data[year][quarter][lang] for year in yearly_data for quarter in yearly_data[year] for lang in yearly_data[year][quarter]])
|
||||||
for repository in repos:
|
|
||||||
try:
|
|
||||||
time.sleep(0.7)
|
|
||||||
datas = run_v3_api(get_loc_url.substitute(owner=repository["owner"]["login"], repo=repository["name"]))
|
|
||||||
for data in datas:
|
|
||||||
total_loc = total_loc + data[1] - data[2]
|
|
||||||
except Exception as execp:
|
|
||||||
print(execp)
|
|
||||||
return humanize.intword(int(total_loc))
|
return humanize.intword(int(total_loc))
|
||||||
|
|
||||||
|
|
||||||
@@ -459,7 +451,8 @@ def get_stats(github):
|
|||||||
|
|
||||||
if showLocChart.lower() in truthy:
|
if showLocChart.lower() in truthy:
|
||||||
loc = LinesOfCode(id, username, ghtoken, repositoryList)
|
loc = LinesOfCode(id, username, ghtoken, repositoryList)
|
||||||
loc.calculateLoc()
|
yearly_data = loc.calculateLoc()
|
||||||
|
loc.plotLoc(yearly_data)
|
||||||
stats += '**' + translate['Timeline'] + '**\n\n'
|
stats += '**' + translate['Timeline'] + '**\n\n'
|
||||||
stats = stats + ' \n\n'
|
stats = stats + ' \n\n'
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user