-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit_python_news.py
86 lines (64 loc) · 2.04 KB
/
reddit_python_news.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# =========================================================
# Author:
# Rodolfo Ferro Pérez
# ferro(at)cimat(dot)mx
#
# Alexa skill to gather Python news from Reddit
# =========================================================
from flask_ask import Ask, statement, question, session
from pprint import pprint
from flask import Flask
import unidecode
import requests
import logging
import json
import time
# Set global variables
name = 'Rodolfo'
log = logging.getLogger()
log.addHandler(logging.StreamHandler())
log.setLevel(logging.DEBUG)
logging.getLogger("flask_ask").setLevel(logging.DEBUG)
# Create Flask, Ask apps
app = Flask(__name__) # Standard Flask app
ask = Ask(app, "/reddit_python") # App endpoint
# App route
@app.route("/")
def homepage():
return "Hey there {}! Flask is running with no problems!".format(name)
# Alexa initial message (starting app...)
@ask.launch
def start_skill():
welcome_msg = "Hi! Would you like to hear any Python toppics on Redit?"
return question(welcome_msg)
# If answer is yes
@ask.intent("YesIntent")
def share_headlines():
pyheadlines = reddit_python_headlines()
msg = "The current Python headlines on Redit are: {}.".format(pyheadlines)
return statement(msg)
# If answer is no
@ask.intent("NoIntent")
def no_intent():
bye_text = "Okay, good bye."
return statement(bye_text)
# End session
@ask.session_ended
def session_ended():
log.debug("Session ended.")
return "", 200
# Consume Reddit API
def reddit_python_headlines():
# Consume Reddit's API to gather info
url = "https://www.reddit.com/r/Python/.json?limit=10"
headers = {'User-Agent': 'Python Reddit headlines with Alexa'}
html = requests.get(url, headers=headers)
print(html.status_code)
info = json.loads(html.content.decode('utf-8'))
# pprint(info)
child = info['data']['children']
titles = [unidecode.unidecode(elem['data']['title']) for elem in child]
titles = "... ".join([title for title in titles])
return titles
if __name__ == "__main__":
app.run(debug=True)