[PYTHON] Tweet WakaTime Summary using AWS Lambda

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

Tweet WakaTime Summary using AWS Lambda
Summary if using AWS Lambda (Python)
Tweet from AWS Lambda
Web scraping using AWS lambda
Using Lambda with AWS Amplify with Go
[AWS] Using ini files with Lambda [Python]
Regularly post to Twitter using AWS lambda!
Summary of how to write AWS Lambda
Tweet in Chama Slack Bot ~ How to make a Slack Bot using AWS Lambda ~
Serverless scraping using selenium with [AWS Lambda] -Part 1-
Summary of studying Python to use AWS Lambda
Develop, run, and deploy AWS Lambda remotely using lambda-uploader
Summary when using Fabric
Check types_map when using mimetypes on AWS Lambda (Python)
How to set layer on Lambda using AWS SAM
I tried to get an AMI using AWS Lambda
Example of using lambda
Try AWS Lambda Destinations
Addictive points when downloading files using boto on AWS Lambda
Create API with Python, lambda, API Gateway quickly using AWS SAM
[Python] Scraping in AWS Lambda
The simplest AWS Lambda implementation
Try using AWS SageMaker Studio
AWS Lambda with PyTorch [Lambda import]
I tried using AWS Chalice
I stopped my instance at a specific time using AWS Lambda
I compared Node.js and Python in creating thumbnails using AWS Lambda
Using PhantomJS with AWS Lambda until displaying the html of the website
[Python] I wrote a REST API using AWS API Gateway and Lambda.