Skip to content

Commit

Permalink
improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
yuygfgg committed Jul 29, 2024
1 parent a74ba9c commit ed13ae2
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import threading
from flask import Flask, Response, request, jsonify, send_file, send_from_directory
from musiclib import MusicLibrary, Artist, Album, Song
import os
import sys
from flask_cors import CORS
from flask_compress import Compress
import json
import orjson
import opencc
import datetime
import logging
Expand All @@ -20,6 +22,10 @@
static_folder = os.path.join(current_dir, 'webui')

def save_library():
thread = threading.Thread(target=do_save_library)
thread.start()

def do_save_library():
def convert_datetime(obj):
if isinstance(obj, datetime.datetime):
return obj.isoformat()
Expand Down Expand Up @@ -48,16 +54,15 @@ def serialize_data(data):
'album': song.album['uuid'],
'artists': [artist['uuid'] for artist in song.artists]
}) for uuid, song in library.songs.items()},
'graph': serialize_data(library.graph) # Convert sets to lists for JSON serialization
}
try:
os.remove(data_file)
except Exception as e:
print(e)

try:
with open(data_file, 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4, default=convert_datetime)
with open(data_file, 'wb') as file:
file.write(orjson.dumps(data, default=convert_datetime))
print("Library saved to file")
except Exception as e:
print(f"Failed to save library to file: {e}")
Expand Down Expand Up @@ -160,7 +165,8 @@ def parse_datetime(obj, fields):

# Restore graph
try:
library.graph = {k: {kk: {'strength': vv['strength'], 'details': set(vv['details'])} for kk, vv in v.items()} for k, v in data.get('graph', {}).items()}
# library.graph = {k: {kk: {'strength': vv['strength'], 'details': set(vv['details'])} for kk, vv in v.items()} for k, v in data.get('graph', {}).items()}
library.graph = library.build_graph()
logger.debug("Restored graph")
except Exception as e:
logger.error(f"Failed to restore graph: {e}")
Expand Down

0 comments on commit ed13ae2

Please sign in to comment.