You've already forked wakapi-readme-stats
FIX: Added Error Handling
This commit is contained in:
39
main.py
39
main.py
@@ -1,11 +1,10 @@
|
||||
'''
|
||||
WakaTime progress visualizer
|
||||
Readme Development Metrics With waka time progress
|
||||
'''
|
||||
|
||||
import re
|
||||
import os
|
||||
import base64
|
||||
import pytz
|
||||
import requests
|
||||
from github import Github
|
||||
import datetime
|
||||
@@ -23,8 +22,8 @@ showProjects = os.getenv('INPUT_SHOW_PROJECTS')
|
||||
showEditors = os.getenv('INPUT_SHOW_EDITORS')
|
||||
showOs = os.getenv('INPUT_SHOW_OS')
|
||||
showCommit = os.getenv('INPUT_SHOW_COMMIT')
|
||||
showLanguage = os.getenv('INPUT_SHOW_LANGUAGE')
|
||||
|
||||
headers = {"Authorization": "Bearer " + ghtoken}
|
||||
# The GraphQL query to get commit data.
|
||||
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)
|
||||
if request.status_code == 200:
|
||||
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))
|
||||
|
||||
|
||||
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):
|
||||
'''Make progress graph from API graph'''
|
||||
done_block = '█'
|
||||
@@ -121,7 +111,6 @@ def generate_commit_list():
|
||||
username = result["data"]["viewer"]["login"]
|
||||
id = result["data"]["viewer"]["id"]
|
||||
print("user {} id {}".format(username, id))
|
||||
print("on new version")
|
||||
|
||||
result = run_query(createContributedRepoQuery.substitute(username=username))
|
||||
nodes = result["data"]["user"]["repositoriesContributedTo"]["nodes"]
|
||||
@@ -165,14 +154,19 @@ def generate_commit_list():
|
||||
|
||||
def get_stats():
|
||||
'''Gets API data and returns markdown progress'''
|
||||
data = requests.get(
|
||||
f"https://wakatime.com/api/v1/users/current/stats/last_7_days?api_key={waka_key}").json()
|
||||
stats = ''
|
||||
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:
|
||||
data = request.json()
|
||||
stats = stats + '```text\n'
|
||||
if showTimeZone.lower() in ['true', '1', 't', 'y', 'yes']:
|
||||
timezone = data['data']['timezone']
|
||||
stats = stats + '⌚︎ Timezone: ' + timezone + '\n\n'
|
||||
|
||||
if showLanguage.lower() in ['true', '1', 't', 'y', 'yes']:
|
||||
lang_list = make_list(data['data']['languages'])
|
||||
stats = stats + '💬 Languages: \n' + lang_list + '\n\n'
|
||||
|
||||
@@ -189,9 +183,16 @@ def get_stats():
|
||||
stats = stats + '💻 Operating Systems: \n' + os_list + '\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']:
|
||||
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
|
||||
|
||||
@@ -209,12 +210,16 @@ def generate_new_readme(stats: str, readme: str):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
g = Github(ghtoken)
|
||||
repo = g.get_repo(f"{user}/{user}")
|
||||
contents = repo.get_readme()
|
||||
headers = {"Authorization": "Bearer " + ghtoken}
|
||||
waka_stats = get_stats()
|
||||
rdmd = decode_readme(contents.content)
|
||||
new_readme = generate_new_readme(stats=waka_stats, readme=rdmd)
|
||||
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