This lambda function is used to remove old comments from your own reddit account.
Be aware that removing your comments from reddit does not guarantee you will keep being anonymous on the platform - that can only be done by being careful about what you write in the comments.
People with time and (some) resources will still be able to access the comments anyway: multiple torrents are available with data dumps of the entire website, and hosting it on some hosted solution for doing queries is (relatively) cheap.
TL;DR: using this will not guarantee you keep being an happy anon on reddit. Sorry.
To use this code, you'll need a reddit application. To create it, follow this guide.
Please take note of the application id (what we will call app_id
around here,
usually shown below the type of the application in the reddit application page)
and the application secret (app_secret
). We will use both in the next step.
A refresh token is necessary to allow the lambda function to retrieve data from the reddit API. To get a refresh token you need to follow the Oauth2 authentication flow - I created the utils/get_token.py script to simplify the work.
To use it, copy-paste the following in a terminal while being in the source directory of this repository (you'll need to have virtualenv and python2 installed for this to work):
virtualenv .env
source .env/bin/activate
pip install flask requests
python2 utils/get_token.py -I "<your app id>" -S "<your app secret>"
This should in the end start the server and provide the following output:
* Running on http://127.0.0.1:6501/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: XXX-XXX-XXX
Click on the URL shown in the first line, and follow the link. It should bring you to a page where (if you are logged onto reddit) you will be asked to allow access to your account by the application you created above.
Then you'll get redirected to a page that should show a json with a
refresh_token
field. Note it down, it will be needed in the next step.
If you get a "Too many requests" error keep refreshing, it will succeed after a couple of attempts.
For your convenience the same data is also printed in the console from where you started the application. You can now kill (with a CTRL+C) the python script we spawned.
{
"app_id": "<APPLICATION ID>",
"app_secret": "<APPLICATION SECRET>",
"refresh_token": "<CURRENT REFRESH TOKEN>",
"days": 90
}
Before packaging your function you need to create a config.json
file to store:
- your
app_id
andapp_secret
. Those are generated by reddit when you created a new application. - the
refresh_token
you got in the previous step. - the number of
days
after which you want your comments to be deleted. This is your choice, but keep in mind the function will be only able to access the last 1000 comments (this is a reddit API limitation).
You can copy config-sample.json and use it as a starting point for your own configuration file.
It's easy. Just run:
make
This will download all the dependencies and generate a kimsufi-monitor.zip
file you can use in your lambda function.
There are at least a couple of ways to deploy an AWS lambda function, but for
this kind of small upload-once-and-forget-about-it functions I like the CLI
more. Obliviously the following commands assume you already installed and
configured aws-cli
on your current host (see the
documentation if it is not) and you have some familiarity with
AWS terminology.
Let's then create our new function!
aws lambda create-function \
--function-name RedditShredder \
--runtime python2.7 \
--memory 128 \
--timeout 10 \
--handler main.handle_lambda \
--role "<your lambda execution role ARN>" \
--zip-file fileb://./reddit-shredder.zip \
--description "Reddit Comment Shredder" --output json
This should return to you something like (depending in what version you are downloading from this repository):
{
"Description": "Reddit Comment Shredder",
"Runtime": "python2.7",
"CodeSize": 950867,
"FunctionName": "RedditShredder",
"CodeSha256": "b/gcaDsaL5ly13pMRs301IG8BMdWGTUBcHfbwC/tsiQ=",
"Handler": "main.handle_lambda",
"Timeout": 10,
"Role": "arn:aws:iam::0000000:role/lambda_reddit_shredder",
"FunctionArn": "arn:aws:lambda:us-east-1:0000000:function:RedditShredder",
"LastModified": "2016-08-02T21:25:23.417+0000",
"MemorySize": 128,
"Version": "$LATEST"
}
This is unfortunately not enough. We have a lambda function yes, but can only execute it manually right now, and instead we want to schedule it. To do that, we need to leverage CloudWatch events, like below.
aws events put-rule \
--name "1HourRule" \
--schedule-expression "rate(1 hour)" --output json
aws events put-targets \
--rule "1HourRule"
--targets "Id=rs,Arn=arn:aws:lambda:us-east-1:0000000:function:RedditShredder"
For the second command, in the targets
parameter, the Arn
to use is the
FunctionArn
we got when creating the lambda function, while the Id
is
simply a unique string.
And we are all set!
Now you have less comments in your reddit account. Is that happiness?