-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
44 lines (35 loc) · 1.25 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import json
import os
import praw
from typing import Generator
from random import randrange
env = {
'username': os.environ["REDDITNAME"],
'password': os.environ.get("PASSWORD",""), # for testing purposes, password is blank...
'id': os.environ["APPID"],
'secret': os.environ["APPSECRET"],
'app-name': os.environ["APPNAME"],
'limit': os.environ["LIMIT"],
}
reddit = praw.Reddit(client_id=env['id'],
client_secret=env['secret'],
user_agent=f"{env['app-name']} by {env['username']}",
username=env['username'],
password=env['password'],
)
def wholesomify(limit: int) -> Generator:
# assume you have a Reddit instance bound to variable `reddit`
choice = ["wholesomememes", "aww", "EyeBleach"]
subreddit = reddit.subreddit(choice[randrange(len(choice))])
submissions = []
# populate
for submission in subreddit.hot(limit=limit):
submissions.append(submission)
return submissions[randrange(limit)]
def lambda_handler(event, context):
submission = wholesomify(int(env['limit']))
message = f"wholesome time!: {submission.url}"
return {
'statusCode': 200,
'body': json.dumps(message)
}