-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·68 lines (53 loc) · 2.05 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
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
import os
from flask import Flask, flash, request, redirect, url_for, jsonify
from flask.templating import render_template, render_template_string
from werkzeug.utils import secure_filename
from book_utils import analyse_ebook
from utils import get_books, get_book
from constants import UPLOAD_FOLDER, ALLOWED_EXTENSIONS
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET'])
def upload_file():
return render_template('upload_file.html')
@app.route('/books', methods=['GET'])
def show_books():
books = get_books()
return render_template('books.html', books=books)
@app.route('/books/<string:hash>', methods=['GET'])
def show_book(hash: str):
book_data = get_book(hash)
# return render_template('books.html', books=[book_data])
return render_template('book.html', book=book_data)
@app.route('/books/<string:hash>/plots', methods=['GET'])
def show_plots(hash: str):
book_data = get_book(hash)
return render_template('plots.html', book=book_data)
@app.route('/api/books/<string:hash>', methods=['GET'])
def return_json(hash: str):
book_data = get_book(hash)
return jsonify(book_data)
@app.route('/upload', methods=['POST'])
def upload_ebook():
if 'file' not in request.files:
flash('No file part')
return redirect('/')
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect('/')
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
save_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(save_path)
book_data = analyse_ebook(save_path)
file_hash = book_data['file_hash']
return redirect(f'/books/{file_hash}')
if __name__ == '__main__':
app.run(debug=True)