-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathmain.py
110 lines (94 loc) · 3.87 KB
/
main.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
'''
WakaTime progress visualizer
'''
import re
import os
import base64
import sys
import datetime
import requests
from github import Github, GithubException
START_COMMENT = '<!--START_SECTION:waka-->'
END_COMMENT = '<!--END_SECTION:waka-->'
GRAPH_LENGTH = 25
listReg = f"{START_COMMENT}[\\s\\S]+{END_COMMENT}"
repository = os.getenv('INPUT_REPOSITORY')
waka_key = os.getenv('INPUT_WAKATIME_API_KEY')
ghtoken = os.getenv('INPUT_GH_TOKEN')
show_title = os.getenv("INPUT_SHOW_TITLE")
commit_message = os.getenv("INPUT_COMMIT_MESSAGE")
blocks = os.getenv("INPUT_BLOCKS")
def this_week() -> str:
'''Returns a week streak'''
week_end = datetime.datetime.today() - datetime.timedelta(days=1)
week_start = week_end - datetime.timedelta(days=7)
print("Week header created")
return f"Week: {week_start.strftime('%d %B, %Y')} - {week_end.strftime('%d %B, %Y')}"
def make_graph(percent: float, blocks: str) -> str:
'''Make progress graph from API graph'''
if len(blocks) < 2:
raise "The BLOCKS need to have at least two characters."
divs = len(blocks) - 1
graph = blocks[-1] * int(percent / 100 * GRAPH_LENGTH + 0.5 / divs)
remainder_block = int((percent / 100 * GRAPH_LENGTH - len(graph)) * divs + 0.5)
if remainder_block > 0:
graph += blocks[remainder_block]
graph += blocks[0] * (GRAPH_LENGTH - len(graph))
return graph
def get_stats() -> str:
'''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()
try:
lang_data = data['data']['languages']
except KeyError:
print("Please Add your WakaTime API Key to the Repository Secrets")
sys.exit(1)
data_list = []
try:
pad = len(max([l['name'] for l in lang_data[:5]], key=len))
except ValueError:
print("The Data seems to be empty. Please wait for a day for the data to be filled in.")
return '```text\nNo Activity tracked this Week\n```'
for lang in lang_data[:5]:
if lang['hours'] == 0 and lang['minutes'] == 0:
continue
lth = len(lang['name'])
ln_text = len(lang['text'])
# following line provides a neat finish
fmt_percent = format(lang['percent'], '0.2f').zfill(5)
data_list.append(
f"{lang['name']}{' '*(pad + 3 - lth)}{lang['text']}{' '*(16 - ln_text)}{make_graph(lang['percent'], blocks)} {fmt_percent} % ")
print("Graph Generated")
data = '\n'.join(data_list)
if show_title == 'true':
print("Stats with Weeks in Title Generated")
return '```text\n'+this_week()+'\n\n'+data+'\n```'
else:
print("Usual Stats Generated")
return '```text\n'+data+'\n```'
def decode_readme(data: str) -> str:
'''Decode the contents of old readme'''
decoded_bytes = base64.b64decode(data)
return str(decoded_bytes, 'utf-8')
def generate_new_readme(stats: str, readme: str) -> str:
'''Generate a new Readme.md'''
stats_in_readme = f"{START_COMMENT}\n{stats}\n{END_COMMENT}"
return re.sub(listReg, stats_in_readme, readme)
if __name__ == '__main__':
g = Github(ghtoken)
try:
repo = g.get_repo(repository)
except GithubException:
print("Authentication Error. Try saving a GitHub Token in your Repo Secrets or Use the GitHub Actions Token, which is automatically used by the action.")
sys.exit(1)
if len(blocks) < 1:
print("Invalid blocks string. Please provide provide a string with 2 or more characters. Eg. '░▒▓█'")
sys.exit(1)
contents = repo.get_readme()
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=commit_message,
content=new_readme, sha=contents.sha)