You've already forked wakapi-readme-stats
download manager improved to handle and cache all kinds of requests
This commit is contained in:
72
loc.py
72
loc.py
@@ -1,8 +1,9 @@
|
||||
import re
|
||||
import os
|
||||
import base64
|
||||
import requests
|
||||
from github import Github, InputGitAuthor
|
||||
from asyncio import sleep
|
||||
|
||||
from github import Github, InputGitAuthor, AuthenticatedUser
|
||||
import datetime
|
||||
from string import Template
|
||||
import matplotlib.pyplot as plt
|
||||
@@ -10,44 +11,34 @@ from io import StringIO, BytesIO
|
||||
from dotenv import load_dotenv
|
||||
import time
|
||||
|
||||
from download_manager import DownloadManager
|
||||
from make_bar_graph import BarGraph
|
||||
|
||||
|
||||
class LinesOfCode:
|
||||
|
||||
def __init__(self, id, username, ghtoken, repositoryData, ignored_repos):
|
||||
self.id = id
|
||||
self.username = username
|
||||
|
||||
def __init__(self, user: AuthenticatedUser, ghtoken, repositoryData, ignored_repos):
|
||||
self.g = Github(ghtoken)
|
||||
self.headers = {"Authorization": "Bearer " + ghtoken}
|
||||
self.user = user
|
||||
self.repositoryData = repositoryData
|
||||
self.ignored_repos = ignored_repos
|
||||
|
||||
def calculateLoc(self):
|
||||
async def calculateLoc(self):
|
||||
result = self.repositoryData
|
||||
yearly_data = {}
|
||||
for repo in result['data']['user']['repositories']['edges']:
|
||||
total = len(result['data']['user']['repositories']['edges'])
|
||||
for ind, repo in enumerate(result['data']['user']['repositories']['edges']):
|
||||
if repo['node']['name'] not in self.ignored_repos:
|
||||
self.getCommitStat(repo['node'], yearly_data)
|
||||
time.sleep(0.7)
|
||||
print(f"{ind}/{total}", "Retrieving repo:", repo['node']["owner"]["login"], repo['node']['name'])
|
||||
await self.getCommitStat(repo['node'], yearly_data)
|
||||
await sleep(0.7)
|
||||
return yearly_data
|
||||
|
||||
def plotLoc(self, yearly_data):
|
||||
async def plotLoc(self, yearly_data):
|
||||
graph = BarGraph(yearly_data)
|
||||
graph.build_graph()
|
||||
await graph.build_graph()
|
||||
self.pushChart()
|
||||
|
||||
def run_query_v3(self, endPoint):
|
||||
# print(endPoint)
|
||||
request = requests.get(endPoint, headers=self.headers)
|
||||
if request.status_code == 401:
|
||||
raise Exception("Invalid token {}.".format(request.status_code))
|
||||
elif request.status_code == 204:
|
||||
return []
|
||||
else:
|
||||
return request.json()
|
||||
|
||||
def getQuarter(self, timeStamp):
|
||||
month = datetime.datetime.fromisoformat(timeStamp).month
|
||||
if month >= 1 and month <= 3:
|
||||
@@ -59,45 +50,30 @@ class LinesOfCode:
|
||||
elif month >= 10 and month <= 12:
|
||||
return 4
|
||||
|
||||
def getCommitStat(self, repoDetails, yearly_data):
|
||||
commitsURL = 'https://api.github.com/repos/' + repoDetails['nameWithOwner'] + '/commits'
|
||||
filteredCommitsEndPoint = commitsURL + '?author=' + self.username
|
||||
filteredCommitsResult = self.run_query_v3(filteredCommitsEndPoint)
|
||||
# This ignores the error message you get when you try to list commits for an empty repository
|
||||
if not type(filteredCommitsResult) == list:
|
||||
async def getCommitStat(self, repoDetails, yearly_data):
|
||||
commit_data = await DownloadManager.get_remote_graphql("repository_commit_list", owner=repoDetails["owner"]["login"], name=repoDetails['name'], id=self.user.node_id)
|
||||
|
||||
if commit_data["data"]["repository"] is None:
|
||||
print("\tSkipping:", repoDetails['name'])
|
||||
return
|
||||
this_year = datetime.datetime.utcnow().year
|
||||
|
||||
for i in range(len(filteredCommitsResult)):
|
||||
iso_date = filteredCommitsResult[i]["commit"]["author"]["date"]
|
||||
date = re.search(r'\d+-\d+-\d+', iso_date).group(0)
|
||||
for commit in [commit["node"] for branch in commit_data["data"]["repository"]["refs"]["edges"] for commit in branch["node"]["target"]["history"]["edges"]]:
|
||||
date = re.search(r'\d+-\d+-\d+', commit["committedDate"]).group(0)
|
||||
curr_year = datetime.datetime.fromisoformat(date).year
|
||||
# if curr_year != this_year:
|
||||
|
||||
individualCommitEndPoint = commitsURL + '/' + filteredCommitsResult[i]["sha"]
|
||||
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:
|
||||
yearly_data[curr_year] = {}
|
||||
if quarter not in yearly_data[curr_year]:
|
||||
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']] += (individualCommitResult["stats"]["additions"] - individualCommitResult["stats"]['deletions'])
|
||||
yearly_data[curr_year][quarter][repoDetails['primaryLanguage']['name']] += (commit["additions"] - commit["deletions"])
|
||||
|
||||
# to find total
|
||||
|
||||
# if 'total' not in yearly_data[curr_year]:
|
||||
# yearly_data[curr_year]['total']={}
|
||||
# if repoDetails['primaryLanguage']['name'] not in yearly_data[curr_year]['total']:
|
||||
# yearly_data[curr_year]['total'][repoDetails['primaryLanguage']['name']]=0
|
||||
# yearly_data[curr_year]['total'][repoDetails['primaryLanguage']['name']]+=(result[i][1]+result[i][2])
|
||||
|
||||
def pushChart(self):
|
||||
repo = self.g.get_repo(f"{self.username}/{self.username}")
|
||||
repo = self.g.get_repo(f"{self.user.login}/{self.user.login}")
|
||||
committer = InputGitAuthor('readme-bot', '41898282+github-actions[bot]@users.noreply.github.com')
|
||||
with open('bar_graph.png', 'rb') as input_file:
|
||||
data = input_file.read()
|
||||
|
||||
Reference in New Issue
Block a user