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 053037693b200aa6712e685928819d8fce78adf6

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
31update_comments()
32scheduler.add_job(update_comments, 'interval', minutes=15)
33scheduler.start()
34if __name__ == "__main__":
35 try:
36 app.run(port=56735, debug=True)
37 finally:
38 scheduler.shutdown()
39