-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_random_meme.py
33 lines (28 loc) · 1.07 KB
/
get_random_meme.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
import praw
from prawcore import NotFound
import random
# 0 error code - no memes found
# Sorry error code - subreddit doesn't exist
def get_meme(subs=['memes', 'dankmemes']):
def sub_exists(sub):
exists = True
try:
reddit.subreddits.search_by_name(sub, exact=True)
except NotFound:
exists = False
return exists
reddit = praw.Reddit(client_id="CLIENT_ID",
client_secret="CLIENT_SECRET",
password="PASSWORD",
user_agent="USER_AGENT",
username="USERNAME")
meme_captions_urls = []
for sub in subs:
if not sub_exists(sub):
return "Sorry "+sub+" subreddit doesn't exist."
for post in reddit.subreddit(sub).hot(limit=(50//len(subs))+10):
if (post.url).endswith(('.jpg', '.png', '.jpeg')):
meme_captions_urls.append([post.url, post.title])
return random.choice(meme_captions_urls) if len(meme_captions_urls) > 0 else 0
if __name__ == "__main__":
print(get_meme())