-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathbackend.py
More file actions
137 lines (107 loc) · 3.74 KB
/
Copy pathbackend.py
File metadata and controls
137 lines (107 loc) · 3.74 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import waitress
import io
import os
from flask import Flask, render_template, request, send_file
from flask_caching import Cache
from flask_minify import Minify
from freetar.ug import Search, ug_tab, SongDetail
from freetar.utils import get_version, FreetarError
from freetar.chordpro import song_to_chordpro
cache = Cache(config={'CACHE_TYPE': 'SimpleCache',
"CACHE_DEFAULT_TIMEOUT": 0,
"CACHE_THRESHOLD": 10000})
app = Flask(__name__)
cache.init_app(app)
Minify(app=app, html=True, js=True, cssless=True)
@app.context_processor
def export_variables():
return {
'version': get_version(),
}
@app.route("/")
def index():
return render_template("index.html")
@app.route("/search")
@cache.cached(query_string=True)
def search():
search_term = request.args.get("search_term")
try:
page = int(request.args.get("page", 1))
except ValueError:
return render_template('error.html',
error="Invalid page requested. Not a number.")
search_results = None
if search_term:
search_results = Search(search_term, page)
return render_template("index.html",
search_term=search_term,
title=f"Freetar - Search: {search_term}",
search_results=search_results)
@app.route("/tab/<artist>/<song>")
@cache.cached()
def show_tab(artist: str, song: str):
tab = ug_tab(f"{artist}/{song}")
return render_template("tab.html",
tab=tab,
title=f"{tab.artist_name} - {tab.song_name}")
@app.route("/tab/<tabid>")
@cache.cached()
def show_tab2(tabid: int):
tab = ug_tab(tabid)
return render_template("tab.html",
tab=tab,
title=f"{tab.artist_name} - {tab.song_name}")
@app.route("/download/<artist>/<song>")
def download_tab(artist: str, song: str):
tab = ug_tab(f"{artist}/{song}")
format = request.args.get('format')
return tab_to_dl_file(tab, format)
@app.route("/download/<tabid>")
def download_tab2(tabid: int):
tab = ug_tab(tabid)
format = request.args.get('format')
return tab_to_dl_file(tab, format)
@app.route("/favs")
def show_favs():
return render_template("index.html",
title="Freetar - Favorites",
favs=True)
def tab_to_dl_file(tab: SongDetail, format: str):
if format == 'ug_txt':
ext = 'ug.txt'
content = tab.raw_tab
elif format == 'txt':
ext = 'txt'
content = tab.plain_text()
elif format == 'chordpro':
ext = 'cho'
content = song_to_chordpro(tab)
else:
return f'no such format: {format}', 400
filename = f'{tab.artist_name} - {tab.song_name}.{ext}'
data = io.BytesIO(content.encode('utf-8'))
return send_file(data, as_attachment=True, download_name=filename)
@app.route("/about")
def show_about():
return render_template('about.html')
@app.errorhandler(403)
@app.errorhandler(500)
@app.errorhandler(FreetarError)
def internal_error(error):
search_term = request.args.get("search_term")
return render_template('error.html',
search_term=search_term,
error=error)
def main():
host = os.getenv('FREETAR_HOST', '0.0.0.0')
port = os.getenv('FREETAR_PORT', 22000)
if __name__ == '__main__':
app.run(debug=True,
host=host,
port=port)
else:
threads = os.environ.get("THREADS", "4")
print(f"Running freetar {get_version()} backend on {host}:{port} with {threads} threads")
waitress.serve(app, listen=f"{host}:{port}", threads=threads)
if __name__ == '__main__':
main()