forked from 0oVicero0/OneList
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (48 loc) · 1.55 KB
/
app.py
File metadata and controls
65 lines (48 loc) · 1.55 KB
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
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# Author: MoeClub.org, sxyazi
from process import od
from config import config
from utils import path_format
from flask import Flask, abort, redirect, render_template, Blueprint
bp = Blueprint('main', __name__, url_prefix=config.location_path)
# Views
@bp.route('/favicon.ico')
def favicon():
return abort(404)
@bp.route('/', defaults={'path': '/'})
@bp.route('/<path:path>')
def catch_all(path):
info = od.list_items_with_cache(
path_format(config.start_directory + '/' + path))
if info.is_file: # download
return redirect(info.files[0]['download_url'])
return render_template('list.html', info=info, path=path_format(path).strip('/'))
# Filters
@bp.app_template_filter('date_format')
def date_format(str, format='%Y/%m/%d %H:%M:%S'):
from dateutil import tz
from datetime import datetime
dt = datetime.strptime(str, "%Y-%m-%dT%H:%M:%SZ")
return dt.replace(tzinfo=tz.tzutc()).astimezone(tz.gettz('Asia/Shanghai')).strftime(format)
@bp.app_template_filter('file_size')
def file_size(size):
unit = (
('B', 2**0),
('KB', 2**10),
('MB', 2**20),
('GB', 2**30),
('TB', 2**40),
('PB', 2**50),
('EB', 2**60),
('ZB', 2**70),
('YB', 2**80)
)
for k, v in unit:
if size <= v * 1024:
return '%s %s' % (round(size/v, 2), k)
return 'unknown'
app = Flask(__name__)
app.register_blueprint(bp)
if __name__ == '__main__':
app.run(host='127.0.0.1', port='5000', debug=True)