Skip to content

Commit

Permalink
feat: (localMusic) 优化读取本地音乐meta信息,修复重复读取的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
helloplhm-qwq committed Feb 4, 2024
1 parent 35f1fe5 commit b9a1e53
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
17 changes: 12 additions & 5 deletions common/localMusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sys
from PIL import Image
import aiohttp
from common.utils import createMD5, timeLengthFormat
from common.utils import createFileMD5, createMD5, timeLengthFormat
from . import log, config
import ujson as json
import traceback
Expand Down Expand Up @@ -211,6 +211,7 @@ def getAudioMeta(filepath):
"lyrics": lyric[0],
'length': audio.info.length,
'format_length': timeLengthFormat(audio.info.length),
'md5': createFileMD5(filepath),
}
except:
logger.error(f"get audio meta error: {filepath}")
Expand Down Expand Up @@ -238,7 +239,7 @@ def extractCover(audio_info, temp_path):
f.write(audio_info['cover'])
return path

def findAudios():
def findAudios(cache):

available_exts = [
'mp3',
Expand All @@ -253,15 +254,21 @@ def findAudios():
return []

audios = []
_map = {}
for c in cache:
_map[c['filepath']] = c
for file in files:
if (not file.endswith(tuple(available_exts))):
continue
path = os.path.join(AUDIO_PATH, file)
if (not checkAudioValid(path)):
continue
logger.info(f"found audio: {path}")
meta = getAudioMeta(path)
audios = audios + [meta]
if (not (_map.get(path) and _map[path]['md5'] == createFileMD5(path))):
meta = getAudioMeta(path)
audios = audios + [meta]
else:
audios = audios + [_map[path]]

return audios

Expand Down Expand Up @@ -321,7 +328,7 @@ def initMain():
if (cache['file_list'] == os.listdir(AUDIO_PATH)):
audios = cache['audios']
else:
audios = findAudios()
audios = findAudios(cache['audios'])
writeLocalCache(audios)
for a in audios:
map[a['filepath']] = a
Expand Down
11 changes: 9 additions & 2 deletions common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# ----------------------------------------
# This file is part of the "lx-music-api-server" project.

import hashlib
import platform
import binascii
import builtins
Expand All @@ -16,7 +17,6 @@
import re
import xmltodict
from urllib.parse import quote, unquote, urlparse
from hashlib import md5 as handleCreateMD5

def createBase64Encode(data_bytes):
encoded_data = base64.b64encode(data_bytes)
Expand Down Expand Up @@ -67,7 +67,14 @@ def filterFileName(filename):
def createMD5(s: (str, bytes)):
if (isinstance(s, str)):
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"):
try:
Expand Down

0 comments on commit b9a1e53

Please sign in to comment.