Skip to content

Instantly share code, notes, and snippets.

@blha303
Last active June 25, 2021 04:41
Show Gist options
  • Save blha303/ba6e6117bf8123fbbc78 to your computer and use it in GitHub Desktop.
Save blha303/ba6e6117bf8123fbbc78 to your computer and use it in GitHub Desktop.
Gets random comment from /r/gonewild, since they're pretty much all compliments. http://compliment.b303.me Warning: May contain sexual content
#!/usr/bin/env python3
import requests
from flask import *
import random
from apscheduler.schedulers.background import BackgroundScheduler
app = Flask(__name__)
scheduler = BackgroundScheduler()
url = "https://reddit.com/r/gonewild/comments.json?limit=200"
comments = []
def request_wants_json():
best = request.accept_mimetypes \
.best_match(['application/json', 'text/html'])
return best == 'application/json' and \
request.accept_mimetypes[best] > \
request.accept_mimetypes['text/html']
def update_comments():
global comments
data = requests.get(url, headers={"User-Agent": "/u/suudo http://compliment.b303.me (comments from gonewild)"}).json()
comments = [a["data"]["body"] for a in data["data"]["children"]]
@app.route("/")
def compliment():
if request_wants_json():
return jsonify({"compliment": random.choice(comments)})
return random.choice(comments)
@app.route("/comments")
def all():
if request_wants_json():
return jsonify({"comments": comments})
return "<ul>\n" + "\n".join("<li>{}</li>\n".format(c) for c in comments) + "</ul>"
update_comments()
scheduler.add_job(update_comments, 'interval', minutes=15)
scheduler.start()
if __name__ == "__main__":
try:
app.run(port=56735, debug=True)
finally:
scheduler.shutdown()

Uses

Text to speech (OSX)

while true; do msg=$(curl -sk http://compliment.b303.me); echo "$msg"; say "$msg" -v "Samantha"; done

Text to speech (Linux)

while true; do msg=$(curl -sk http://compliment.b303.me); echo "$msg" | espeak; echo "$msg"; done

Text to speech (Windows) (untested)

Uses ptts and curl

:top
curl http://compliment.b303.me -O compliment.txt
SET /p MSG=<compliment.txt
echo %MSG%
echo %MSG% | ptts
goto top
@tankorsmash
Copy link

Traceback (most recent call last):
  File "/local_path/temp.py", line 39, in <module>
    scheduler.add_job(update_comments, 'interval', minutes=15)
  File "/usr/local/lib/python2.7/dist-packages/apscheduler/schedulers/base.py", line 330, in add_job
    'trigger': self._create_trigger(trigger, trigger_args),
  File "/usr/local/lib/python2.7/dist-packages/apscheduler/schedulers/base.py", line 782, in _create_trigger
    return self._create_plugin_instance('trigger', trigger, trigger_args)
  File "/usr/local/lib/python2.7/dist-packages/apscheduler/schedulers/base.py", line 766, in _create_plugin_instance
    raise LookupError('No {0} by the name "{1}" was found'.format(type_, alias))
LookupError: No trigger by the name "interval" was found

On python2, upgraded Flask and apscheduler

@blha303
Copy link
Author

blha303 commented Nov 28, 2015

#!/usr/bin/env python3

python3 only pls

@intxcc
Copy link

intxcc commented May 9, 2016

You should keep your server certificate up-to-date with the (sub-)domains. https://compliment.b303.me/ is giving back "[...]COMMON_NAME_INVALID"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment