Giter Site home page Giter Site logo

reddit-shredder-lambda's Introduction

REDDIT SHREDDER LAMBDA

This lambda function is used to remove old comments from your own reddit account.

Notes

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.

Create your reddit application

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.

Getting a refresh token

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.

Configuration file

{
  "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 and app_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.

Packaging

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.

Deploying

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.