This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
beatporttagger.py
128 lines (111 loc) · 3.21 KB
/
beatporttagger.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
116
117
118
119
120
121
122
123
124
125
126
127
128
import webview
import os
import sys
import tagger
import math
import sys
import json
import threading
import logging
from flask import Flask, send_from_directory, request, send_file
#Disable Flask logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.WARN)
progress = {}
_tagger = None
#Get path to assets dir, for pyinstaller
def assets_path():
path = os.getcwd()
try:
path = getattr(sys, '_MEIPASS')
except Exception:
pass
return os.path.join(path, 'assets')
#Flask setup
app = Flask(__name__, static_url_path='', static_folder=assets_path())
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
@app.route('/')
def index():
return send_from_directory(assets_path(), 'index.html')
@app.route('/failed')
def send_failed():
if len(_tagger.fail) > 1:
prefix = os.path.commonprefix(_tagger.fail)
failed = [f.replace(prefix, '') for f in _tagger.fail]
return json.dumps(failed)
return json.dumps(_tagger.fail)
#Browse for file
@app.route('/browse')
def browse():
p = window.create_file_dialog(dialog_type=webview.FOLDER_DIALOG, allow_multiple=False)
if p == None:
return ''
return p[0]
#Return text = error
@app.route('/start', methods = ['POST'])
def start():
data = request.get_json(force=True)
#Check path
path = data['path']
if not os.path.isdir(path):
return 'Invalid path!'
#Generate config
config = tagger.TagUpdaterConfig(
update_tags = [tagger.UpdatableTags[t] for t in data['tags']],
replace_art=data['replaceArt'],
art_resolution=data['artResolution'],
artist_separator=data['artistSeparator'],
fuzziness=data['fuzziness'],
overwrite=data['overwrite'],
id3v23=data['id3v23']
)
#Start
thread = threading.Thread(target=start_tagger, args=(config,path))
thread.start()
return ''
@app.route('/progress')
def progress_request():
return json.dumps(progress)
#Generate JSON with progress for UI
def _update_progress(file):
global progress
if _tagger != None and _tagger.total > 0:
#Percentage
p = (len(_tagger.success) + len(_tagger.fail)) / _tagger.total
percent = math.floor(p*100)
progress = {
'percent': percent,
'success': len(_tagger.success),
'failed': len(_tagger.fail)
}
def start_tagger(config, path):
global progress
global _tagger
#Reset progress
progress = {}
_tagger = tagger.TagUpdater(config, success_callback=_update_progress, fail_callback=_update_progress)
_tagger.tag_dir(path)
def start_flask():
cli = sys.modules['flask.cli']
cli.show_server_banner = lambda *x: None
app.run(host='127.0.0.1', port=36958)
if __name__ == '__main__':
#Flask
thread = threading.Thread(target=start_flask)
thread.daemon = True
thread.start()
#Pywebview
window = webview.create_window(
'Beatport Tagger',
'http://localhost:36958/',
resizable=False,
width=400,
height=725,
min_size=(400, 725),
)
if sys.platform == 'win32':
webview.start(debug=True, gui='cef')
else:
webview.start(debug=True)
sys.exit()