-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmemeGenerator.py
111 lines (102 loc) · 4 KB
/
memeGenerator.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import praw
from random import choice
import os
reddit = praw.Reddit(client_id=os.getenv("CLIENT_ID"),
client_secret=os.getenv("CLIENT_SECRET"),
user_agent=os.getenv("USER_AGENT")
)
TOPICS = [ # Default Topics When No Topic is given!!
"dankmemes",
"memes",
"AdviceAnimals",
"MemeEconomy",
"me_irl",
"ComedyCemetery",
"terriblefacebookmemes"
]
def ScrapMemes(topic=0, num=1):
'''
topic: The Name Of Subreddit to search for memes.. If default will choose random topic from the TOPICS list
Each subreddit has five different ways of organizing the topics created by redditors: .hot, .new, .controversial, .top, and .gilded. You can also use .search("SEARCH_KEYWORDS") to get only results matching an engine search.
'''
try:
if num <= 0:
return {
"code": 400,
"message": "Enter a positive Number less than 200"
}
result = {}
num = num % 200
if topic == 0:
topic = choice(TOPICS)
if num == 1:
subreddit = reddit.subreddit(topic)
meme = subreddit.random()
try:
_ = meme.preview
result = {
"code":200,
"post_link": meme.shortlink,
"subreddit": topic,
"title": meme.title,
"url": meme.url,
"ups": meme.ups,
"author": meme.author.name,
"spoilers_enabled": subreddit.spoilers_enabled,
"nsfw": subreddit.over18,
"image_previews": [i["url"] for i in meme.preview.get("images")[0].get("resolutions")]
}
except Exception as e:
result = {
"post_link": meme.shortlink,
"subreddit": topic,
"title": meme.title,
"url": meme.url,
"ups": meme.ups,
"author": meme.author.name,
"spoilers_enabled": subreddit.spoilers_enabled,
"nsfw": subreddit.over18,
"image_previews": ["No Preview Found For This Meme.. Sorry For That"]
}
else:
subreddit = reddit.subreddit(topic)
submissions = subreddit.random_rising(limit=num)
result = {
"code": 200,
"count": num,
"memes": []
}
for meme in submissions:
try:
_ = meme.preview
item = {
"post_link": meme.shortlink,
"subreddit": topic,
"title": meme.title,
"url": meme.url,
"ups": meme.ups,
"author": meme.author.name,
"spoilers_enabled": subreddit.spoilers_enabled,
"nsfw": subreddit.over18,
"image_previews": [i["url"] for i in meme.preview.get("images")[0].get("resolutions")]
}
except Exception as e:
item = {
"post_link": meme.shortlink,
"subreddit": topic,
"title": meme.title,
"url": meme.url,
"ups": meme.ups,
"author": meme.author.name,
"spoilers_enabled": subreddit.spoilers_enabled,
"nsfw": subreddit.over18,
"image_previews": ["No Preview Found For This Meme.. Sorry For That"]
}
result.get("memes").append(item)
return result
except Exception as e:
return {
"code": 400,
"message": str(type(e))+str(e),
"help": "Subreddit Doesn't Exist, Check if u spelled it correctly.."
}