This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.py
63 lines (53 loc) · 1.84 KB
/
server.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
from flask import Flask, render_template, jsonify, request, send_from_directory
from random import *
from threading import Thread
from flask_cors import CORS
from wattpad2epub import get_book
import time
import os
import platform
def creation_date(path_to_file):
"""
Try to get the date that a file was created, falling back to when it was
last modified if that isn't possible.
See http://stackoverflow.com/a/39501288/1709587 for explanation.
"""
if platform.system() == 'Windows':
return os.path.getctime(path_to_file)
else:
stat = os.stat(path_to_file)
try:
return stat.st_birthtime
except AttributeError:
# We're probably on Linux. No easy way to get creation dates here,
# so we'll settle for when its content was last modified.
return stat.st_mtime
def cleanupFiles():
while (1 == 1):
current_time = time.time()
for f in os.listdir("./books"):
creation_time = os.path.getctime(os.path.join("./books", f))
if (current_time - 7 * 24 * 60 * 60 > creation_time):
os.unlink(os.path.join("./books", f))
print('{} removed'.format(f))
time.sleep(10)
app = Flask(__name__,
static_folder = "./dist/",
template_folder= "./dist/")
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
@app.route('/cdn/<path:filename>')
def custom_static(filename):
return send_from_directory('./', filename)
@app.route('/<path:filename>')
def stat(filename):
return send_from_directory('./dist', filename)
@app.route('/api', methods=["POST"])
def random_number():
return get_book(request.get_json()["url"])
@app.route('/')
def catch_all():
return render_template("index.html")
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
cleanup = Thread(target=cleanupFiles)
cleanup.start()