compliment.b303.me.py
                        
                             · 1.3 KiB · Python
                        
                    
                    
                      
                        Raw
                      
                      
                        
                          
                        
                    
                    
                
                
            #!/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()
                | 1 | #!/usr/bin/env python3 | 
| 2 | import requests | 
| 3 | from flask import * | 
| 4 | import random | 
| 5 | |
| 6 | from apscheduler.schedulers.background import BackgroundScheduler | 
| 7 | |
| 8 | app = Flask(__name__) | 
| 9 | scheduler = BackgroundScheduler() | 
| 10 | url = "https://reddit.com/r/gonewild/comments.json?limit=200" | 
| 11 | comments = [] | 
| 12 | |
| 13 | def 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 | |
| 20 | def 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("/") | 
| 26 | def compliment(): | 
| 27 | if request_wants_json(): | 
| 28 | return jsonify({"compliment": random.choice(comments)}) | 
| 29 | return random.choice(comments) | 
| 30 | |
| 31 | @app.route("/comments") | 
| 32 | def 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 | |
| 37 | update_comments() | 
| 38 | scheduler.add_job(update_comments, 'interval', minutes=15) | 
| 39 | scheduler.start() | 
| 40 | if __name__ == "__main__": | 
| 41 | try: | 
| 42 | app.run(port=56735, debug=True) | 
| 43 | finally: | 
| 44 | scheduler.shutdown() | 
| 45 | 
                    
                        
                        fun-uses.md
                        
                             · 626 B · Markdown
                        
                    
                    
                      
                        Raw
                      
                      
                        
                          
                        
                    
                    
                
                
            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](http://jampal.sourceforge.net/ptts.html) and [wget](http://nebm.ist.utl.pt/~glopes/wget/)
    :top
    curl http://compliment.b303.me -O compliment.txt
    SET /p MSG=<compliment.txt
    echo %MSG%
    echo %MSG% | ptts
    goto top
                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)
:top
curl http://compliment.b303.me -O compliment.txt
SET /p MSG=<compliment.txt
echo %MSG%
echo %MSG% | ptts
goto top