You've already forked wakapi-readme-stats
FIX: Added Error Handling
This commit is contained in:
89
main.py
89
main.py
@@ -1,11 +1,10 @@
|
|||||||
'''
|
'''
|
||||||
WakaTime progress visualizer
|
Readme Development Metrics With waka time progress
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import base64
|
import base64
|
||||||
import pytz
|
|
||||||
import requests
|
import requests
|
||||||
from github import Github
|
from github import Github
|
||||||
import datetime
|
import datetime
|
||||||
@@ -23,8 +22,8 @@ showProjects = os.getenv('INPUT_SHOW_PROJECTS')
|
|||||||
showEditors = os.getenv('INPUT_SHOW_EDITORS')
|
showEditors = os.getenv('INPUT_SHOW_EDITORS')
|
||||||
showOs = os.getenv('INPUT_SHOW_OS')
|
showOs = os.getenv('INPUT_SHOW_OS')
|
||||||
showCommit = os.getenv('INPUT_SHOW_COMMIT')
|
showCommit = os.getenv('INPUT_SHOW_COMMIT')
|
||||||
|
showLanguage = os.getenv('INPUT_SHOW_LANGUAGE')
|
||||||
|
|
||||||
headers = {"Authorization": "Bearer " + ghtoken}
|
|
||||||
# The GraphQL query to get commit data.
|
# The GraphQL query to get commit data.
|
||||||
userInfoQuery = """
|
userInfoQuery = """
|
||||||
{
|
{
|
||||||
@@ -69,7 +68,7 @@ query {
|
|||||||
""")
|
""")
|
||||||
|
|
||||||
|
|
||||||
def run_query(query): # A simple function to use requests.post to make the API call. Note the json= section.
|
def run_query(query):
|
||||||
request = requests.post('https://api.github.com/graphql', json={'query': query}, headers=headers)
|
request = requests.post('https://api.github.com/graphql', json={'query': query}, headers=headers)
|
||||||
if request.status_code == 200:
|
if request.status_code == 200:
|
||||||
return request.json()
|
return request.json()
|
||||||
@@ -77,15 +76,6 @@ def run_query(query): # A simple function to use requests.post to make the API
|
|||||||
raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))
|
raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))
|
||||||
|
|
||||||
|
|
||||||
def this_week():
|
|
||||||
'''Returns current week span'''
|
|
||||||
week_number = datetime.date.today().isocalendar()[1]
|
|
||||||
month = datetime.date.today().strftime('%B')
|
|
||||||
week_start = datetime.datetime.today().day - datetime.datetime.today().weekday()
|
|
||||||
week_end = week_start + 5
|
|
||||||
return f"Week #{week_number} : {month} {week_start} - {week_end}"
|
|
||||||
|
|
||||||
|
|
||||||
def make_graph(percent: float):
|
def make_graph(percent: float):
|
||||||
'''Make progress graph from API graph'''
|
'''Make progress graph from API graph'''
|
||||||
done_block = '█'
|
done_block = '█'
|
||||||
@@ -121,7 +111,6 @@ def generate_commit_list():
|
|||||||
username = result["data"]["viewer"]["login"]
|
username = result["data"]["viewer"]["login"]
|
||||||
id = result["data"]["viewer"]["id"]
|
id = result["data"]["viewer"]["id"]
|
||||||
print("user {} id {}".format(username, id))
|
print("user {} id {}".format(username, id))
|
||||||
print("on new version")
|
|
||||||
|
|
||||||
result = run_query(createContributedRepoQuery.substitute(username=username))
|
result = run_query(createContributedRepoQuery.substitute(username=username))
|
||||||
nodes = result["data"]["user"]["repositoriesContributedTo"]["nodes"]
|
nodes = result["data"]["user"]["repositoriesContributedTo"]["nodes"]
|
||||||
@@ -165,33 +154,45 @@ def generate_commit_list():
|
|||||||
|
|
||||||
def get_stats():
|
def get_stats():
|
||||||
'''Gets API data and returns markdown progress'''
|
'''Gets API data and returns markdown progress'''
|
||||||
data = requests.get(
|
stats = ''
|
||||||
f"https://wakatime.com/api/v1/users/current/stats/last_7_days?api_key={waka_key}").json()
|
try:
|
||||||
|
request = requests.get(
|
||||||
|
f"https://wakatime.com/api/v1/users/current/stats/last_7_days?api_key={waka_key}")
|
||||||
|
|
||||||
stats = '```text\n'
|
if request.status_code == 200:
|
||||||
if showTimeZone.lower() in ['true', '1', 't', 'y', 'yes']:
|
data = request.json()
|
||||||
timezone = data['data']['timezone']
|
stats = stats + '```text\n'
|
||||||
stats = stats + '⌚︎ Timezone: ' + timezone + '\n\n'
|
if showTimeZone.lower() in ['true', '1', 't', 'y', 'yes']:
|
||||||
|
timezone = data['data']['timezone']
|
||||||
|
stats = stats + '⌚︎ Timezone: ' + timezone + '\n\n'
|
||||||
|
|
||||||
lang_list = make_list(data['data']['languages'])
|
if showLanguage.lower() in ['true', '1', 't', 'y', 'yes']:
|
||||||
stats = stats + '💬 Languages: \n' + lang_list + '\n\n'
|
lang_list = make_list(data['data']['languages'])
|
||||||
|
stats = stats + '💬 Languages: \n' + lang_list + '\n\n'
|
||||||
|
|
||||||
if showEditors.lower() in ['true', '1', 't', 'y', 'yes']:
|
if showEditors.lower() in ['true', '1', 't', 'y', 'yes']:
|
||||||
edit_list = make_list(data['data']['editors'])
|
edit_list = make_list(data['data']['editors'])
|
||||||
stats = stats + '🔥 Editors: \n' + edit_list + '\n\n'
|
stats = stats + '🔥 Editors: \n' + edit_list + '\n\n'
|
||||||
|
|
||||||
if showProjects.lower() in ['true', '1', 't', 'y', 'yes']:
|
if showProjects.lower() in ['true', '1', 't', 'y', 'yes']:
|
||||||
project_list = make_list(data['data']['projects'])
|
project_list = make_list(data['data']['projects'])
|
||||||
stats = stats + '🐱💻 Projects: \n' + project_list + '\n\n'
|
stats = stats + '🐱💻 Projects: \n' + project_list + '\n\n'
|
||||||
|
|
||||||
if showOs.lower() in ['true', '1', 't', 'y', 'yes']:
|
if showOs.lower() in ['true', '1', 't', 'y', 'yes']:
|
||||||
os_list = make_list(data['data']['operating_systems'])
|
os_list = make_list(data['data']['operating_systems'])
|
||||||
stats = stats + '💻 Operating Systems: \n' + os_list + '\n\n'
|
stats = stats + '💻 Operating Systems: \n' + os_list + '\n\n'
|
||||||
|
|
||||||
stats = stats + '```\n\n'
|
stats = stats + '```\n\n'
|
||||||
|
else:
|
||||||
|
print("Waka Time Api Key Not Configured Properly")
|
||||||
|
except Exception as e:
|
||||||
|
print("Waka Time Api Key Not Configured" + str(e))
|
||||||
|
|
||||||
if showCommit.lower() in ['true', '1', 't', 'y', 'yes']:
|
if showCommit.lower() in ['true', '1', 't', 'y', 'yes']:
|
||||||
stats = stats + generate_commit_list() + '\n\n'
|
try:
|
||||||
|
stats = stats + generate_commit_list() + '\n\n'
|
||||||
|
except Exception as ex:
|
||||||
|
print("GitHub Personal access token not configured properly or invalid" + str(ex))
|
||||||
|
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
@@ -209,12 +210,16 @@ def generate_new_readme(stats: str, readme: str):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
g = Github(ghtoken)
|
try:
|
||||||
repo = g.get_repo(f"{user}/{user}")
|
g = Github(ghtoken)
|
||||||
contents = repo.get_readme()
|
repo = g.get_repo(f"{user}/{user}")
|
||||||
waka_stats = get_stats()
|
contents = repo.get_readme()
|
||||||
rdmd = decode_readme(contents.content)
|
headers = {"Authorization": "Bearer " + ghtoken}
|
||||||
new_readme = generate_new_readme(stats=waka_stats, readme=rdmd)
|
waka_stats = get_stats()
|
||||||
if new_readme != rdmd:
|
rdmd = decode_readme(contents.content)
|
||||||
repo.update_file(path=contents.path, message='Updated with Dev Metrics',
|
new_readme = generate_new_readme(stats=waka_stats, readme=rdmd)
|
||||||
content=new_readme, sha=contents.sha, branch='master')
|
if new_readme != rdmd:
|
||||||
|
repo.update_file(path=contents.path, message='Updated with Dev Metrics',
|
||||||
|
content=new_readme, sha=contents.sha, branch='master')
|
||||||
|
except Exception as e:
|
||||||
|
print("Exception Occurred" + str(e))
|
||||||
|
|||||||
Reference in New Issue
Block a user