move song previews to the database

This commit is contained in:
Bui 2019-02-20 22:02:44 +00:00
parent 13ca5e368b
commit 3a28d967b9

82
app.py
View File

@ -40,10 +40,10 @@ def get_config():
try: try:
config = json.load(open('config.json', 'r')) config = json.load(open('config.json', 'r'))
except ValueError: except ValueError:
print 'WARNING: Invalid config.json, using default values' print('WARNING: Invalid config.json, using default values')
config = {} config = {}
else: else:
print 'WARNING: No config.json found, using default values' print('WARNING: No config.json found, using default values')
config = {} config = {}
if not config.get('songs_baseurl'): if not config.get('songs_baseurl'):
@ -55,77 +55,6 @@ def get_config():
return config return config
def parse_osu(osu):
osu_lines = open(osu, 'r').read().replace('\x00', '').split('\n')
sections = {}
current_section = (None, [])
for line in osu_lines:
line = line.strip()
secm = re.match('^\[(\w+)\]$', line)
if secm:
if current_section:
sections[current_section[0]] = current_section[1]
current_section = (secm.group(1), [])
else:
if current_section:
current_section[1].append(line)
else:
current_section = ('Default', [line])
if current_section:
sections[current_section[0]] = current_section[1]
return sections
def get_osu_key(osu, section, key, default=None):
sec = osu[section]
for line in sec:
ok = line.split(':', 1)[0].strip()
ov = line.split(':', 1)[1].strip()
if ok.lower() == key.lower():
return ov
return default
def get_preview(song_id, song_type):
preview = 0
if song_type == "tja":
if os.path.isfile('public/songs/%s/main.tja' % song_id):
preview = get_tja_preview('public/songs/%s/main.tja' % song_id)
else:
osus = [osu for osu in os.listdir('public/songs/%s' % song_id) if osu in ['easy.osu', 'normal.osu', 'hard.osu', 'oni.osu']]
if osus:
osud = parse_osu('public/songs/%s/%s' % (song_id, osus[0]))
preview = int(get_osu_key(osud, 'General', 'PreviewTime', 0))
return preview
def get_tja_preview(tja):
tja_lines = open(tja, 'r').read().replace('\x00', '').split('\n')
for line in tja_lines:
line = line.strip()
if ':' in line:
name, value = line.split(':', 1)
if name.lower() == 'demostart':
value = value.strip()
try:
value = float(value)
except ValueError:
pass
else:
return int(value * 1000)
elif line.lower() == '#start':
break
return 0
def get_version(): def get_version():
version = {'commit': None, 'commit_short': '', 'version': None, 'url': DEFAULT_URL} version = {'commit': None, 'commit_short': '', 'version': None, 'url': DEFAULT_URL}
if os.path.isfile('version.json'): if os.path.isfile('version.json'):
@ -168,7 +97,7 @@ def route_api_preview():
abort(400) abort(400)
song_type = song_row[0][12] song_type = song_row[0][12]
prev_path = make_preview(song_id, song_type) prev_path = make_preview(song_id, song_type, song_row[0][15])
if not prev_path: if not prev_path:
return redirect(get_config()['songs_baseurl'] + '%s/main.mp3' % song_id) return redirect(get_config()['songs_baseurl'] + '%s/main.mp3' % song_id)
@ -195,7 +124,7 @@ def route_api_songs():
for song in songs: for song in songs:
song_id = song[0] song_id = song[0]
song_type = song[12] song_type = song[12]
preview = get_preview(song_id, song_type) preview = song[15]
category_out = categories[song[11]] if song[11] in categories else def_category category_out = categories[song[11]] if song[11] in categories else def_category
song_skin_out = song_skins[song[14]] if song[14] in song_skins else None song_skin_out = song_skins[song[14]] if song[14] in song_skins else None
@ -226,12 +155,11 @@ def route_api_config():
return jsonify(config) return jsonify(config)
def make_preview(song_id, song_type): def make_preview(song_id, song_type, preview):
song_path = 'public/songs/%s/main.mp3' % song_id song_path = 'public/songs/%s/main.mp3' % song_id
prev_path = 'public/songs/%s/preview.mp3' % song_id prev_path = 'public/songs/%s/preview.mp3' % song_id
if os.path.isfile(song_path) and not os.path.isfile(prev_path): if os.path.isfile(song_path) and not os.path.isfile(prev_path):
preview = get_preview(song_id, song_type) / 1000
if not preview or preview <= 0.1: if not preview or preview <= 0.1:
print('Skipping #%s due to no preview' % song_id) print('Skipping #%s due to no preview' % song_id)
return False return False