Last active 1624560119

Gets random comment from /r/gonewild, since they're pretty much all compliments. http://compliment.b303.me Warning: May contain sexual content

Revision d830ad41e64d1996342b54f807987665dff258e2

compliment.b303.me.py Raw
1#!/usr/bin/env python3
2import requests
3from flask import *
4import random
5
6from apscheduler.schedulers.background import BackgroundScheduler
7
8app = Flask(__name__)
9scheduler = BackgroundScheduler()
10url = "https://reddit.com/r/gonewild/comments.json?limit=200"
11comments = []
12
13def request_wants_json():
14 best = request.accept_mimetypes \
15 .best_match(['application/json', 'text/html'])
16 return best == 'application/json' and \
17 request.accept_mimetypes[best] > \
18 request.accept_mimetypes['text/html']
19
20def update_comments():
21 global comments
22 data = requests.get(url, headers={"User-Agent": "/u/suudo http://compliment.b303.me (comments from gonewild)"}).json()
23 comments = [a["data"]["body"] for a in data["data"]["children"]]
24
25@app.route("/")
26def compliment():
27 if request_wants_json():
28 return jsonify({"compliment": random.choice(comments)})
29 return random.choice(comments)
30
31@app.route("/comments")
32def all():
33 if request_wants_json():
34 return jsonify({"comments": comments})
35 return "<ul>\n" + "\n".join("<li>{}</li>\n".format(c) for c in comments) + "</ul>"
36
37if __name__ == "__main__":
38 update_comments()
39 scheduler.add_job(update_comments, 'interval', minutes=15)
40 scheduler.start()
41 try:
42 app.run(port=56735, debug=True)
43 finally:
44 scheduler.shutdown()
45