feat: bumped versions, upd. deps
- Fix issues with retry while fetching API data - Made readme more reader friendly - Some improvements in log messages - Update project dependencies
This commit is contained in:
parent
4518aa2e35
commit
30c19dde74
@ -1,13 +1,16 @@
|
||||
# Contributing
|
||||
|
||||
First off, thanks! You can contribute to the repo via the following steps.
|
||||

|
||||
|
||||
First off, thank you! Please follow along.
|
||||
|
||||
1. Fork this repository and clone your fork into a local machine.
|
||||
2. Install poetry with: `curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - --version 1.1.13`
|
||||
2. Install poetry with: `curl -sSL https://install.python-poetry.org | python -`
|
||||
3. Open a terminal in the cloned folder and create a virtual environment using: `poetry shell` and install dependencies with `poetry install`
|
||||
4. You can put the environment variable in a local `.env` file
|
||||
5. Test the program `python -m unittest discover`. Read [main.py:L289](main.py#L289) before step 6.
|
||||
6. Finally run it in development mode with `python -m main --dev`.
|
||||
4. Put environment variables in a local `.env` file
|
||||
5. Test the program `python -m unittest discover`.
|
||||
6. Read [main.py:L389](main.py#L389) before step 7.
|
||||
7. Finally run it in development mode with `python -m main --dev`.
|
||||
|
||||
## Resources
|
||||
|
||||
|
@ -9,7 +9,7 @@ ENV PYTHONFAULTHANDLER=1 \
|
||||
PIP_DISABLE_PIP_VERSION_CHECK=on \
|
||||
PIP_DEFAULT_TIMEOUT=100 \
|
||||
# poetry:
|
||||
POETRY_VERSION=1.1.13 \
|
||||
POETRY_VERSION=1.1.14 \
|
||||
POETRY_NO_INTERACTION=1 \
|
||||
POETRY_CACHE_DIR='/var/cache/pypoetry' \
|
||||
PATH="$PATH:/root/.local/bin"
|
||||
|
276
README.md
276
README.md
@ -6,239 +6,145 @@
|
||||
|
||||
# Dev Metrics in Readme [](https://travis-ci.com/athul/waka-readme)
|
||||
|
||||
[WakaTime](https://wakatime.com) weekly metrics on your profile readme.
|
||||
|
||||

|
||||
|
||||
[WakaTime](https://wakatime.com) Weekly Metrics on your Profile Readme:
|
||||
|
||||
**Forum**: [GitHub Discussions](https://github.com/athul/waka-readme/discussions)
|
||||
|
||||
## Prep Work
|
||||
|
||||
1. You need to update a markdown file (`.md`) with 2 special comments. You can refer [this](#update-your-readme) to update it.
|
||||
2. You'll need a WakaTime API Key, which you can get from your WakaTime Account Settings. Click [here](#new-to-wakatime), if you're new to WakaTime.
|
||||
3. **Optionally** you might need a GitHub API Token with `repo` scope, generated from [here](https://github.com/settings/tokens), if you're running this 'action' on any repo other than your [profile repository](https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-github-profile/customizing-your-profile/managing-your-profile-readme). Then go to [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 your repository settings. Be sure to save those as the following.
|
||||
- WakaTime-api-key as `WAKATIME_API_KEY = <your wakatime API Key>` and
|
||||
- The GitHub Access Token as `GH_TOKEN=<your github access token>`
|
||||
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.
|
||||
|
||||
## Update your Readme
|
||||
|
||||
Add comments to your `README.md` like this:
|
||||
|
||||
```md
|
||||
<!--START_SECTION:waka-->
|
||||
<!--END_SECTION:waka-->
|
||||
```
|
||||
|
||||
These lines will be the entry-points for dev metrics.
|
||||
:speech_balloon: **Forum** | [GitHub discussions][gh_discuss]
|
||||
|
||||
## New to WakaTime?
|
||||
|
||||
> Nope? Skip to [prep work](#prep-work).
|
||||
|
||||
WakaTime gives you an idea of the time you spent on coding. This helps you boost your productivity and competitive edge.
|
||||
|
||||
- Head over to <https://wakatime.com> 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.
|
||||
1. Head over to <https://wakatime.com/> and create an account.
|
||||
2. After logging in get your WakaTime API Key from <https://wakatime.com/api-key/>.
|
||||
3. Install [WakaTime plugin][waka_plugins] in your favorite editor / IDE.
|
||||
4. Paste in your API key to start telemetry.
|
||||
|
||||
Alternatively, you can also choose to fetch data from third-party WakaTime-compatible services like [Wakapi](https://wakapi.dev) or [Hakatime](https://github.com/mujx/hakatime) instead. For details, see [extras](#extras) section below.
|
||||
:information_source: **Info** | You can read [WakaTime help][waka_help] to know more about configurations.
|
||||
Alternatively, you can also fetch data from WakaTime compatible services like [Wakapi][wakapi] or [Hakatime][hakatime].
|
||||
|
||||
## Profile Repository
|
||||
## Prep Work
|
||||
|
||||
_If you're executing the workflow on your Profile Repository (`<username>/<username>`)_
|
||||
A GitHub repository and a README file is required. We'll be making use of readme in the [profile repository][profile_readme]\*.
|
||||
|
||||
Please follow the steps below:
|
||||
- Save the README file after copy-pasting the following special comments. Your dev-metics will show up in between.
|
||||
|
||||
1. Go to your `<username>/<username>/actions`, hit `New workflow` and `set up a workflow yourself`, then delete all the default content.
|
||||
2. Copy the following code and paste it to your new workflow file you just created and save/commit it as `wakatime.yml`.
|
||||
```md
|
||||
|
||||
```yml
|
||||
name: Waka Readme
|
||||
<!--START_SECTION:waka-->
|
||||
<!--END_SECTION:waka-->
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
schedule:
|
||||
# Runs at every 12AM UTC
|
||||
- cron: "0 0 * * *"
|
||||
```
|
||||
|
||||
jobs:
|
||||
update-readme:
|
||||
name: Update this repo's README
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: athul/waka-readme@master
|
||||
with:
|
||||
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
|
||||
```
|
||||
- Navigate to your repo's `Settings > Secrets` and add a new secret *named* `WAKATIME_API_KEY` with your API key as it's *value*.
|
||||
> Or use the url <https://github.com/USERNAME/USERNAME/settings/secrets/actions/new> by replacing the `USERNAME` with your own username.
|
||||
> ![new_secrets_actions][new_secrets_actions]
|
||||
|
||||
3. Go to your repo secrets by hitting `Settings > Secrets`. You can also enter the url <https://github.com/USERNAME/USERNAME/settings/secrets/actions/new> . Please replace the `USERNAME` with your own username.
|
||||
4. Create a new Secret. `Name: WAKATIME_API_KEY` and `Value:` Paste the Wakatime API key here.
|
||||
- If you're not using [profile repository][profile_readme], add another secret *named* `GH_TOKEN` and insert your [GitHub token][gh_access_token]\* in place of *value*.
|
||||
|
||||

|
||||
- Create a new workflow file (`waka-readme.yml`) inside `.github/workflows/` folder of your repository. You can create it from a template using the *actions tab* of your repository too.
|
||||
- Clear any existing contents, add the following lines and save the file.
|
||||
|
||||
If you don't know what the key is, please go to [Wakatime API](https://wakatime.com/api-key) to get your API Key (See [New to WakaTime?](#new-to-wakatime)).
|
||||
```yml
|
||||
name: Waka Readme
|
||||
|
||||
Add secret.
|
||||
on:
|
||||
workflow_dispatch: # for manual workflow trigger
|
||||
schedule:
|
||||
- cron: '0 0 * * *' # runs at every 12AM UTC
|
||||
|
||||
~~5. Go to Action tab, click on `Waka Readme`, and `Run workflow`.~~
|
||||
jobs:
|
||||
update-readme:
|
||||
name: WakaReadme DevMetrics
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: athul/waka-readme@master
|
||||
with:
|
||||
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
|
||||
# following flags are required, only if this is not on
|
||||
# profile readme, remove the leading `#` to use them
|
||||
#GH_TOKEN: ${{ secrets.GH_TOKEN }}
|
||||
#REPOSITORY: <gh_username/gh_username>
|
||||
```
|
||||
|
||||
5. Go to your profile page. you will be able to see it in 24 hrs.
|
||||
## Tweaks
|
||||
|
||||
## Other Repository (not Profile)
|
||||
There are many flags that you can tweak to suit your taste!
|
||||
|
||||
_If you're executing the workflow on another repo other than `<username>/<username>`_
|
||||
| Flag | Default | Options | Meaning |
|
||||
| ------------------ | -------------------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- |
|
||||
| `API_BASE_URL` | `https://wakapi.dev/api` | `https://wakapi.dev/api`, `https://hakatime.mtx-dev.xyz/api` | Integration with WakaTime compatible services like [Wakapi][wakapi] & [Hakatime][hakatime] are possible |
|
||||
| `REPOSITORY` | `<gh_username>/<gh_username>` | `<gh_username>/<repo_name>` | Waka-readme stats will appear on the provided repository |
|
||||
| `COMMIT_MESSAGE` | `Updated waka-readme graph with new metrics` | anything else! | Messaged used when committing updated stats |
|
||||
| `SHOW_TITLE` | `false` | `false`, `true` | Add title to waka-readme stats blob |
|
||||
| `BLOCKS` | `░▒▓█` | `░▒▓█`, `⣀⣄⣤⣦⣶⣷⣿`, `-#`, you can be creative! | Ascii art used to build stats graph |
|
||||
| `TIME_RANGE` | `last_7_days` | `last_7_days`, `last_30_days`, `last_6_months`, `last_year`, `all_time` | String representing a dispensation from which stats are aggregated |
|
||||
| `SHOW_TIME` | `true` | `false`, `true` | Displays the amount of time spent for each language |
|
||||
| `SHOW_TOTAL` | `false` | `false`, `true` | Show total coding time |
|
||||
| `SHOW_MASKED_TIME` | `false` | `false`, `true` | Adds total coding time including unclassified languages (overrides: `SHOW_TOTAL`) |
|
||||
|
||||
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 = <Your GitHub Access Token>`
|
||||
# Example
|
||||
|
||||
Here is Sample Workflow File for running it:
|
||||
**`waka-readme.yml`**
|
||||
|
||||
```yml
|
||||
name: Waka Readme
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
schedule:
|
||||
# Runs at 12am UTC
|
||||
- cron: "0 0 * * *"
|
||||
- cron: '0 0 * * *'
|
||||
|
||||
jobs:
|
||||
update-readme:
|
||||
name: Update Readme with Metrics
|
||||
name: WakaReadme DevMetrics
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: athul/waka-readme@master
|
||||
with:
|
||||
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
|
||||
GH_TOKEN: ${{ secrets.GH_TOKEN }}
|
||||
REPOSITORY: <username/username> # optional, By default, it will automatically use the repository which is executing the workflow.
|
||||
SHOW_TITLE: true
|
||||
BLOCKS: ->
|
||||
TIME_RANGE: all_time
|
||||
SHOW_TIME: true
|
||||
SHOW_MASKED_TIME: true
|
||||
```
|
||||
|
||||
## Extras
|
||||
**`README.md`**
|
||||
|
||||
1. As an alternative to official WakaTime, _waka-readme_ also integrates with WakaTime-compatible services like [Wakapi](https://wakapi.dev) and [Hakatime](https://github.com/mujx/hakatime). To use one of these, **adapt the API URL accordingly and use the respective service's API key** instead:
|
||||
```md
|
||||
From: 10 July 2020 - To: 06 August 2022
|
||||
|
||||
```yml
|
||||
- uses: athul/waka-readme@master
|
||||
with:
|
||||
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
|
||||
API_BASE_URL: https://wakapi.dev/api
|
||||
```
|
||||
Total Time: 1,464 hrs 54 mins
|
||||
|
||||
2. If you want to add a title for your stats, by setting `SHOW_TITLE: true` (which by default is `false`) in your workflow file like this
|
||||
Python 859 hrs 29 mins >>>>>>>>>>>>>>----------- 54.68 %
|
||||
Markdown 132 hrs 33 mins >>----------------------- 08.43 %
|
||||
TeX 103 hrs 52 mins >>----------------------- 06.61 %
|
||||
HTML 94 hrs 48 mins >>----------------------- 06.03 %
|
||||
Nim 64 hrs 31 mins >------------------------ 04.11 %
|
||||
Other 47 hrs 58 mins >------------------------ 03.05 %
|
||||
```
|
||||
|
||||
```yml
|
||||
- uses: athul/waka-readme@master
|
||||
with:
|
||||
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
|
||||
GH_TOKEN: ${{ secrets.GH_TOKEN }}
|
||||
SHOW_TITLE: true
|
||||
```
|
||||
## Why only the language stats (and not other data) from the API?
|
||||
|
||||
Here is an example output with `SHOW_TITLE` set to `true`.
|
||||
I am a fan of minimal designs and the profile readme is a great way to show off your skills and interests. The WakaTime API, gets us a **lot of data** about a person's **coding activity including the editors and Operating Systems you used and the projects you worked on**. Some of these projects maybe secretive and should not be shown out to the public. Using up more data via the Wakatime API will clutter the profile readme and hinder your chances on displaying what you provide **value to the community** like the pinned Repositories. I believe that **Coding Stats is nerdiest of all** since you can tell the community that you are ***exercising these languages or learning a new language***, this will also show that you spend some amount of time to learn and exercise your development skills. That's what matters in the end :heart:
|
||||
|
||||
```text
|
||||
From: 12 February, 2022 - To: 19 February, 2022
|
||||
---
|
||||
|
||||
Python 8 hrs 52 mins ███████████████████░░░░░░ 75.87 %
|
||||
Go 1 hr 15 mins ██░░░░░░░░░░░░░░░░░░░░░░░ 10.79 %
|
||||
Markdown 52 mins █░░░░░░░░░░░░░░░░░░░░░░░░ 07.43 %
|
||||
Docker 16 mins ░░░░░░░░░░░░░░░░░░░░░░░░░ 02.32 %
|
||||
YAML 7 mins ░░░░░░░░░░░░░░░░░░░░░░░░░ 01.07 %
|
||||
```
|
||||
<sup>*</sup>`REPOSITORY` flag and `GH_TOKEN` secret are required you're not using profile readme.
|
||||
|
||||
3. You can specify the time range in the parameter (default `last_7_days`):
|
||||
[//]: #(Links)
|
||||
|
||||
```yml
|
||||
- uses: athul/waka-readme@master
|
||||
with:
|
||||
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
|
||||
TIME_RANGE: last_30_days
|
||||
```
|
||||
|
||||
See [Wakatime API docs](https://wakatime.com/developers#stats) for more possible values.
|
||||
|
||||
4. You can specify a commit message to override the default _"Updated the Graph with new Metrics"_. Here is how you do it
|
||||
|
||||
```yml
|
||||
- uses: athul/waka-readme@master
|
||||
with:
|
||||
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
|
||||
GH_TOKEN: ${{ secrets.GH_TOKEN }}
|
||||
COMMIT_MESSAGE: Updated the Readme
|
||||
```
|
||||
|
||||
If no commit message is specified in the `yml` file, it defaults to _"Updated the Graph with new Metrics"_
|
||||
|
||||
5. You can change the block characters to match with the style of your readme. By default the one show in the graphs before is used. Here is how you do it.
|
||||
|
||||
```yml
|
||||
- uses: athul/waka-readme@master
|
||||
with:
|
||||
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
|
||||
BLOCKS: ⣀⣄⣤⣦⣶⣷⣿
|
||||
```
|
||||
|
||||
Requires `BLOCKS` to be of at least 2 characters in length. It will change the graph to something like this:
|
||||
|
||||
```text
|
||||
Python 8 hrs 52 mins ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣀⣀⣀⣀⣀⣀ 75.87 %
|
||||
Go 1 hr 15 mins ⣿⣿⣦⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀ 10.79 %
|
||||
Markdown 52 mins ⣿⣿⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀ 07.43 %
|
||||
Docker 16 mins ⣤⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀ 02.32 %
|
||||
YAML 7 mins ⣄⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀ 01.07 %
|
||||
```
|
||||
|
||||
6. If you want to add total time in stats, by setting `SHOW_TOTAL: true` (which by default is `false`) in your workflow file like this:
|
||||
|
||||
```yml
|
||||
- uses: athul/waka-readme@master
|
||||
with:
|
||||
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
|
||||
GH_TOKEN: ${{ secrets.GH_TOKEN }}
|
||||
SHOW_TOTAL: true
|
||||
```
|
||||
|
||||
It will change the graph to something like this:
|
||||
|
||||
```text
|
||||
Total Time: 44 hrs 27 mins
|
||||
|
||||
Python 36 hrs 28 mins ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣀⣀⣀⣀⣀ 80.09 %
|
||||
YAML 2 hrs 30 mins ⣿⣤⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀ 05.52 %
|
||||
TOML 1 hr 36 mins ⣷⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀ 03.54 %
|
||||
Docker 1 hr 11 mins ⣶⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀ 02.61 %
|
||||
Other 1 hr 4 mins ⣶⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀ 02.37 %
|
||||
```
|
||||
|
||||
7. If you do not like to share how much time you spent on each language, you can add `SHOW_TIME: false` (by default it will be `true`) in your workflow file like so:
|
||||
|
||||
```yml
|
||||
- uses: athul/waka-readme@master
|
||||
with:
|
||||
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
|
||||
SHOW_TIME: false
|
||||
```
|
||||
|
||||
Here is an example output with `SHOW_TIME` set to `false`.
|
||||
|
||||
```text
|
||||
From: 12 February, 2022 - To: 19 February, 2022
|
||||
|
||||
PHP ████████████████████▒░░░░░░░░░░░░░░░░░░░░ 49.98 %
|
||||
Twig ████▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 11.07 %
|
||||
YAML ████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 09.77 %
|
||||
JavaScript ██▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 06.34 %
|
||||
Other ██▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 05.87 %
|
||||
```
|
||||
|
||||
> You can find all the options in [action.yml](action.yml) and a sample workflow [here](https://github.com/athul/athul/blob/master/.github/workflows/update-readme.yml).
|
||||
>
|
||||
> Tip 💡: Add `on: workflow_dispatch:` to enable manual runs. [See](https://github.com/joe733/joe733/blob/master/.github/workflows/waka.yml#L4).
|
||||
|
||||
## Why only the language stats and not other data from the API?
|
||||
|
||||
I am a fan of minimal designs and the profile readme is a great way to show off your skills and interests. The WakaTime API, gets us a **lot of data** about a person's **coding activity including the editors and Operating Systems you used and the projects you worked on**. Some of these projects maybe secretive and should not be shown out to the public. Using up more data via the Wakatime API will clutter the profile readme and hinder your chances on displaying what you provide **value to the community** like the pinned Repositories. I believe that **Coding Stats is nerdiest of all** since you can tell the community that you are _**exercising these languages or learning a new language**_, this will also show that you spend some amount of time to learn and exercise your development skills. That's what matters in the end :heart:
|
||||
[wakapi]: https://wakapi.dev
|
||||
[hakatime]: https://github.com/mujx/hakatime
|
||||
[workflow_dispatch]: https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/
|
||||
[waka_plugins]: https://wakatime.com/plugins
|
||||
[waka_help]: https://wakatime.com/help/editors
|
||||
[profile_readme]: https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-github-profile/customizing-your-profile/managing-your-profile-readme
|
||||
[new_secrets_actions]: https://user-images.githubusercontent.com/52720626/151221742-bc37d139-2bb3-4554-b27c-46b107d1f408.png
|
||||
[gh_access_token]: https://docs.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token
|
||||
[gh_discuss]: https://github.com/athul/waka-readme/discussions
|
||||
|
@ -24,7 +24,7 @@ inputs:
|
||||
|
||||
COMMIT_MESSAGE:
|
||||
description: "Add a commit message of your choice"
|
||||
default: "Updated WakaReadme graph with new metrics"
|
||||
default: "Updated waka-readme graph with new metrics"
|
||||
required: false
|
||||
|
||||
# content tweaks
|
||||
|
73
main.py
73
main.py
@ -44,8 +44,12 @@ import re
|
||||
import os
|
||||
|
||||
# external
|
||||
# # github
|
||||
from github import GithubException, Github
|
||||
import requests
|
||||
# # requests
|
||||
from requests.exceptions import RequestException
|
||||
from requests.adapters import HTTPAdapter, Retry
|
||||
from requests.sessions import Session
|
||||
|
||||
|
||||
# pylint: disable=logging-fstring-interpolation
|
||||
@ -188,7 +192,7 @@ def make_title(dawn: str, dusk: str, /) -> str:
|
||||
except ValueError as err:
|
||||
logger.error(err)
|
||||
sys.exit(1)
|
||||
logger.debug('Title was made')
|
||||
logger.debug('Title was made\n')
|
||||
return f'From: {start_date} - To: {end_date}'
|
||||
|
||||
|
||||
@ -202,7 +206,7 @@ def make_graph(
|
||||
|
||||
Makes time graph from the API's data.
|
||||
"""
|
||||
logger.debug(f'Generating graph for {lg_nm or "..."}')
|
||||
logger.debug(f'Generating graph for "{lg_nm or "..."}"')
|
||||
markers: int = len(block_style) - 1
|
||||
proportion: float = percent / 100 * gr_len
|
||||
graph_bar: str = block_style[-1] * int(proportion + 0.5 / markers)
|
||||
@ -236,13 +240,13 @@ def prep_content(stats: dict | None, /) -> str:
|
||||
contents += make_title(stats.get('start'), stats.get('end')) + '\n\n'
|
||||
|
||||
# make byline
|
||||
if wk_i.show_total_time and (
|
||||
total_time := stats.get('human_readable_total')
|
||||
):
|
||||
contents += f'Total Time: {total_time}\n\n'
|
||||
# # overrides 'human_readable_total'
|
||||
if wk_i.show_masked_time and (
|
||||
total_time := stats.get('human_readable_total_including_other_language')
|
||||
):
|
||||
# overrides 'human_readable_total'
|
||||
contents += f'Total Time: {total_time}\n\n'
|
||||
elif wk_i.show_total_time and (
|
||||
total_time := stats.get('human_readable_total')
|
||||
):
|
||||
contents += f'Total Time: {total_time}\n\n'
|
||||
|
||||
@ -271,7 +275,7 @@ def prep_content(stats: dict | None, /) -> str:
|
||||
if idx >= 5 or lang_name == 'Other':
|
||||
break
|
||||
|
||||
logger.debug('Contents were made')
|
||||
logger.debug('Contents were made\n')
|
||||
return contents.rstrip('\n')
|
||||
|
||||
|
||||
@ -282,34 +286,39 @@ def fetch_stats() -> Any:
|
||||
|
||||
Returns statistics as JSON string
|
||||
"""
|
||||
tries, statistic = 3, {}
|
||||
logger.debug('Fetching WakaTime statistics')
|
||||
attempts, statistic, retries = 2, {}, Retry( # for auto retry
|
||||
total=5,
|
||||
backoff_factor=0.5,
|
||||
status_forcelist=[500, 502, 503, 504],
|
||||
)
|
||||
encoded_key: str = str(b64encode(bytes(wk_i.waka_key, 'utf-8')), 'utf-8')
|
||||
|
||||
with requests.Session() as rqs:
|
||||
# why session? read @
|
||||
# https://docs.python-requests.org/en/latest/user/advanced/#session-objects
|
||||
while tries > 0:
|
||||
# TODO: instead of manual retry use requests.adapters.Retry with back-off and
|
||||
# requests.adapters.HTTPAdapter: see https://stackoverflow.com/questions/15431044/
|
||||
# making a request
|
||||
with Session() as rqs:
|
||||
rqs.mount('http://', HTTPAdapter(max_retries=retries))
|
||||
rqs.mount('https://', HTTPAdapter(max_retries=retries))
|
||||
|
||||
while attempts > 0:
|
||||
logger.debug('Fetching WakaTime statistics')
|
||||
resp = rqs.get(
|
||||
url=f'{wk_i.api_base_url.rstrip("/")}/v1/users/current/stats/{wk_i.time_range}',
|
||||
headers={'Authorization': f'Basic {encoded_key}'}
|
||||
headers={'Authorization': f'Basic {encoded_key}'},
|
||||
)
|
||||
logger.debug(
|
||||
f'API response @ trial #{4 - tries}: {resp.status_code} {resp.reason}'
|
||||
f'API response @ trial #{3 - attempts}: {resp.status_code} • {resp.reason}'
|
||||
)
|
||||
if resp.status_code == 200 and (statistic := resp.json()):
|
||||
logger.debug('Fetched WakaTime statistics')
|
||||
break
|
||||
logger.debug('Retrying in 3s ...')
|
||||
sleep(3)
|
||||
tries -= 1
|
||||
attempts -= 1
|
||||
|
||||
if err := (statistic.get('error') or statistic.get('errors')):
|
||||
logger.error(err)
|
||||
sys.exit(1)
|
||||
|
||||
print()
|
||||
return statistic.get('data')
|
||||
|
||||
|
||||
@ -320,21 +329,25 @@ def churn(old_readme: str, /) -> str | None:
|
||||
|
||||
Composes WakaTime stats within markdown code snippet
|
||||
"""
|
||||
# getting content
|
||||
try:
|
||||
if not (waka_stats := fetch_stats()):
|
||||
logger.error('Unable to fetch data, please rerun workflow')
|
||||
sys.exit(1)
|
||||
except requests.RequestException as rq_exp:
|
||||
except RequestException as rq_exp:
|
||||
logger.critical(rq_exp)
|
||||
sys.exit(1)
|
||||
|
||||
# processing content
|
||||
generated_content = prep_content(waka_stats)
|
||||
print('\n', generated_content, '\n', sep='')
|
||||
print(generated_content, '\n', sep='')
|
||||
new_readme = re.sub(
|
||||
pattern=wk_c.waka_block_pattern,
|
||||
repl=f'{wk_c.start_comment}\n\n```text\n{generated_content}\n```\n\n{wk_c.end_comment}',
|
||||
string=old_readme
|
||||
)
|
||||
if len(sys.argv) == 2 and sys.argv[1] == '--dev':
|
||||
logger.debug('Detected run in `dev` mode.')
|
||||
# to avoid accidentally writing back to Github
|
||||
# when developing and testing WakaReadme
|
||||
return None
|
||||
@ -347,23 +360,23 @@ def genesis() -> None:
|
||||
gh_connect = Github(wk_i.gh_token)
|
||||
gh_repo = gh_connect.get_repo(wk_i.repository)
|
||||
readme_file = gh_repo.get_readme()
|
||||
logger.debug('Decoding readme contents')
|
||||
logger.debug('Decoding readme contents\n')
|
||||
readme_contents = str(readme_file.decoded_content, encoding='utf-8')
|
||||
if new_content := churn(readme_contents):
|
||||
logger.debug('Updating readme')
|
||||
logger.debug('WakaReadme stats has changed')
|
||||
gh_repo.update_file(
|
||||
path=readme_file.path,
|
||||
message=wk_i.commit_message,
|
||||
content=new_content,
|
||||
sha=readme_file.sha
|
||||
)
|
||||
logger.info('Updated waka stats successfully')
|
||||
logger.info('Stats updated successfully')
|
||||
return
|
||||
logger.info('No changes were made')
|
||||
logger.info('WakaReadme was not updated')
|
||||
|
||||
|
||||
################### driver ###################
|
||||
|
||||
print()
|
||||
# configure logger
|
||||
logger.getLogger('urllib3').setLevel(logger.WARNING)
|
||||
logger.getLogger('github.Requester').setLevel(logger.WARNING)
|
||||
@ -395,7 +408,11 @@ if __name__ == '__main__':
|
||||
# run
|
||||
try:
|
||||
genesis()
|
||||
except KeyboardInterrupt:
|
||||
print()
|
||||
logger.error('Interrupt signal received')
|
||||
sys.exit(1)
|
||||
except GithubException as gh_exp:
|
||||
logger.critical(gh_exp)
|
||||
sys.exit(1)
|
||||
print('Thanks for using WakaReadme!')
|
||||
print('\nThanks for using WakaReadme!')
|
||||
|
24
poetry.lock
generated
24
poetry.lock
generated
@ -128,7 +128,7 @@ colorama = {version = ">=0.3.4", markers = "sys_platform == \"win32\""}
|
||||
win32-setctime = {version = ">=1.0.0", markers = "sys_platform == \"win32\""}
|
||||
|
||||
[package.extras]
|
||||
dev = ["colorama (>=0.3.4)", "docutils (==0.16)", "flake8 (>=3.7.7)", "tox (>=3.9.0)", "pytest (>=4.6.2)", "pytest-cov (>=2.7.1)", "black (>=19.10b0)", "isort (>=5.1.1)", "Sphinx (>=4.1.1)", "sphinx-autobuild (>=0.7.1)", "sphinx-rtd-theme (>=0.4.3)"]
|
||||
dev = ["sphinx-rtd-theme (>=0.4.3)", "sphinx-autobuild (>=0.7.1)", "Sphinx (>=4.1.1)", "isort (>=5.1.1)", "black (>=19.10b0)", "pytest-cov (>=2.7.1)", "pytest (>=4.6.2)", "tox (>=3.9.0)", "flake8 (>=3.7.7)", "docutils (==0.16)", "colorama (>=0.3.4)"]
|
||||
|
||||
[[package]]
|
||||
name = "mccabe"
|
||||
@ -152,11 +152,11 @@ test = ["appdirs (==1.4.4)", "pytest-cov (>=2.7)", "pytest-mock (>=3.6)", "pytes
|
||||
|
||||
[[package]]
|
||||
name = "pycodestyle"
|
||||
version = "2.8.0"
|
||||
version = "2.9.1"
|
||||
description = "Python style guide checker"
|
||||
category = "dev"
|
||||
optional = false
|
||||
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
|
||||
python-versions = ">=3.6"
|
||||
|
||||
[[package]]
|
||||
name = "pycparser"
|
||||
@ -192,10 +192,10 @@ optional = false
|
||||
python-versions = ">=3.6"
|
||||
|
||||
[package.extras]
|
||||
tests = ["coverage[toml] (==5.0.4)", "pytest (>=6.0.0,<7.0.0)"]
|
||||
docs = ["zope.interface", "sphinx-rtd-theme", "sphinx"]
|
||||
dev = ["pre-commit", "mypy", "coverage[toml] (==5.0.4)", "pytest (>=6.0.0,<7.0.0)", "cryptography (>=3.3.1)", "zope.interface", "sphinx-rtd-theme", "sphinx"]
|
||||
crypto = ["cryptography (>=3.3.1)"]
|
||||
dev = ["sphinx", "sphinx-rtd-theme", "zope.interface", "cryptography (>=3.3.1)", "pytest (>=6.0.0,<7.0.0)", "coverage[toml] (==5.0.4)", "mypy", "pre-commit"]
|
||||
docs = ["sphinx", "sphinx-rtd-theme", "zope.interface"]
|
||||
tests = ["pytest (>=6.0.0,<7.0.0)", "coverage[toml] (==5.0.4)"]
|
||||
|
||||
[[package]]
|
||||
name = "pylint"
|
||||
@ -289,7 +289,7 @@ python-versions = ">=3.6,<4.0"
|
||||
|
||||
[[package]]
|
||||
name = "urllib3"
|
||||
version = "1.26.10"
|
||||
version = "1.26.11"
|
||||
description = "HTTP library with thread-safe connection pooling, file post, and more."
|
||||
category = "main"
|
||||
optional = false
|
||||
@ -309,7 +309,7 @@ optional = false
|
||||
python-versions = ">=3.5"
|
||||
|
||||
[package.extras]
|
||||
dev = ["pytest (>=4.6.2)", "black (>=19.3b0)"]
|
||||
dev = ["black (>=19.3b0)", "pytest (>=4.6.2)"]
|
||||
|
||||
[[package]]
|
||||
name = "wrapt"
|
||||
@ -479,8 +479,8 @@ platformdirs = [
|
||||
{file = "platformdirs-2.5.2.tar.gz", hash = "sha256:58c8abb07dcb441e6ee4b11d8df0ac856038f944ab98b7be6b27b2a3c7feef19"},
|
||||
]
|
||||
pycodestyle = [
|
||||
{file = "pycodestyle-2.8.0-py2.py3-none-any.whl", hash = "sha256:720f8b39dde8b293825e7ff02c475f3077124006db4f440dcbc9a20b76548a20"},
|
||||
{file = "pycodestyle-2.8.0.tar.gz", hash = "sha256:eddd5847ef438ea1c7870ca7eb78a9d47ce0cdb4851a5523949f2601d0cbbe7f"},
|
||||
{file = "pycodestyle-2.9.1-py2.py3-none-any.whl", hash = "sha256:d1735fc58b418fd7c5f658d28d943854f8a849b01a5d0a1e6f3f3fdd0166804b"},
|
||||
{file = "pycodestyle-2.9.1.tar.gz", hash = "sha256:2c9607871d58c76354b697b42f5d57e1ada7d261c261efac224b664affdc5785"},
|
||||
]
|
||||
pycparser = [
|
||||
{file = "pycparser-2.21-py2.py3-none-any.whl", hash = "sha256:8ee45429555515e1f6b185e78100aea234072576aa43ab53aefcae078162fca9"},
|
||||
@ -531,8 +531,8 @@ tomlkit = [
|
||||
{file = "tomlkit-0.11.1.tar.gz", hash = "sha256:61901f81ff4017951119cd0d1ed9b7af31c821d6845c8c477587bbdcd5e5854e"},
|
||||
]
|
||||
urllib3 = [
|
||||
{file = "urllib3-1.26.10-py2.py3-none-any.whl", hash = "sha256:8298d6d56d39be0e3bc13c1c97d133f9b45d797169a0e11cdd0e0489d786f7ec"},
|
||||
{file = "urllib3-1.26.10.tar.gz", hash = "sha256:879ba4d1e89654d9769ce13121e0f94310ea32e8d2f8cf587b77c08bbcdb30d6"},
|
||||
{file = "urllib3-1.26.11-py2.py3-none-any.whl", hash = "sha256:c33ccba33c819596124764c23a97d25f32b28433ba0dedeb77d873a38722c9bc"},
|
||||
{file = "urllib3-1.26.11.tar.gz", hash = "sha256:ea6e8fb210b19d950fab93b60c9009226c63a28808bc8386e05301e25883ac0a"},
|
||||
]
|
||||
win32-setctime = [
|
||||
{file = "win32_setctime-1.1.0-py3-none-any.whl", hash = "sha256:231db239e959c2fe7eb1d7dc129f11172354f98361c4fa2d6d2d7e278baa8aad"},
|
||||
|
@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "waka-readme"
|
||||
version = "0.1.5"
|
||||
version = "0.1.6"
|
||||
description = "Wakatime Weekly Metrics on your Profile Readme."
|
||||
authors = ["Athul Cyriac Ajay <athul8720@gmail.com>"]
|
||||
license = "MIT"
|
||||
|
Loading…
x
Reference in New Issue
Block a user