forked from D3vd/Meme_Api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
198 lines (146 loc) · 4.14 KB
/
app.py
File metadata and controls
198 lines (146 loc) · 4.14 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from flask import Flask, render_template, jsonify
from flask_cors import CORS, cross_origin
import os, json
from reddit_handler import *
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
meme_subreddits = ['memes', 'dankmemes', 'meirl']
@app.route('/')
def index():
return render_template('index.html')
@app.route('/openapi.json')
def openapi():
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
json_url = os.path.join(SITE_ROOT, "static", "openapi.json")
data = json.load(open(json_url))
return data
@app.route('/gimme')
@cross_origin()
def one_post():
sub = random.choice(meme_subreddits)
try:
re = get_posts(sub, 100)
except ResponseException:
return jsonify({
'status_code': 500,
'message': 'Internal Server Error'
})
r = random.choice(re)
while not is_img_link(r["url"]):
r = random.choice(re)
return jsonify({
'title': r["title"],
'url': r["url"],
'postLink': r["link"],
'subreddit': sub
})
@app.route('/gimme/<int:count>')
@cross_origin()
def multiple_posts(count):
if count > 100:
return jsonify({
'status_code': 400,
'message': 'Please ensure the count is less than 100'
})
sub = random.choice(meme_subreddits)
try:
re = get_posts(sub, 100)
except ResponseException:
return jsonify({
'status_code': 500,
'message': 'Internal Server Error'
})
random.shuffle(re)
memes = []
for post in re:
if len(memes) == count:
break
if is_img_link(post['url']):
temp = {
'title': post["title"],
'url': post["url"],
'postLink': post["link"],
'subreddit': sub
}
memes.append(temp)
return jsonify({
'memes': memes,
'count': len(memes)
})
@app.route('/gimme/<subreddit>')
@cross_origin()
def one_post_from_sub(subreddit):
try:
re = get_posts(subreddit, 100)
except Redirect:
return jsonify({
'status_code': 404,
'message': 'Invalid Subreddit'
})
except ResponseException:
return jsonify({
'status_code': 500,
'message': 'Internal Server Error'
})
r = random.choice(re)
while not is_img_link(r["url"]):
r = random.choice(re)
return jsonify({
'title': r["title"],
'url': r["url"],
'postLink': r["link"],
'subreddit': subreddit
})
@app.route('/gimme/<subreddit>/<int:count>')
@cross_origin()
def multiple_posts_from_sub(subreddit, count):
if count > 100:
return jsonify({
'status_code': 400,
'message': 'Please ensure the count is less than 100'
})
try:
re = get_posts(subreddit, 100)
except Redirect:
return jsonify({
'status_code': 404,
'message': 'Invalid Subreddit'
})
except ResponseException:
return jsonify({
'status_code': 500,
'message': 'Internal Server Error'
})
random.shuffle(re)
memes = []
for post in re:
if len(memes) == count:
break
if is_img_link(post['url']):
temp = {
'title': post["title"],
'url': post["url"],
'postLink': post["link"]
}
memes.append(temp)
return jsonify({
'memes': memes,
'count': len(memes),
'subreddit': subreddit
})
@app.route('/sample')
def sample():
re = get_posts(random.choice(meme_subreddits), 100)
r = random.choice(re)
while not is_img_link(r["url"]):
r = random.choice(re)
return render_template('sample.html', title=r["title"], img_url=r["url"], shortlink=r["link"])
@app.route('/test')
def test():
re = get_posts(random.choice(meme_subreddits), 100)
return render_template('test.html', re=re)
@app.errorhandler(404)
@app.route('/<something>')
def not_found(something):
return render_template('not_found.html')