From 0dc06a87bd49052a4a4a1d25624d9a1ae7dc3a9d Mon Sep 17 00:00:00 2001 From: joe733 Date: Wed, 15 Jul 2020 12:12:23 +0530 Subject: [PATCH 1/5] fix & feat: added week numbers; applied linting; --- README.md | 18 +++++++++------- main.py | 63 ++++++++++++++++++++++++++++++++----------------------- 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 1111415..dd6e328 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,23 @@ # Dev Metrics in Readme + ![Project Preview](https://user-images.githubusercontent.com/8397274/87243943-e6b45c00-c457-11ea-94c9-2aa0bf241be8.png) ---- -[WakaTime](https://wakatime.com) Weekly Metrics on your Profile Readme: +[WakaTime](https://wakatime.com) Weekly Metrics on your Profile Readme: ## Prep Work + 1. You need to update the markdown file(.md) with 2 comments. You can refer [here](#update-your-readme) for updating it. -2. You'll need a Wakatime API Key. You can get that from your Wakatime Account Settings - - You can refer [here](#new-to-wakatime),if you're new to Wakatime - - +2. You'll need a WakaTime API Key. You can get that from your WakaTime Account Settings + - You can refer [here](#new-to-wakatime), if you're new to WakaTime 3. **Optional** You'll need a GitHub API Token with `repo` scope from [here](https://github.com/settings/tokens) if you're running the action not in your Profile Repository - You can use [this](#other-repository-not-profile) example to work it out -4. You need to save the Wakatime API Key (and the GitHub API Token, if you need it) in the repository secrets. You can find that in the Settings of your Repository.Be sure to save those as the following. - - Wakatime-api-key as `WAKATIME_API_KEY = `and +4. You need to save the WakaTime API Key (and the GitHub API Token, if you need it) in the repository secrets. You can find that in the Settings of your Repository.Be sure to save those as the following. + - WakaTime-api-key as `WAKATIME_API_KEY = `and - The GitHub Access Token as `GH_TOKEN=` -5. You can follow either of the Two Examples according to your needs to get started with. +5. You can follow either of the Two Examples according to your needs to get started with. + > I strongly suggest you to run the Action in your Profile Repo since you won't be needing a GitHub Access Token This Action will run everyday at 00.00 UTC @@ -72,7 +74,7 @@ jobs: You'll need to get a [GitHub Access Token](https://docs.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token) with a `repo` scope and save it in the Repo Secrets `GH_TOKEN = ` -Here is Sample Worflow File for running it: +Here is Sample Workflow File for running it: ```yml name: Waka Readme diff --git a/main.py b/main.py index 04c4588..cc98a0a 100644 --- a/main.py +++ b/main.py @@ -1,26 +1,35 @@ -import base64 -import requests +''' +WakaTime progress visualizer +''' + import re import os +import base64 +import datetime +import requests from github import Github + START_COMMENT = '' END_COMMENT = '' -listReg = f'{START_COMMENT}[\\s\\S]+{END_COMMENT}' +listReg = f"{START_COMMENT}[\\s\\S]+{END_COMMENT}" +this_week = datetime.datetime.now().strftime('%W') -user = os.getenv("INPUT_USERNAME") -waka_key = os.getenv("INPUT_WAKATIME_API_KEY") -ghtoken = os.getenv("INPUT_GH_TOKEN") +user = os.getenv('INPUT_USERNAME') +waka_key = os.getenv('INPUT_WAKATIME_API_KEY') +ghtoken = os.getenv('INPUT_GH_TOKEN') -def makeGraph(percent: float): - done_block = "█" - empty_block = "░" +def make_graph(percent: float): + '''Make progress graph from API graph''' + done_block = '█' + empty_block = '░' pc_rnd = round(percent) - return (f'{done_block*int(pc_rnd/4)}{empty_block*int( 25-int(pc_rnd/4))}') + return f"{done_block*int(pc_rnd/4)}{empty_block*int(25-int(pc_rnd/4))}" -def getStats(): +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() lang_data = data['data']['languages'] @@ -28,29 +37,31 @@ def getStats(): for l in lang_data[:5]: ln = len(l['name']) ln_text = len(l['text']) - op = f"{l['name']}{' '*(12-ln)}{l['text']}{' '*(20-ln_text)}{makeGraph(l['percent'])} {l['percent']}" + op = f"{l['name']}{' '*(12-ln)}{l['text']}{' '*(20-ln_text)}{make_graph(l['percent'])} {l['percent']}" data_list.append(op) - data = " \n".join(data_list) - return ("```text\n"+data+"\n```") + data = ' \n'.join(data_list) + return '```text\n'+'Week #'+this_week+'\n'+data+'\n```' -def decodeReadme(data: str): - decodedBytes = base64.b64decode(data) - return str(decodedBytes, "utf-8") +def decode_readme(data: str): + '''Decode the contets of old readme''' + decoded_bytes = base64.b64decode(data) + return str(decoded_bytes, 'utf-8') -def generatenewReadme(stats: str, readme: str): - statsinReadme = f"{START_COMMENT}\n{stats}\n{END_COMMENT}" - return re.sub(listReg, statsinReadme, readme) +def generate_new_readme(stats: str, readme: 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) repo = g.get_repo(f"{user}/{user}") contents = repo.get_readme() - stats = getStats() - rdmd = decodeReadme(contents.content) - newreadme = generatenewReadme(stats=stats, readme=rdmd) - if newreadme != rdmd: - repo.update_file(path=contents.path, message="Updated with Dev Metrics", - content=newreadme, sha=contents.sha, branch="master") + stats = get_stats() + rdmd = decode_readme(contents.content) + new_readme = generate_new_readme(stats=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') From b9ef50f20ada92e1fa828c5ead51cb22302d8e51 Mon Sep 17 00:00:00 2001 From: joe733 Date: Wed, 15 Jul 2020 12:18:05 +0530 Subject: [PATCH 2/5] fix: Redefining name 'stats' from outer scope ln62 --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index cc98a0a..01524ec 100644 --- a/main.py +++ b/main.py @@ -59,9 +59,9 @@ if __name__ == '__main__': g = Github(ghtoken) repo = g.get_repo(f"{user}/{user}") contents = repo.get_readme() - stats = get_stats() + waka_stats = get_stats() rdmd = decode_readme(contents.content) - new_readme = generate_new_readme(stats=stats, readme=rdmd) + 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') From 4d999ae477a3e696976830bf67bad87a835d4483 Mon Sep 17 00:00:00 2001 From: joe733 Date: Wed, 15 Jul 2020 12:18:47 +0530 Subject: [PATCH 3/5] fix: one more linting :/ --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dd6e328..c5ad1d1 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ These lines will be our entry-points for the dev metrics. WakaTime gives you an idea of the time you really spent on coding. This helps you boost your productivity and competitive edge. -- Head over to https://wakatime.com and create an account. +- Head over to and create an account. - Get your WakaTime API Key from your [Account Settings in WakaTime](https://wakatime.com/settings/account). - Install the [WakaTime plugin](https://wakatime.com/plugins) in your favourite editor / IDE. - Paste in your API key to start the analysis. From c6628c271b7cc7c42d5a536c95ab35f39e0aae60 Mon Sep 17 00:00:00 2001 From: Jovial Joe Jayarson Date: Fri, 17 Jul 2020 08:58:18 +0530 Subject: [PATCH 4/5] feat: Added week span --- main.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 01524ec..075a944 100644 --- a/main.py +++ b/main.py @@ -13,12 +13,18 @@ from github import Github START_COMMENT = '' END_COMMENT = '' listReg = f"{START_COMMENT}[\\s\\S]+{END_COMMENT}" -this_week = datetime.datetime.now().strftime('%W') user = os.getenv('INPUT_USERNAME') waka_key = os.getenv('INPUT_WAKATIME_API_KEY') ghtoken = os.getenv('INPUT_GH_TOKEN') +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''' @@ -37,10 +43,10 @@ def get_stats(): for l in lang_data[:5]: ln = len(l['name']) ln_text = len(l['text']) - op = f"{l['name']}{' '*(12-ln)}{l['text']}{' '*(20-ln_text)}{make_graph(l['percent'])} {l['percent']}" + op = f"{l['name']}{' '*(12-ln)}{l['text']}{' '*(20-ln_text)}{make_graph(l['percent'])} {l['percent']}%" data_list.append(op) data = ' \n'.join(data_list) - return '```text\n'+'Week #'+this_week+'\n'+data+'\n```' + return '```text\n'+this_week()+'\n\n'+data+'\n```' def decode_readme(data: str): From 8d201a6313dd35335434141e8335d53582ee96de Mon Sep 17 00:00:00 2001 From: Jovial Joe Jayarson Date: Fri, 17 Jul 2020 08:58:18 +0530 Subject: [PATCH 5/5] feat: Added week span and % --- main.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 01524ec..075a944 100644 --- a/main.py +++ b/main.py @@ -13,12 +13,18 @@ from github import Github START_COMMENT = '' END_COMMENT = '' listReg = f"{START_COMMENT}[\\s\\S]+{END_COMMENT}" -this_week = datetime.datetime.now().strftime('%W') user = os.getenv('INPUT_USERNAME') waka_key = os.getenv('INPUT_WAKATIME_API_KEY') ghtoken = os.getenv('INPUT_GH_TOKEN') +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''' @@ -37,10 +43,10 @@ def get_stats(): for l in lang_data[:5]: ln = len(l['name']) ln_text = len(l['text']) - op = f"{l['name']}{' '*(12-ln)}{l['text']}{' '*(20-ln_text)}{make_graph(l['percent'])} {l['percent']}" + op = f"{l['name']}{' '*(12-ln)}{l['text']}{' '*(20-ln_text)}{make_graph(l['percent'])} {l['percent']}%" data_list.append(op) data = ' \n'.join(data_list) - return '```text\n'+'Week #'+this_week+'\n'+data+'\n```' + return '```text\n'+this_week()+'\n\n'+data+'\n```' def decode_readme(data: str):