A service that measures coding time by inserting a plug-in into each editor WakaTime I've been using it for about a month, but the statistics are interesting. So, since there is also an API, a test to finally tweet all together. Also, AWS Lambda can now do cron-like things, so I also tested it.
The Python code looks like this.
lambda_function.py
# -*- coding: utf-8 -*-
import requests
import twitter
import config
def get_stats():
    headers = {'Authorization': 'Bearer ' + config.wakatime['access_token']}
    return requests.get('https://wakatime.com/api/v1/users/current/stats/last_7_days', headers=headers)
def tweet(status):
    api = twitter.Api(consumer_key=config.twitter['consumer_key'],
                      consumer_secret=config.twitter['consumer_secret'],
                      access_token_key=config.twitter['access_token_key'],
                      access_token_secret=config.twitter['access_token_secret'])
    # print api.VerifyCredentials()
    api.PostUpdate(status)
def make_summary(data):
    total = data['human_readable_total'].replace('hours', 'h').replace('minutes', 'm')
    lang_stats =  map(lambda l: '{} {}%'.format(l['name'], int(round(l['percent']))), data['languages'][0:4])
    return 'Last week\'s coding stats: Total {} ({}) (via @WakaTime https://wakatime.com/@Saqoosha)'.format(total, ' / '.join(lang_stats))
def lambda_handler(event, context):
    result = get_stats()
    print result.text
    summary = make_summary(result.json()['data'])
    tweet(summary)
    return summary
if __name__ == '__main__':
    # import json
    # with open('_exp/data.json') as f:
    #     data = json.load(f)
    #     summary = make_summary(data['data'])
    #     print summary
    lambda_handler(None, None)
The point to note when using AWS Lambda is that you have to put the 3rd party module in the same place as the code above with pip install python-twitter -t . and upload it in ZIP, and the cron format is always It's a little different from that one. Easy. Easy to do without a server.
Recommended Posts