-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
62 lines (43 loc) · 1.32 KB
/
app.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
from flask import *
import requests
import random
app = Flask(__name__)
@app.route('/')
def root():
subreddit_list = [
{
"name": 'memes'
},
{
"name": 'wholesomememes'
},
{
"name": 'AdviceAnimals'
},
{
"name": 'dankmemes'
}
]
subreddit = random.choice(subreddit_list)
subreddit = str({subreddit['name']})[1:-1]
subreddit = subreddit.replace("'", '')
url = f"https://meme-api.com/gimme/{subreddit}/1/"
meme_api_response = requests.get(url)
response_json = meme_api_response.json()
check_nsfw_meme = response_json["memes"][0]["nsfw"]
meme_url = response_json["memes"][0]["preview"][3]
title = response_json["memes"][0]["title"]
author = response_json["memes"][0]["author"]
subreddit = response_json["memes"][0]["subreddit"]
reddit_url = response_json["memes"][0]["postLink"]
if check_nsfw_meme == "true":
return redirect('/' , code=302)
else:
return render_template('index.html', meme_url=meme_url , title=title, author=author, subreddit=subreddit, reddit_url=reddit_url)
@app.errorhandler(404)
def not_found(error):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_error(error):
return redirect('/' ,code=302)
app.run(host='0.0.0.0', port=5000 )