Skip to content

Instantly share code, notes, and snippets.

@Kmaschta
Created May 5, 2017 15:21
Show Gist options
  • Save Kmaschta/bbd2da18d7d9a62c9206f1454e363400 to your computer and use it in GitHub Desktop.
Save Kmaschta/bbd2da18d7d9a62c9206f1454e363400 to your computer and use it in GitHub Desktop.
Retrieve a maximum of Wakatime stat
#!/usr/bin/python
help = """Retrieve all the stats from WakaTime API
Usage:
wakatime_stats.py <token>
Options:
-h --help Show this very help message
See https://wakatime.com/developers
"""
import os
import json
import requests
from docopt import docopt
from base64 import b64encode
args = docopt(help)
token = args['<token>']
b64_token = b64encode(token)
base_api = 'https://wakatime.com/api/v1'
headers = {'Authorization': 'Basic %s' % b64_token}
DAYS = ['2017-04-28', '2017-05-02', '2017-05-03', '2017-05-04', '2017-05-05']
def request(url):
r = requests.get('%s%s' % (base_api, url), headers=headers)
json = r.json()
try:
return json['data']
except KeyError:
print('An error occured')
print(json)
return None
def write_file(filename, data):
with open(filename, 'w') as f:
f.write(json.dumps(data))
def retrieve_and_save(name, url, multiple_days=False):
if multiple_days:
if not os.path.exists(name):
os.makedirs(name)
for day in DAYS:
data = request('%s?date=%s' % (url, day))
write_file('%s/%s.json' % (name, day), data)
else:
data = request(url)
write_file('%s.json' % name, data)
retrieve_and_save('user', '/users/current')
retrieve_and_save('projects', '/users/current/projects')
retrieve_and_save('stats', '/users/current/stats/last_7_days')
retrieve_and_save('summaries', '/users/current/summaries?start=2017-04-28&end=2017-05-05')
retrieve_and_save('user_agents', '/users/current/user_agents')
retrieve_and_save('hearthbeats', '/users/current/heartbeats', True)
retrieve_and_save('durations', '/users/current/durations', True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment