-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
58 lines (43 loc) · 1.35 KB
/
app.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
import os
import glob
from flask import (
Flask,
render_template
)
app = Flask(__name__)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
def list_posts():
posts = glob.glob(f'posts/*.txt')
return [
post_name.split('.')[0] for post_name in [
post.split('/')[1] for post in posts
] if post_name not in ['404.txt', 'index.txt']
]
def get_file_contents(filename, feed=False):
if not feed:
file_path = f'posts/{filename}'
else:
file_path = f'feeds/{filename}'
file_path = os.path.join(BASE_DIR, file_path )
with open(file_path, 'r') as f:
return f.read()
@app.route('/')
def index():
content = get_file_contents('index.txt')
return render_template('post.html', content=content, posts=list_posts())
@app.route('/posts/<post_name>', methods=['GET'])
def post(post_name):
try:
content = get_file_contents(f'{post_name}.txt')
except:
content = get_file_contents('404.txt')
return render_template('post.html', content=content)
### static feeds
CATS_FEED_DATA = get_file_contents('cat-facts.json', feed=True)
@app.route('/feed/cat-facts', methods=['GET'])
def cat_feed():
return CATS_FEED_DATA
BLS_FEED_DATA = get_file_contents('bls.csv', feed=True)
@app.route('/feed/bls', methods=['GET'])
def bls_feed():
return BLS_FEED_DATA