mirror of
https://github.com/MeoProject/lx-music-api-server.git
synced 2025-07-06 22:42:14 +08:00
chore: 优化代码
This commit is contained in:
@ -17,27 +17,27 @@ import re
|
||||
import ujson as json
|
||||
import xmltodict
|
||||
from urllib.parse import quote
|
||||
from hashlib import md5 as _md5
|
||||
from hashlib import md5 as handleCreateMD5
|
||||
from aiohttp.web import Response
|
||||
# from flask import Response
|
||||
|
||||
def to_base64(data_bytes):
|
||||
def createBase64Encode(data_bytes):
|
||||
encoded_data = base64.b64encode(data_bytes)
|
||||
return encoded_data.decode('utf-8')
|
||||
|
||||
def to_hex(data_bytes):
|
||||
def createHexEncode(data_bytes):
|
||||
hex_encoded = binascii.hexlify(data_bytes)
|
||||
return hex_encoded.decode('utf-8')
|
||||
|
||||
def from_base64(data):
|
||||
def createBase64Decode(data):
|
||||
decoded_data = base64.b64decode(data)
|
||||
return decoded_data
|
||||
|
||||
def from_hex(data):
|
||||
def createHexDecode(data):
|
||||
decoded_data = binascii.unhexlify(data.decode('utf-8'))
|
||||
return decoded_data
|
||||
|
||||
def inflate_raw_sync(data):
|
||||
def handleInflateRawSync(data):
|
||||
decompress_obj = zlib.decompressobj(-zlib.MAX_WBITS)
|
||||
decompressed_data = decompress_obj.decompress(data) + decompress_obj.flush()
|
||||
return decompressed_data
|
||||
@ -54,10 +54,10 @@ def require(module):
|
||||
index += 1
|
||||
return _module
|
||||
|
||||
def add_to_global_namespace(key, data):
|
||||
def addToGlobalNamespace(key, data):
|
||||
setattr(builtins, key, data)
|
||||
|
||||
def sanitize_filename(filename):
|
||||
def filterFileName(filename):
|
||||
if platform.system() == 'Windows' or platform.system() == 'Cygwin':
|
||||
# Windows不合法文件名字符
|
||||
illegal_chars = r'[<>:"/\\|?*\x00-\x1f]'
|
||||
@ -67,21 +67,19 @@ def sanitize_filename(filename):
|
||||
# 将不合法字符替换为下划线
|
||||
return re.sub(illegal_chars, '_', filename)
|
||||
|
||||
def md5(s: str):
|
||||
# 计算md5
|
||||
# print(s)
|
||||
return _md5(s.encode("utf-8")).hexdigest()
|
||||
def createMD5(s: str):
|
||||
return handleCreateMD5(s.encode("utf-8")).hexdigest()
|
||||
|
||||
def readfile(path, mode = "text"):
|
||||
def readFile(path, mode = "text"):
|
||||
try:
|
||||
fileObj = open(path, "rb")
|
||||
except FileNotFoundError:
|
||||
return "file not found"
|
||||
content = fileObj.read()
|
||||
if mode == "base64":
|
||||
return to_base64(content)
|
||||
return createBase64Encode(content)
|
||||
elif mode == "hex":
|
||||
return to_hex(content)
|
||||
return createHexEncode(content)
|
||||
elif mode == "text":
|
||||
return content.decode("utf-8")
|
||||
else:
|
||||
@ -92,29 +90,29 @@ def unique_list(list_in):
|
||||
[unique_list.append(x) for x in list_in if x not in unique_list]
|
||||
return unique_list
|
||||
|
||||
def handle_response(dic, status = 200):
|
||||
def handleResult(dic, status = 200):
|
||||
return Response(body = json.dumps(dic, indent=2, ensure_ascii=False), content_type='application/json', status = status)
|
||||
|
||||
def encodeURIComponent(component):
|
||||
return quote(component)
|
||||
|
||||
def sort_dict(dictionary):
|
||||
def sortDict(dictionary):
|
||||
sorted_items = sorted(dictionary.items())
|
||||
sorted_dict = {k: v for k, v in sorted_items}
|
||||
return sorted_dict
|
||||
|
||||
def merge_dict(dict1, dict2):
|
||||
def mergeDict(dict1, dict2):
|
||||
merged_dict = dict2.copy()
|
||||
merged_dict.update(dict1)
|
||||
return merged_dict
|
||||
|
||||
class jsobject(dict):
|
||||
class CreateObject(dict):
|
||||
def __init__(self, d):
|
||||
super().__init__(d)
|
||||
self._raw = d
|
||||
for key, value in d.items():
|
||||
if isinstance(value, dict):
|
||||
setattr(self, key, jsobject(value))
|
||||
setattr(self, key, CreateObject(value))
|
||||
else:
|
||||
setattr(self, key, value)
|
||||
|
||||
@ -126,7 +124,7 @@ class jsobject(dict):
|
||||
def to_dict(self):
|
||||
result = {}
|
||||
for key, value in self.items():
|
||||
if isinstance(value, jsobject):
|
||||
if isinstance(value, CreateObject):
|
||||
result[key] = value.to_dict()
|
||||
else:
|
||||
result[key] = value
|
||||
@ -141,5 +139,5 @@ def dump_xml(data):
|
||||
def load_xml(data):
|
||||
return xmltodict.parse(data)
|
||||
|
||||
add_to_global_namespace('require', require)
|
||||
addToGlobalNamespace('require', require)
|
||||
|
||||
|
Reference in New Issue
Block a user