-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
115 lines (100 loc) · 3.92 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os, glob
from sys import platform
from flask import Flask, render_template, send_file, request
from flask_paginate import Pagination, get_page_parameter
from whitenoise import WhiteNoise
from readabilipy import simple_json_from_html_string
from config import *
app = Flask(__name__)
app.wsgi_app = WhiteNoise(app.wsgi_app)
# Note: It could be possible to add more than one directory,
# but you have to adjust the code accordingly in the get_filenames function
my_static_folders = (
SAVED_WEBPAGES_DIR,
# "static/folder/two/",
# "static/folder/three/",
)
for static in my_static_folders:
app.wsgi_app.add_files(static)
'''
Renders the index of the site
'''
@app.route("/")
def index():
file_list = get_filenames()
files = [ file.split('/')[-1] for file in file_list]
page = request.args.get(get_page_parameter(), type=int, default=1)
pagination, page_files = get_pagination(page, files)
return render_template('index.html', files=page_files, pagination=pagination)
'''
Return the pagination object and the list of the filenames of the current page
'''
def get_pagination(page, files):
per_page = PER_PAGE
pagination = Pagination(page=page, total=len(files), search=False, record_name='files', per_page=per_page)
page_files = files[(page-1) * per_page : ((page-1) * per_page)+per_page]
return (pagination, page_files)
'''
Send the file without processing to the browser
'''
@app.route('/file/<filename>')
def file(filename):
file = SAVED_WEBPAGES_DIR + filename
return send_file(file)
'''
Displays the file in a more friendly way to read
It uses Mozilla Readability library if possible
'''
@app.route('/read/<filename>')
def read(filename):
if filename.split('.')[-1].lower() in READ_MODE_FILES_COMPATIBLE:
file = SAVED_WEBPAGES_DIR + filename
article = {}
with open(file, 'r') as document:
text = document.read()
article = simple_json_from_html_string(text, use_readability=USE_READABILITY)
title = article.get('title') if article.get('title') else filename.split('.')[-2]
content = txt2html(text) if filename.split('.')[-1].lower() != 'html' else article.get('content')
return render_template('read.html', content=content, title=title)
return "<p style='font-size:2em;'>File not compatible with Read Mode</p>"
'''
Filter the files by filename searching one o more words
'''
@app.route('/search', methods=['GET'])
def search():
search_words = request.args.get('query', '')
search_words = str(search_words).lower().split(' ')
file_list = get_filenames()
filtered_list = [s for s in file_list if all(xs in s.lower() for xs in search_words)]
files = [ file.split('/')[-1] for file in filtered_list]
page = request.args.get(get_page_parameter(), type=int, default=1)
pagination, page_files = get_pagination(page, files)
return render_template('search.html',
files=page_files,
pagination=pagination,
query=request.args.get('query', '')\
)
'''
Return the filenames in the order selected in the config file
'''
def get_filenames():
file_list = []
for type in FILE_TYPES_ALLOWED:
file_list.extend(glob.glob(SAVED_WEBPAGES_DIR + '*.' + type))
if platform == 'linux' or platform == 'linux2':
file_list.extend(glob.glob(SAVED_WEBPAGES_DIR + '*.' + type.upper()))
if SORT_BY == 'CREATION':
file_list.sort(key=os.path.getctime, reverse=REVERSE_ORDER)
elif SORT_BY == 'MODIFIED':
file_list.sort(key=os.path.getmtime, reverse=REVERSE_ORDER)
else:
file_list.sort(reverse=REVERSE_ORDER)
return file_list
# It actually do not convert to html
# but adding <p> tags is enough to display text correctly in the Kindle
def txt2html(text):
paragraphs = text.split('\n')
output = ''
for para in paragraphs:
output += '<p>' + para + '</p>'
return output