-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
92 lines (72 loc) · 2.86 KB
/
main.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
87
88
89
90
91
92
import os
from flask import Flask, render_template, request, jsonify, abort, make_response
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from rss_feed import get_sorted_posts, get_posts, get_single_blog, get_subscriptions, nest_subs, get_all_entries
# set up base directory
basedir = os.path.abspath(os.path.dirname(__file__))
'''----------APP SETUP-------------'''
app = Flask(__name__)
bootstrap = Bootstrap(app)
# init flask moment to add simple date/time to templates
moment = Moment(app)
@app.route('/')
def index():
page_title = 'Today\'s Posts from all feeds'
posts = get_posts()
my_posts = get_sorted_posts(posts)
return render_template('index.html', my_posts=my_posts, page_title=page_title)
@app.route('/all_posts')
def all_posts():
'''Renders the feed sent from the /subscriptions page'''
subs = request.args.get('sub')
the_posts, sub_title = get_single_blog(subs)
page_title = 'All posts from: %s' % sub_title
return render_template('all_posts.html', the_posts=the_posts, page_title=page_title)
@app.route('/subscriptions')
def subscriptions():
''' Grabs the list of subscriptions and renders them to this page'''
subs = get_subscriptions()
page_title = 'All Subscriptions'
return render_template('subs.html', subs=subs, page_title=page_title)
@app.route('/api')
def api():
''' Grabs the list of subscriptions and renders them to this page'''
subs = nest_subs(get_subscriptions())
print(subs)
page_title = 'API Endpoints'
return render_template('api.html', subs=subs, page_title=page_title)
@app.route('/api/v1.0/subscriptions', methods=['GET'])
def get_subs():
subs = nest_subs(get_subscriptions())
return jsonify({'subscriptions': subs})
@app.route('/api/v1.0/subscriptions/all', methods=['GET'])
def get_all_subs():
subs = get_all_entries()
return jsonify({'subscriptions': subs})
@app.route('/api/v1.0/subscriptions/<int:sub_id>', methods=['GET'])
def get_sub(sub_id):
subs = nest_subs(get_subscriptions())
sub = [sub for sub in subs if sub['id'] == sub_id]
if len(sub) == 0:
abort(400)
return jsonify({'subscription': sub[0]})
@app.route('/api/v1.0/subscriptions/<int:sub_id>/entries', methods=['GET'])
def get_sub_entries(sub_id):
subs = nest_subs(get_subscriptions())
sub = [sub for sub in subs if sub['id'] == sub_id]
if len(sub) == 0:
abort(400)
sub_entries, sub_titles = get_single_blog(sub[0]['url'])
return jsonify({'subscription': sub[0]},{'entries': sub_entries})
@app.errorhandler(400)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 400)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500
if __name__ == '__main__':
app.run(host='0.0.0.0')