mirror of
https://github.com/MeoProject/lx-music-api-server.git
synced 2025-07-07 06:52:11 +08:00
feat: (localMusic) 优化读取本地音乐meta信息,修复重复读取的问题
This commit is contained in:
@ -12,7 +12,7 @@ import subprocess
|
|||||||
import sys
|
import sys
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from common.utils import createMD5, timeLengthFormat
|
from common.utils import createFileMD5, createMD5, timeLengthFormat
|
||||||
from . import log, config
|
from . import log, config
|
||||||
import ujson as json
|
import ujson as json
|
||||||
import traceback
|
import traceback
|
||||||
@ -211,6 +211,7 @@ def getAudioMeta(filepath):
|
|||||||
"lyrics": lyric[0],
|
"lyrics": lyric[0],
|
||||||
'length': audio.info.length,
|
'length': audio.info.length,
|
||||||
'format_length': timeLengthFormat(audio.info.length),
|
'format_length': timeLengthFormat(audio.info.length),
|
||||||
|
'md5': createFileMD5(filepath),
|
||||||
}
|
}
|
||||||
except:
|
except:
|
||||||
logger.error(f"get audio meta error: {filepath}")
|
logger.error(f"get audio meta error: {filepath}")
|
||||||
@ -238,7 +239,7 @@ def extractCover(audio_info, temp_path):
|
|||||||
f.write(audio_info['cover'])
|
f.write(audio_info['cover'])
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def findAudios():
|
def findAudios(cache):
|
||||||
|
|
||||||
available_exts = [
|
available_exts = [
|
||||||
'mp3',
|
'mp3',
|
||||||
@ -253,6 +254,9 @@ def findAudios():
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
audios = []
|
audios = []
|
||||||
|
_map = {}
|
||||||
|
for c in cache:
|
||||||
|
_map[c['filepath']] = c
|
||||||
for file in files:
|
for file in files:
|
||||||
if (not file.endswith(tuple(available_exts))):
|
if (not file.endswith(tuple(available_exts))):
|
||||||
continue
|
continue
|
||||||
@ -260,8 +264,11 @@ def findAudios():
|
|||||||
if (not checkAudioValid(path)):
|
if (not checkAudioValid(path)):
|
||||||
continue
|
continue
|
||||||
logger.info(f"found audio: {path}")
|
logger.info(f"found audio: {path}")
|
||||||
meta = getAudioMeta(path)
|
if (not (_map.get(path) and _map[path]['md5'] == createFileMD5(path))):
|
||||||
audios = audios + [meta]
|
meta = getAudioMeta(path)
|
||||||
|
audios = audios + [meta]
|
||||||
|
else:
|
||||||
|
audios = audios + [_map[path]]
|
||||||
|
|
||||||
return audios
|
return audios
|
||||||
|
|
||||||
@ -321,7 +328,7 @@ def initMain():
|
|||||||
if (cache['file_list'] == os.listdir(AUDIO_PATH)):
|
if (cache['file_list'] == os.listdir(AUDIO_PATH)):
|
||||||
audios = cache['audios']
|
audios = cache['audios']
|
||||||
else:
|
else:
|
||||||
audios = findAudios()
|
audios = findAudios(cache['audios'])
|
||||||
writeLocalCache(audios)
|
writeLocalCache(audios)
|
||||||
for a in audios:
|
for a in audios:
|
||||||
map[a['filepath']] = a
|
map[a['filepath']] = a
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
# ----------------------------------------
|
# ----------------------------------------
|
||||||
# This file is part of the "lx-music-api-server" project.
|
# This file is part of the "lx-music-api-server" project.
|
||||||
|
|
||||||
|
import hashlib
|
||||||
import platform
|
import platform
|
||||||
import binascii
|
import binascii
|
||||||
import builtins
|
import builtins
|
||||||
@ -16,7 +17,6 @@ import time
|
|||||||
import re
|
import re
|
||||||
import xmltodict
|
import xmltodict
|
||||||
from urllib.parse import quote, unquote, urlparse
|
from urllib.parse import quote, unquote, urlparse
|
||||||
from hashlib import md5 as handleCreateMD5
|
|
||||||
|
|
||||||
def createBase64Encode(data_bytes):
|
def createBase64Encode(data_bytes):
|
||||||
encoded_data = base64.b64encode(data_bytes)
|
encoded_data = base64.b64encode(data_bytes)
|
||||||
@ -67,7 +67,14 @@ def filterFileName(filename):
|
|||||||
def createMD5(s: (str, bytes)):
|
def createMD5(s: (str, bytes)):
|
||||||
if (isinstance(s, str)):
|
if (isinstance(s, str)):
|
||||||
s = s.encode("utf-8")
|
s = s.encode("utf-8")
|
||||||
return handleCreateMD5(s).hexdigest()
|
return hashlib.md5(s).hexdigest()
|
||||||
|
|
||||||
|
def createFileMD5(path):
|
||||||
|
with open(path, 'rb') as f:
|
||||||
|
md5 = hashlib.md5()
|
||||||
|
for chunk in iter(lambda: f.read(4096), b""):
|
||||||
|
md5.update(chunk)
|
||||||
|
return md5.hexdigest()
|
||||||
|
|
||||||
def readFile(path, mode = "text"):
|
def readFile(path, mode = "text"):
|
||||||
try:
|
try:
|
||||||
|
Reference in New Issue
Block a user