I love NBA 2K MyTeam. MyTeam is a game mode in the NBA 2K series that allows you to build your own team, consisting of former and current NBA players. You have the ability to customize your team’s franchise and appearance. MyTeam has several different gamemodes inside of it.
Sometimes, they relase a locker code. Locker code is text code that you enter into the game that most often rewards you with a free player or pack in NBA 2K20 MyTeam.
Luckily, there is a website that always update new locker code so we don’t need to wait someone to share locker code on twitter or other media.
With this easiness, I still too lazy to check their website everyday or every couple hours. Luckily again, they use naked API as their datasource. So I plan to develop an automation that can check every hour and send me the fresh locker code.
Because I am so lazy, I will use AWS Lambda and AWS Cloudwatch Events.
AWS Lambda lets us run code without provisioning or managing servers. With that we only need to care about our code. And AWS Cloudwatch Events is our cronjob.
I will use python, you can choose other language option if you want.
We will just using the lambda web console since it will not take much code to do. Lambda function with python template is like this
We will cloudwatch event as out trigger with no input paramater, so we will not use the event param.
First step, I will call the locker code datasource API that return json body. We need requests library. But since I code in web console, I can not add external package. So let use requests from botocore
I already get the data in json dict. I wanna get all the active locker code, so I need to filter out the expired keys. I also need to sync the timezone before comparing datetime.
OK now I have concanated message, what next? I wanna get update in my phone so I will use telegram bot.
Create telegram bot is very easy, I just need to chat @BotFather. Type /newbot
and follow the instruction. When it is done, I got token to access the HTTP API.
Back to my lambda console, I wanna use the environment variables to store the token and my telegram id.
This is my final code
And my last step is adding cloudwatch event as a trigger.
All done. This is the result.