Used Wakatime API

This commit is contained in:
athul 2020-07-10 00:58:46 +05:30
parent 50bc9e60c7
commit 74594fa648
2 changed files with 43 additions and 12 deletions

View File

@ -2,18 +2,28 @@
[Wakatime](https://wakatime.com) Weekly Metrics on your Profile Readme
## Update your Readme
Add a comment to your README like the follows
```md
<!--START_SECTION:waka-->
<!--END_SECTION:waka-->
```
The lines will be our entrypoints for our metrics.
## How to get it
**You Need to Have [waka-box](https://github.com/matchai/waka-box) in your Repo**
> *This maybe temporary and will be updated soon as I'll update this to an Action*
- Clone the Repo
- Install the dependencies `pip install -r requirements.txt`
- Get a GitHub Access Token with a `repo` scope
- Get a GitHub Access Token with a `repo` scope.
- Get your Wakatime API Key.
- Load env vars like
```text
GH_TOKEN = <ghtoken>
GIST_ID = <gist_id of the waka-box gist>
WAKATIME_API_KEY = <your wakatime API Key>
USERNAME = <github_username>
```

37
main.py
View File

@ -9,28 +9,49 @@ END_COMMENT = '<!--END_SECTION:waka-->'
listReg = f'{START_COMMENT}[\\s\\S]+{END_COMMENT}'
user = os.getenv("USERNAME")
gistid = os.getenv("GIST_ID")
waka_key = os.getenv("WAKATIME_API_KEY")
ghtoken = os.getenv("GH_TOKEN")
def makeGraph(percent: float):
done_block = ""
empty_block = ""
pc_rnd = round(percent)
return (f'{done_block*int(pc_rnd/4)}{empty_block*int( 25-int(pc_rnd/4))}')
def getStats():
data = requests.get(f"https://gist.githubusercontent.com/{user}/{gistid}/raw/").text
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']
data_list = []
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']}"
data_list.append(op)
data = " \n".join(data_list)
return ("```text\n"+data+"\n```")
def decodeReadme(data:str):
def decodeReadme(data: str):
decodedBytes = base64.b64decode(data)
decodedStr = str(decodedBytes, "utf-8")
return decodedStr
def generatenewReadme(stats:str,readme:str):
def generatenewReadme(stats: str, readme: str):
statsinReadme = f"{START_COMMENT}\n{stats}\n{END_COMMENT}"
newReadme = re.sub(listReg,statsinReadme,readme)
newReadme = re.sub(listReg, statsinReadme, readme)
return newReadme
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)
repo.update_file(path=contents.path,message="Updated with Dev Metrics",content=newreadme,sha=contents.sha,branch="master")
newreadme = generatenewReadme(stats=stats, readme=rdmd)
repo.update_file(path=contents.path, message="Updated with Dev Metrics",
content=newreadme, sha=contents.sha, branch="master")